-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSysquake.htm
More file actions
3030 lines (2733 loc) · 115 KB
/
Sysquake.htm
File metadata and controls
3030 lines (2733 loc) · 115 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<HEAD>
<TITLE>Using lpsolve from Sysquake</TITLE>
<style TYPE="text/css"> BODY { font-family:verdana,arial,helvetica; margin:15; }
</style>
</HEAD>
<BODY>
<h1 align="left"><u>Using lpsolve from Sysquake</u></h1>
<a name="Sysquake"></a>
<h3>Sysquake?</h3>
<p>
Sysquake is an interactive design CAD tool for getting insight into complicated scientific problems and designing
advanced technical devices.
</p>
<p>
To design technical devices, or to understand the physical and mathematical
laws which describe their behavior, engineers and scientists frequently use
computers to calculate and represent graphically different quantities, such as
the sample sequence and the frequency response of a digital audio filter,
or the trajectory and the mass of a rocket flying to Mars.
Usually, these quantities are related to each other; they are different
views of the same reality. Understanding these relationships is the key to a
good design. In some cases, especially for simple systems, an intuitive
understanding can be acquired. For more complicated systems, it is often
difficult or impossible to "guess", for instance, whether increasing the
thickness of a robot arm will increase or decrease the frequency of the oscillations.</p>
<p>Traditionally, the design of a complicated system is performed in several
iterations. Specifications can seldom be used directly to calculate the value
of the parameters of the system, because there is no explicit formula to link
them. Hence each iteration is made of two phases. The first one, often called
<em>synthesis</em>, consists in calculating the unknown parameters of the system
based on a set of <em>design variables</em>. The design variables are more or
less loosely related to the specifications. During the second phase, called
<em>analysis</em>, the performance of the system is evaluated and compared
to the specifications. If it does not match them, the design variables are
modified and a new iteration is carried out.</p>
<p>When the relationship between the criteria used for evaluating the performance
and the design parameters is not very well known, modifications of the design
parameters might lead as well to poorer performance as to better one. Manual
trial and error may work but is cumbersome. This is where interactive design
may help. Instead of splitting each iteration between synthesis and analysis,
both phases are merged into a single one where the effect of modifying a parameter
results immediately in the update of graphics. The whole design procedure
becomes really dynamic; the engineer perceives the gradient of the change of
performance criteria with respect to what he manipulates, and the compromises
which can be obtained are easily identified.</p>
<p>Sysquake's purpose is to support this kind of design in fields such as automatic
control and signal processing. Several graphics are displayed simultaneously,
and some of them contain elements which can be manipulated with the mouse.
During the manipulation, all the graphics are updated to reflect the change.
What the graphics show and how their update is performed are not fixed, but
depend on programs written in an easy-to-learn language specialized for numerical
computation. Several programs are included with Sysquake for common tasks, such
as the design of PID controllers; but you are free to modify
them to better suit your needs and to write new ones for other design methods or
new applications.</p>
<p>Another area where Sysquake shines is teaching. Replacing the static figures
you find in books or the animations you see on the Web with interactive graphics,
where the student can manipulate himself the curves to acquire an intuitive
understanding of the theory they represent, accelerates and improves the learning
process tremendously.</p>
<p>We will not discuss the specifics of Sysquake here but instead refer the reader to the
<a href="http://www.calerga.com/products/Sysquake/index.html">Sysquake</a> website and
<a href="http://www.calerga.com/doc/index.html">documentation</a>.
</p>
<a name="Sysquake_and_lpsolve"></a>
<h3>Sysquake and lpsolve</h3>
<p>lpsolve is callable from Sysquake via an external interface or external code. As such, it looks like lpsolve is fully integrated
with Sysquake. Matrices can directly be transferred between Sysquake and lpsolve in both directions. The complete interface
is written in C so it has maximum performance. The whole lpsolve API is implemented with some extra's specific for
Sysquake (especially for matrix support). So you have full control to the complete lpsolve functionality via the sqlpsolve
Sysquake driver. If you find that this involves too much work to solve an lp model then you can also work via a higher-level
library that can make things a lot easier. See further in this article.
</p>
<p>Sysquake is ideally suited to handle linear programming problems.
These are problems in which you have a quantity, depending linearly on several variables,
that you want to maximize or minimize subject to several constraints that are expressed
as linear inequalities in the same variables. If the number of variables and the number
of constraints are small, then there are numerous mathematical techniques for solving a
linear programming problem.
Indeed these techniques are often taught in high school or university level
courses in finite mathematics. But sometimes these numbers are high, or even if low,
the constants in the linear inequalities or the object expression for the quantity
to be optimized may be numerically complicated in which case a software package like
Sysquake is required to effect a solution.</p>
<p>Although there is a free version of Sysquake (Sysquake LE), it does not support to call
external library code as needed by lpsolve. As such, the lpsolve link to Sysquake only works
with the commercial version.
</p>
<a name="Installation"></a>
<h3>Installation</h3>
<p>To make this possible, a driver program is needed: sqlpsolve (sqlpsolvext.dll under Windows, sqlpsolvext.so under Unix/Linux).
This driver must be put in a directory known to Sysquake (LMEExt under Windows, sys under Unix/Linux) and Sysquake can call the sqlpsolve solver.</p>
<p>This driver calls lpsolve via the lpsolve shared library (lpsolve55.dll under Windows
and liblpsolve55.so under Unix/Linux) (archive lp_solve_5.5.2.14_dev.zip/lp_solve_5.5.2.14_dev.tar.gz). This has the advantage that the sqlpsolve driver doesn't have to
be recompiled when an update of lpsolve is provided. The shared library must be somewhere in the Windows path.</p>
<p>So note the difference between the Sysquake lpsolve driver that is called sqlpsolve, its physical name on disk (sqlpsolvext.*) and the lpsolve library that implements the
API that is named lpsolve55.* on disk.</p>
<p>There is also the sqlpsolve.lml higher level library file and some example library files (.lml) as a quick start.</p>
<p>To test if everything is installed correctly, enter sqlpsolve(); in the Sysquake command window.
If it gives the following, then everything is ok:</p>
<pre>sqlpsolve Sysquake Interface version 5.5.0.7
using lpsolve version 5.5.2.14
Usage: ret = sqlpsolve('functionname', arg1, arg2, ...)
</pre>
<p>However, if you get the following:</p>
<pre>Undefined function 'sqlpsolve'
</pre>
<p>Then Sysquake cannot find either sqlpsolve.dll/sqlpsolve.so or lpsolve55.dll/liblpsolve55.so.<br>
On Unix/Linux, if liblpsolve55.so cannot be found, then an error message is also displayed when Sysquake is started
when all external libraries are loaded:</p>
<pre>liblpsolve55.so: cannot open shared object file: No such file or directory
</pre>
<p>Unfortunately, under Windows no message at all is shown and you can't know if the problem
is that sqlpsolveext.dll cannot be found or lpsolve55.dll.</p>
<p>Note that it is necessary to restart Sysquake after having put the files in the specified directory.
Sysquake loads the libraries only at startup. When started, a list of loaded extensions is given.
Make sure that between the list, there is also:
</p>
<pre>lpsolve extension: sqlpsolve
</pre>
<p>You can also use the following Sysquake command:</p>
<pre>info b
</pre>
<p>A list of loaded extension functions is given. LME/sqlpsolve must be in the list to be ok.</p>
<p>All this is developed and tested with Sysquake version 4.1 Pro.</p>
<a name="Solve_an_lp_model_from_Sysquake_via_sqlpsolve"></a>
<h3>Solve an lp model from Sysquake via sqlpsolve</h3>
<p>In the following text, > before the Sysquake commands is the Sysquake prompt.
Only the text after > must be entered.
</p>
<p>To call an lpsolve function, the following syntax must be used:</p>
<pre>> [ret1, ret2, ...] = sqlpsolve('functionname', arg1, arg2, ...)</pre>
<p>The return values are optional and depend on the function called. functionname must always be enclosed between single
quotes to make it alphanumerical and it is case sensitive. The number and type of arguments depend on the function called.
Some functions even have a variable number of arguments and a different behaviour occurs depending on the type of the argument.
functionname can be (almost) any of the lpsolve API routines (see <a href="lp_solveAPIreference.htm">lp_solve API reference</a>)
plus some extra Sysquake specific functions.
Most of the lpsolve API routines use or return an lprec structure. To make things more robust in Sysquake, this structure
is replaced by a handle or the model name. The lprec structures are maintained internally by the lpsolve driver.
The handle is an incrementing number starting from 0.
Starting from driver version 5.5.0.2, it is also possible to use the model name instead of the handle.
This can of course only be done if a name is given to the model. This is done via lpsolve routine
<a href="#set_lp_name">set_lp_name</a> or by specifying the model name in routine <a href="#read_lp">read_lp</a>.
See <a href="#Using_model_name_instead_of_handle">Using model name instead of handle</a>.
</p>
<p>Almost all callable functions can be found in the <a href="lp_solveAPIreference.htm">lp_solve API reference</a>.
Some are exactly as described in the reference guide, others have a slightly different syntax to make maximum
use of the Sysquake functionality. For example make_lp is used identical as described. But get_variables is slightly
different. In the API reference, this function has two arguments. The first the lp handle and the second the
resulting variables and this array must already be dimensioned. When lpsolve is used from Sysquake, nothing must
be dimensioned in advance. The sqlpsolve driver takes care of dimensioning all return variables and they are
always returned as return value of the call to sqlpsolve. Never as argument to the routine. This can be a single
value as for get_objective (although Sysquake stores this in a 1x1 matrix) or a matrix or vector as in get_variables.
In this case, get_variables returns a 4x1 matrix (vector) with the result of the 4 variables of the lp model.
</p>
<a name="Constants"></a>
<h3>Constants</h3>
<p>Some API calls needs constant arguments. For example <a href="add_constraint.htm">add_constraint</a> has the constants
LE, GE, EQ to define if the added constraint is a less than, greater than, equal to the RHS.
Although it is legal and possible to use the constant numbers itself, it is easier, clearer and much readable to use labels.
In this documentation these are used everywere. They are not defined in the external library sqlpsolveext.dll/so.
They are however defined in the library sqlpsolve.lml. To use these, the library must first be loaded in Sysquaqe via a use statement:</p>
<pre>use sqlpsolve
</pre>
<p>See also <a href="#libraries">libraries</a></p>
<p>Here is a list of all constants defined in sqlpsolve.lml (end of file):</p>
<pre>public
define LPSOLVE_ANTIDEGEN_BOUNDFLIP = 512;
define LPSOLVE_ANTIDEGEN_COLUMNCHECK = 2;
define LPSOLVE_ANTIDEGEN_DURINGBB = 128;
define LPSOLVE_ANTIDEGEN_DYNAMIC = 64;
define LPSOLVE_ANTIDEGEN_FIXEDVARS = 1;
define LPSOLVE_ANTIDEGEN_INFEASIBLE = 32;
define LPSOLVE_ANTIDEGEN_LOSTFEAS = 16;
define LPSOLVE_ANTIDEGEN_NONE = 0;
define LPSOLVE_ANTIDEGEN_NUMFAILURE = 8;
define LPSOLVE_ANTIDEGEN_RHSPERTURB = 256;
define LPSOLVE_ANTIDEGEN_STALLING = 4;
define LPSOLVE_BRANCH_AUTOMATIC = 2;
define LPSOLVE_BRANCH_DEFAULT = 3;
define LPSOLVE_BRANCH_CEILING = 0;
define LPSOLVE_BRANCH_FLOOR = 1;
define LPSOLVE_CRASH_LEASTDEGENERATE = 3;
define LPSOLVE_CRASH_MOSTFEASIBLE = 2;
define LPSOLVE_CRASH_NONE = 0;
define LPSOLVE_CRITICAL = 1;
define LPSOLVE_DEGENERATE = 4;
define LPSOLVE_DETAILED = 5;
define LPSOLVE_EQ = 3;
define LPSOLVE_FEASFOUND = 12;
define LPSOLVE_FR = 0;
define LPSOLVE_FULL = 6;
define LPSOLVE_GE = 2;
define LPSOLVE_IMPORTANT = 3;
define LPSOLVE_IMPROVE_BBSIMPLEX = 8;
define LPSOLVE_IMPROVE_DUALFEAS = 2;
define LPSOLVE_IMPROVE_NONE = 0;
define LPSOLVE_IMPROVE_SOLUTION = 1;
define LPSOLVE_IMPROVE_THETAGAP = 4;
define LPSOLVE_INFEASIBLE = 2;
define LPSOLVE_Infinite = 1e+030;
define LPSOLVE_LE = 1;
define LPSOLVE_MSG_LPFEASIBLE = 8;
define LPSOLVE_MSG_LPOPTIMAL = 16;
define LPSOLVE_MSG_MILPBETTER = 512;
define LPSOLVE_MSG_MILPEQUAL = 256;
define LPSOLVE_MSG_MILPFEASIBLE = 128;
define LPSOLVE_MSG_PRESOLVE = 1;
define LPSOLVE_NEUTRAL = 0;
define LPSOLVE_NODE_AUTOORDER = 8192;
define LPSOLVE_NODE_BRANCHREVERSEMODE = 16;
define LPSOLVE_NODE_BREADTHFIRSTMODE = 4096;
define LPSOLVE_NODE_DEPTHFIRSTMODE = 128;
define LPSOLVE_NODE_DYNAMICMODE = 1024;
define LPSOLVE_NODE_FIRSTSELECT = 0;
define LPSOLVE_NODE_FRACTIONSELECT = 3;
define LPSOLVE_NODE_GAPSELECT = 1;
define LPSOLVE_NODE_GREEDYMODE = 32;
define LPSOLVE_NODE_GUBMODE = 512;
define LPSOLVE_NODE_PSEUDOCOSTMODE = 64;
define LPSOLVE_NODE_PSEUDOCOSTSELECT = 4;
define LPSOLVE_NODE_PSEUDONONINTSELECT = 5;
define LPSOLVE_NODE_PSEUDORATIOSELECT = 6;
define LPSOLVE_NODE_RANDOMIZEMODE = 256;
define LPSOLVE_NODE_RANGESELECT = 2;
define LPSOLVE_NODE_RCOSTFIXING = 16384;
define LPSOLVE_NODE_RESTARTMODE = 2048;
define LPSOLVE_NODE_STRONGINIT = 32768;
define LPSOLVE_NODE_USERSELECT = 7;
define LPSOLVE_NODE_WEIGHTREVERSEMODE = 8;
define LPSOLVE_NOFEASFOUND = 13;
define LPSOLVE_NOMEMORY = -2;
define LPSOLVE_NORMAL = 4;
define LPSOLVE_NUMFAILURE = 5;
define LPSOLVE_OPTIMAL = 0;
define LPSOLVE_PRESOLVED = 9;
define LPSOLVE_PRESOLVE_BOUNDS = 262144;
define LPSOLVE_PRESOLVE_COLDOMINATE = 16384;
define LPSOLVE_PRESOLVE_COLFIXDUAL = 131072;
define LPSOLVE_PRESOLVE_COLS = 2;
define LPSOLVE_PRESOLVE_DUALS = 524288;
define LPSOLVE_PRESOLVE_ELIMEQ2 = 256;
define LPSOLVE_PRESOLVE_IMPLIEDFREE = 512;
define LPSOLVE_PRESOLVE_IMPLIEDSLK = 65536;
define LPSOLVE_PRESOLVE_KNAPSACK = 128;
define LPSOLVE_PRESOLVE_LINDEP = 4;
define LPSOLVE_PRESOLVE_MERGEROWS = 32768;
define LPSOLVE_PRESOLVE_NONE = 0;
define LPSOLVE_PRESOLVE_PROBEFIX = 2048;
define LPSOLVE_PRESOLVE_PROBEREDUCE = 4096;
define LPSOLVE_PRESOLVE_REDUCEGCD = 1024;
define LPSOLVE_PRESOLVE_REDUCEMIP = 64;
define LPSOLVE_PRESOLVE_ROWDOMINATE = 8192;
define LPSOLVE_PRESOLVE_ROWS = 1;
define LPSOLVE_PRESOLVE_SENSDUALS = 1048576;
define LPSOLVE_PRESOLVE_SOS = 32;
define LPSOLVE_PRICER_DANTZIG = 1;
define LPSOLVE_PRICER_DEVEX = 2;
define LPSOLVE_PRICER_FIRSTINDEX = 0;
define LPSOLVE_PRICER_STEEPESTEDGE = 3;
define LPSOLVE_PRICE_ADAPTIVE = 32;
define LPSOLVE_PRICE_AUTOPARTIAL = 256;
define LPSOLVE_PRICE_HARRISTWOPASS = 4096;
define LPSOLVE_PRICE_LOOPALTERNATE = 2048;
define LPSOLVE_PRICE_LOOPLEFT = 1024;
define LPSOLVE_PRICE_MULTIPLE = 8;
define LPSOLVE_PRICE_PARTIAL = 16;
define LPSOLVE_PRICE_PRIMALFALLBACK = 4;
define LPSOLVE_PRICE_RANDOMIZE = 128;
define LPSOLVE_PRICE_TRUENORMINIT = 16384;
define LPSOLVE_PROCBREAK = 11;
define LPSOLVE_PROCFAIL = 10;
define LPSOLVE_SCALE_COLSONLY = 1024;
define LPSOLVE_SCALE_CURTISREID = 7;
define LPSOLVE_SCALE_DYNUPDATE = 256;
define LPSOLVE_SCALE_EQUILIBRATE = 64;
define LPSOLVE_SCALE_EXTREME = 1;
define LPSOLVE_SCALE_GEOMETRIC = 4;
define LPSOLVE_SCALE_INTEGERS = 128;
define LPSOLVE_SCALE_LOGARITHMIC = 16;
define LPSOLVE_SCALE_MEAN = 3;
define LPSOLVE_SCALE_NONE = 0;
define LPSOLVE_SCALE_POWER2 = 32;
define LPSOLVE_SCALE_QUADRATIC = 8;
define LPSOLVE_SCALE_RANGE = 2;
define LPSOLVE_SCALE_ROWSONLY = 512;
define LPSOLVE_SCALE_USERWEIGHT = 31;
define LPSOLVE_SEVERE = 2;
define LPSOLVE_SIMPLEX_DUAL_DUAL = 10;
define LPSOLVE_SIMPLEX_DUAL_PRIMAL = 6;
define LPSOLVE_SIMPLEX_PRIMAL_DUAL = 9;
define LPSOLVE_SIMPLEX_PRIMAL_PRIMAL = 5;
define LPSOLVE_SUBOPTIMAL = 1;
define LPSOLVE_TIMEOUT = 7;
define LPSOLVE_UNBOUNDED = 3;
define LPSOLVE_USERABORT = 6;
</pre>
Also see <a href="#Using_string_constants">Using string constants</a> for an alternative.
<a name="An_example"></a>
<h3>An example</h3>
<p>(Note that you can execute this example by entering command per command as shown below or by just executing the
command from example1.lml as follows:</p>
<pre>use example1; example1;</pre>
<p>This will execute example1.lml. It will also echo the executes commands. See also <a href="#libraries">libraries</a>)</p>
<pre>> use sqlpsolve
> lp=sqlpsolve('make_lp', 0, 4);
> sqlpsolve('set_verbose', lp, 3);
> sqlpsolve('set_obj_fn', lp, [1, 3, 6.24, 0.1]);
> sqlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], LPSOLVE_GE, 92.3);
> sqlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], LPSOLVE_LE, 14.8);
> sqlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], LPSOLVE_GE, 4);
> sqlpsolve('set_lowbo', lp, 1, 28.6);
> sqlpsolve('set_lowbo', lp, 4, 18);
> sqlpsolve('set_upbo', lp, 4, 48.98);
> sqlpsolve('set_col_name', lp, 1, 'COLONE');
> sqlpsolve('set_col_name', lp, 2, 'COLTWO');
> sqlpsolve('set_col_name', lp, 3, 'COLTHREE');
> sqlpsolve('set_col_name', lp, 4, 'COLFOUR');
> sqlpsolve('set_row_name', lp, 1, 'THISROW');
> sqlpsolve('set_row_name', lp, 2, 'THATROW');
> sqlpsolve('set_row_name', lp, 3, 'LASTROW');
> sqlpsolve('write_lp', lp, 'a.lp');
> sqlpsolve('get_mat', lp, 1, 2)
ans =
78.2600
> sqlpsolve('solve', lp)
ans =
0
> sqlpsolve('get_objective', lp)
ans =
31.7828
> sqlpsolve('get_variables', lp)
ans =
28.6000
0.0000
0.0000
31.8276
> sqlpsolve('get_constraints', lp)
ans =
92.3000
6.8640
391.2928
</pre>
<p>Note that there are some commands that return an answer. To see the answer, the command was not terminated with
a semicolon (;). If the semicolon is put at the end of a command, the answer is not shown. However it is also possible
to write the answer in a variable. For example:
</p>
<pre>> obj=sqlpsolve('get_objective', lp)
obj =
31.7828
</pre>
<p>Or without echoing on screen:</p>
<pre>> obj=sqlpsolve('get_objective', lp);</pre>
<p>The last command will only write the result in variable obj without showing anything on screen.
get_variables and get_constraints return a vector with the result. This can also be put in a variable:</p>
<pre>> x=sqlpsolve('get_variables', lp);
> b=sqlpsolve('get_constraints', lp);
</pre>
<p>It is always possible to show the contents of a variable by just giving it as command:</p>
<pre>> x
x =
28.6000
0.0000
0.0000
31.8276
</pre>
<p>Don't forget to free the handle and its associated memory when you are done:</p>
<pre>> sqlpsolve('delete_lp', lp);</pre>
<a name="Using_model_name_instead_of_handle"></a>
<h3>Using model name instead of handle</h3>
From driver version 5.5.0.2, it is possible to use the model name instead of the handle. From the moment the model
has a name, you can use this name instead of the handle. This is best shown by an example. Above example would look
like this:
<pre>> use sqlpsolve
> lp=sqlpsolve('make_lp', 0, 4);
> sqlpsolve('set_lp_name', lp, 'mymodel');
> sqlpsolve('set_verbose', 'mymodel', 3);
> sqlpsolve('set_obj_fn', 'mymodel', [1, 3, 6.24, 0.1]);
> sqlpsolve('add_constraint', 'mymodel', [0, 78.26, 0, 2.9], LPSOLVE_GE, 92.3);
> sqlpsolve('add_constraint', 'mymodel', [0.24, 0, 11.31, 0], LPSOLVE_LE, 14.8);
> sqlpsolve('add_constraint', 'mymodel', [12.68, 0, 0.08, 0.9], LPSOLVE_GE, 4);
> sqlpsolve('set_lowbo', 'mymodel', 1, 28.6);
> sqlpsolve('set_lowbo', 'mymodel', 4, 18);
> sqlpsolve('set_upbo', 'mymodel', 4, 48.98);
> sqlpsolve('set_col_name', 'mymodel', 1, 'COLONE');
> sqlpsolve('set_col_name', 'mymodel', 2, 'COLTWO');
> sqlpsolve('set_col_name', 'mymodel', 3, 'COLTHREE');
> sqlpsolve('set_col_name', 'mymodel', 4, 'COLFOUR');
> sqlpsolve('set_row_name', 'mymodel', 1, 'THISROW');
> sqlpsolve('set_row_name', 'mymodel', 2, 'THATROW');
> sqlpsolve('set_row_name', 'mymodel', 3, 'LASTROW');
> sqlpsolve('write_lp', 'mymodel', 'a.lp');
> sqlpsolve('get_mat', 'mymodel', 1, 2)
ans =
78.2600
> sqlpsolve('solve', 'mymodel')
ans =
0
> sqlpsolve('get_objective', 'mymodel')
ans =
31.7828
> sqlpsolve('get_variables', 'mymodel')
ans =
28.6000
0.0000
0.0000
31.8276
> sqlpsolve('get_constraints', 'mymodel')
ans =
92.3000
6.8640
391.2928
</pre>
<p>So everywhere a handle is needed, you can also use the model name. You can even mix the two methods.
There is also a specific Sysquake routine to get the handle from the model name: <a href="#get_handle">get_handle</a>.<br>
For example:</p>
<pre>
> sqlpsolve('get_handle', 'mymodel')
ans =
0
</pre>
<p>Don't forget to free the handle and its associated memory when you are done:</p>
<pre>> sqlpsolve('delete_lp', 'mymodel');</pre>
<p>In the next part of this documentation, the handle is used. But if you name the model, the name could thus also be used.</p>
<a name="Matrices"></a>
<h3>Matrices</h3>
<p>In Sysquake, all numerical data is stored in matrices; even a scalar variable. Sysquake also supports complex numbers
(a + b * i with i=SQRT(-1)). sqlpsolve can only work with real numbers.</p>
<p>Most of the time, variables are used to provide the data:</p>
<pre>> sqlpsolve('add_constraint', lp, a1, LPSOLVE_LE, 14.8);</pre>
<p>Where a1 is a matrix variable.</p>
<p>Note that if a matrix is provided, the dimension must exactly match the dimension that is expected by
sqlpsolve. Matrices with too few or too much elements gives an 'invalid vector.' error.</p>
<p>Most of the time, sqlpsolve needs vectors (rows or columns).
In all situations, it doesn't matter if the vectors are row or column vectors. The driver accepts them both.
For example:</p>
<pre>> sqlpsolve('add_constraint', lp, [0.24; 0; 11.31; 0], LPSOLVE_LE, 14.8);</pre>
<p>Which is a column vector, but it is also accepted.</p>
<p>An important final note. Several lp_solve API routines accept a vector where the first element (element 0) is not used.
Other lp_solve API calls do use the first element. In the Sysquake interface, there is never an unused element in the matrices.
So if the lp_solve API specifies that the first element is not used, then this element is not in the Sysquake matrix.</p>
<a name="Sets"></a>
<h3>Sets</h3>
<p>All numerical data is stored in matrices. Alphanumerical data, however, is more difficult to store in matrices.
Matrices require that each element has the same size (length) and that is difficult and unpractical for alphanumerical
data. In a limited number of lpsolve routines, alphanumerical data is required or returned and in some also multiple
elements. An example is set_col_name. For this, Sysquake sets are used. To specify a set of alphanumerical elements,
the following notation is used: { 'element1', 'element2', ... }. Note the { and } symbols instead of [ and ] that
are used with matrices.
</p>
<a name="Maximum_usage_of_matrices_sets_with_sqlpsolve"></a>
<h3>Maximum usage of matrices/sets with sqlpsolve</h3>
<p>Because Sysquake is all about matrices, all lpsolve API routines that need a column or row number to get/set information for that
column/row are extended in the sqlpsolve Sysquake driver to also work with matrices. For example set_int in the API can
only set the integer status for one column. If the status for several integer variables must be set, then set_int
must be called multiple times. The sqlpsolve Sysquake driver however also allows specifying a vector to set the integer
status of all variables at once. The API call is: return = sqlpsolve('set_int', lp, column, must_be_int). The
matrix version of this call is: return = sqlpsolve('set_int', lp, [must_be_int]).
The API call to return the integer status of a variable is: return = sqlpsolve('is_int', lp, column). The
matrix version of this call is: [is_int] = sqlpsolve('is_int', lp)<br>
Also note the get_mat and set_mat routines. In Sysquake these are extended to return/set the complete constraint matrix.
See following example.
</p>
<p>Above example can thus also be done as follows:<br>
(Note that you can execute this example by entering command per command as shown below or by just executing the
command from example2.lml as follows:</p>
<pre>use example2; example2;</pre>
<p>This will execute example2.lml. It will also echo the executes commands. See also <a href="#libraries">libraries</a>)</p>
<pre>> use sqlpsolve
> lp=sqlpsolve('make_lp', 0, 4);
> sqlpsolve('set_verbose', lp, 3);
> sqlpsolve('set_obj_fn', lp, [1, 3, 6.24, 0.1]);
> sqlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], LPSOLVE_GE, 92.3);
> sqlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], LPSOLVE_LE, 14.8);
> sqlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], LPSOLVE_GE, 4);
> sqlpsolve('set_lowbo', lp, [28.6, 0, 0, 18]);
> sqlpsolve('set_upbo', lp, [Inf, Inf, Inf, 48.98]);
> sqlpsolve('set_col_name', lp, {'COLONE', 'COLTWO', 'COLTHREE', 'COLFOUR'});
> sqlpsolve('set_row_name', lp, {'THISROW', 'THATROW', 'LASTROW'});
> sqlpsolve('write_lp', lp, 'a.lp');
> sqlpsolve('get_mat', lp)
ans =
0.0000 78.2600 0.0000 2.9000
0.2400 0.0000 11.3100 0.0000
12.6800 0.0000 0.0800 0.9000
> sqlpsolve('solve', lp)
ans =
0
> sqlpsolve('get_objective', lp)
ans =
31.7828
> sqlpsolve('get_variables', lp)
ans =
28.6000
0.0000
0.0000
31.8276
> sqlpsolve('get_constraints', lp)
ans =
92.3000
6.8640
391.2928
</pre>
<p>Note the usage of Inf in set_upbo. This stands for 'infinity'. Meaning an infinite upper bound.
It is also possible to use -Inf to express minus infinity. This can for example be used to create a free variable.</p>
<p>To show the full power of the matrices, let's now do some matrix calculations to check the solution.
It works further on above example:</p>
<pre>> A=sqlpsolve('get_mat', lp);
> X=sqlpsolve('get_variables', lp);
> B = A * X
B =
92.3000
6.8640
391.2928
</pre>
<p>So what we have done here is calculate the values of the constraints (RHS) by multiplying the constraint matrix
with the solution vector. Now take a look at the values of the constraints that lpsolve has found:</p>
<pre>> sqlpsolve('get_constraints', lp)
ans =
92.3000
6.8640
391.2928
</pre>
<p>Exactly the same as the calculated B vector, as expected.</p>
<p>Also the value of the objective can be calculated in a same way:</p>
<pre>> C=sqlpsolve('get_obj_fn', lp);
> X=sqlpsolve('get_variables', lp);
> obj = C * X
obj =
31.7828
</pre>
<p>So what we have done here is calculate the value of the objective by multiplying the objective vector
with the solution vector. Now take a look at the value of the objective that lpsolve has found:</p>
<pre>> sqlpsolve('get_objective', lp)
ans =
31.7828
</pre>
<p>Again exactly the same as the calculated obj value, as expected.</p>
<a name="Using_string_constants"></a>
<h3>Using string constants</h3>
From driver version 5.5.2.14 on, it is possible to use string constants
everywhere an lp_solve constant is needed or returned. This is best shown by an example.
In the above code we had:
<pre>> use sqlpsolve
> lp=sqlpsolve('make_lp', 0, 4);
> sqlpsolve('set_verbose', lp, 3);
> sqlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], LPSOLVE_GE, 92.3);
> sqlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], LPSOLVE_LE, 14.8);
> sqlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], LPSOLVE_GE, 4);</pre>
<p>Note the 3rd parameter on set_verbose and the 4th on add_constraint. These are
lp_solve constants. One can define all the possible constants in Sysquake as is
done in the sqlpsolve library and
then use them in the calls, but that has several disadvantages. First there
stays the possibility to provide a constant that is not intended for that
particular call. Another issue is that calls that return a constant are still
returning it numerical.</p>
<p>Both issues can now be handled by string constants. The above code can be done as
following with string constants:</p>
<pre>> lp=sqlpsolve('make_lp', 0, 4);
> sqlpsolve('set_verbose', lp, 'IMPORTANT');
> sqlpsolve('add_constraint', lp, [0, 78.26, 0, 2.9], 'GE', 92.3);
> sqlpsolve('add_constraint', lp, [0.24, 0, 11.31, 0], 'LE', 14.8);
> sqlpsolve('add_constraint', lp, [12.68, 0, 0.08, 0.9], 'GE', 4);</pre>
<p>This is not only more readable, there is much lesser chance that mistakes are
being made. The calling routine knows which constants are possible and only
allows these. So unknown constants or constants that are intended for other
calls are not accepted. For example:</p>
<pre>> sqlpsolve('set_verbose', lp, 'blabla');
BLABLA: Unknown.
> sqlpsolve('set_verbose', lp, 'GE');
GE: Not allowed here.</pre>
<p>Note the difference between the two error messages. The first says that the
constant is not known, the second that the constant cannot be used at that
place.</p>
<p>Constants are case insensitive. Internally they are always translated to upper
case. Also when returned they will always be in upper case.</p>
<p>The constant names are the ones as specified in the documentation of each API
routine. There are only 3 exceptions, extensions actually. 'LE', 'GE' and 'EQ' in
<a href="add_constraint.htm">add_constraint</a> and <a href="is_constr_type.htm">is_constr_type</a>
can also be '<', '<=', '>', '>=', '='. When returned however, 'GE', 'LE', 'EQ'
will be used.</p>
<p>Also in the matrix version of calls, string constants are possible. For example:</p>
<pre>> sqlpsolve('set_constr_type', lp, {'LE', 'EQ', 'GE'});</pre>
<p>Some constants can be a combination of multiple constants. For example
<a href="set_scaling.htm">set_scaling</a>:</p>
<pre>> sqlpsolve('set_scaling', lp, 3+128);</pre>
<p>With the string version of constants this can be done as following:</p>
<pre>> sqlpsolve('set_scaling', lp, 'SCALE_MEAN|SCALE_INTEGERS');</pre>
<p>| is the OR operator used to combine multiple constants. There may optinally be
spaces before and after the |.</p>
<p>Not all OR combinations are legal. For example in set_scaling, a choice must be
made between SCALE_EXTREME, SCALE_RANGE, SCALE_MEAN, SCALE_GEOMETRIC or
SCALE_CURTISREID. They may not be combined with each other. This is also tested:</p>
<pre>> sqlpsolve('set_scaling', lp, 'SCALE_MEAN|SCALE_RANGE');
SCALE_RANGE cannot be combined with SCALE_MEAN</pre>
<p>Everywhere constants must be provided, numeric or string values may be provided.
The routine automatically interpretes them. </p>
<p>Returning constants is a different
story. The user must let lp_solve know how to return it. Numerical or as string.
The default is numerical:</p>
<pre>> sqlpsolve('get_scaling', lp)
ans =
131</pre>
<p>To let lp_solve return a constant as string, a call to a new function must be
made: return_constants</p>
<pre>> sqlpsolve('return_constants', 1);</pre>
<p>From now on, all returned constants are returned as string:</p>
<pre>> sqlpsolve('get_scaling', lp)
ans =
SCALE_MEAN|SCALE_INTEGERS</pre>
<p>Also when an array of constants is returned, they are returned as string when
return_constants is set:</p>
<pre>> sqlpsolve('get_constr_type', lp)
ans =
{'LE','EQ','GE'}</pre>
<p>This for all routines until return_constants is again called with 0:</p>
<pre>> sqlpsolve('return_constants', 0);</pre>
<p>The (new) current setting of return_constants is always returned by the call.
Even when set:</p>
<pre>> sqlpsolve('return_constants', 1)
ans =
1</pre>
<p>To get the value without setting it, don't provide the second argument:</p>
<pre>> sqlpsolve('return_constants')
ans =
1</pre>
<p>In the next part of this documentation, return_constants is the default, 0, so all
constants are returned numerical and provided constants are also numerical. This
to keep the documentation as compatible as possible with older versions. But
don't let you hold that back to use string constants in your code.</p>
<a name="libraries"></a>
<h3>libraries</h3>
<p>Sysquake can execute a sequence of statements stored in diskfiles. Such files are called
"libraries". They must have the file type of ".lml" as the last part of their filename (extension).
These files are usually created using your local editor.<br>
Note that for Sysquake to be able to find these libraries that they must be put in a special directory named Lib
under the Sysquake directory or the file path where the libraries are put must be identified to Sysquake.
If you are working with the GUI version of Sysquake, this can be done via the menu option Edit, Preferences and then
'Set SQ File Path' or 'Path'. In that input multiple directories can be specified, line per line.
This path can also be seen and changed via the Sysquake path command.</p>
<p>To use a library, it must first be loaded via the following command in Sysquake:</p>
<pre>
use libraryname
</pre>
<p>With libraryname the name of the library, without its extension.
From that moment on, functions defined in it can be called directly from Sysquake.<br>
All lpsolve examples use as functionname the same name as the libraryname.</p>
<p>The sqlpsolve Sysquake distribution contains some example library files to demonstrate this.</p>
<p>To see the contents of such a file also edit these files with your
favourite text editor (or notepad).</p>
<h4>example1.lml</h4>
<p>Contains the commands as shown in the first example of this article.</p>
<h4>example2.lml</h4>
<p>Contains the commands as shown in the second example of this article.</p>
<h4>example3.lml</h4>
<p>Contains the commands of a practical example. See further in this article.</p>
<h4>example4.lml</h4>
<p>Contains the commands of a practical example. See further in this article.</p>
<h4>example5.lml</h4>
<p>Contains the commands of a practical example. See further in this article.</p>
<h4>example6.lml</h4>
<p>Contains the commands of a practical example. See further in this article.</p>
<h4>sqlpsolve.lml</h4>
<p>This library contains some higher-level functions to simplify things. To use it, first give following Sysquake command:</p>
<pre>use sqlpsolve;</pre>
<h4><u>lp_solve</u></h4>
<p>The first function herein is called lp_solve.<br>
It accepts as arguments some matrices and options to create and solve an lp model.<br>
type help lp_solve or just lp_solve to see its usage:</p>
<pre>> use sqlpsolve
> help lp_solve
LP_SOLVE Solves mixed integer linear programming problems.
SYNOPSIS: [obj,x,duals] = lp_solve(f,a,b,e,vlb,vub,xint,scalemode,keep)
solves the MILP problem
max v = f'*x
a*x <> b
vlb <= x <= vub
x(int) are integer
ARGUMENTS: The first four arguments are required:
f: n vector of coefficients for a linear objective function.
a: m by n matrix representing linear constraints.
b: m vector of right sides for the inequality constraints.
e: m vector that determines the sense of the inequalities:
e(i) = -1 ==> Less Than
e(i) = 0 ==> Equals
e(i) = 1 ==> Greater Than
vlb: n vector of lower bounds. If empty or omitted,
then the lower bounds are set to zero.
vub: n vector of upper bounds. May be omitted or empty.
xint: vector of integer variables. May be omitted or empty.
scalemode: scale flag. Off when 0 or omitted.
keep: Flag for keeping the lp problem after it's been solved.
If omitted, the lp will be deleted when solved.
OUTPUT: A nonempty output is returned if a solution is found:
obj: Optimal value of the objective function.
x: Optimal value of the decision variables.
duals: solution of the dual problem.
</pre>
<p>Example of usage. To create and solve following lp-model:</p>
<pre>max: -x1 + 2 x2;
C1: 2x1 + x2 < 5;
-4 x1 + 4 x2 <5;
int x2,x1;
</pre>
<p>The following command can be used:</p>
<pre>> use sqlpsolve
> [obj, x]=lp_solve([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])
obj =
3
x =
1
2
</pre>
<h4><u>lp_maker</u></h4>
<p>A second function in sqlpsolve.lml is lp_maker. It is analog to lp_solve and also uses the API to create the higher-level function.
This function accepts as arguments some matrices and options to create an lp model. Note that this function only
creates a model and returns a handle.<br>
type help lp_maker or just lp_maker to see its usage:</p>
<pre>> use sqlpsolve
> help lp_maker
LP_MAKER Makes mixed integer linear programming problems.
SYNOPSIS: lp_handle = lp_maker(f,a,b,e,vlb,vub,xint,scalemode,setminim)
make the MILP problem
max v = f'*x
a*x <> b
vlb <= x <= vub
x(int) are integer
ARGUMENTS: The first four arguments are required:
f: n vector of coefficients for a linear objective function.
a: m by n matrix representing linear constraints.
b: m vector of right sides for the inequality constraints.
e: m vector that determines the sense of the inequalities:
e(i) < 0 ==> Less Than
e(i) = 0 ==> Equals
e(i) > 0 ==> Greater Than
vlb: n vector of non-negative lower bounds. If empty or omitted,
then the lower bounds are set to zero.
vub: n vector of upper bounds. May be omitted or empty.
xint: vector of integer variables. May be omitted or empty.
scalemode: Autoscale flag. Off when 0 or omitted.
setminim: Set maximum lp when this flag equals 0 or omitted.
OUTPUT: lp_handle is an integer handle to the lp created.
</pre>
<p>Example of usage. To create following lp-model:</p>
<pre>max: -x1 + 2 x2;
C1: 2x1 + x2 < 5;
-4 x1 + 4 x2 <5;
int x2,x1;
</pre>
<p>The following command can be used:</p>
<pre>> use sqlpsolve
> lp=lp_maker([-1, 2], [2, 1; -4, 4], [5, 5], [-1, -1], [], [], [1, 2])
lp =
0
</pre>
<p>To solve the model and get the solution:</p>
<pre>> sqlpsolve('solve', lp)
ans =
0
> sqlpsolve('get_objective', lp)
ans =
3
> sqlpsolve('get_variables', lp)
ans =
1
2
</pre>
<p>Don't forget to free the handle and its associated memory when you are done:</p>
<pre>> sqlpsolve('delete_lp', lp);</pre>
<h4>lpdemo.lml</h4>
<p>Contains several examples to build and solve lp models.</p>
<h4>ex.lml</h4>
<p>Contains several examples to build and solve lp models.
Also solves the lp_examples from the lp_solve distribution.</p>
<a name="A_practical_example"></a>
<h3>A practical example</h3>
<p>We shall illustrate the method of linear programming by means of a simple example,
giving a combination graphical/numerical solution, and then solve both a slightly as well as a substantially
more complicated problem.</p>
<p>Suppose a farmer has 75 acres on which to plant two crops: wheat and barley.
To produce these crops, it costs the farmer (for seed, fertilizer, etc.) $120 per acre for the
wheat and $210 per acre for the barley. The farmer has $15000 available for expenses.
But after the harvest, the farmer must store the crops while awaiting favourable market conditions.
The farmer has storage space for 4000 bushels. Each acre yields an average of 110 bushels of wheat
or 30 bushels of barley. If the net profit per bushel of wheat (after all expenses have been subtracted)
is $1.30 and for barley is $2.00, how should the farmer plant the 75 acres to maximize profit?</p>
<p>We begin by formulating the problem mathematically.
First we express the objective, that is the profit, and the constraints
algebraically, then we graph them, and lastly we arrive at the solution
by graphical inspection and a minor arithmetic calculation.</p>
<p>Let x denote the number of acres allotted to wheat and y the number of acres allotted to barley.
Then the expression to be maximized, that is the profit, is clearly</p>
<p align="center">P = (110)(1.30)x + (30)(2.00)y = 143x + 60y.</p>
<p>There are three constraint inequalities, specified by the limits on expenses, storage and acreage.
They are respectively:</p>
<p align="center">
120x + 210y <= 15000<br>
110x + 30y <= 4000<br>
x + y <= 75