forked from myfarms/php_lp_solve
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPHP.htm
3475 lines (3105 loc) · 125 KB
/
PHP.htm
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 PHP</TITLE>
<style TYPE="text/css"> BODY { font-family:verdana,arial,helvetica; margin:15; }
</style>
</HEAD>
<BODY>
<h1 align="left"><u>Using lpsolve from PHP</u></h1>
<a name="PHP"></a>
<h3>PHP?</h3>
<p>
PHP is a general-purpose scripting language that is especially suited for web development.
PHP generally runs on a web server, taking PHP code as its input and creating web pages as output.
It can also be used for command-line scripting and client-side GUI applications.
PHP can be deployed on most web servers, many operating systems and platforms, and can be used with many relational database management systems.
It is available free of charge, and the PHP Group provides the complete source code for users to build, customize and extend for their own use.
PHP primarily acts as a filter, taking input from a file or stream containing text and/or PHP instructions and outputs another stream of data; most commonly the output will be HTML.
It can automatically detect the language of the user.
From PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor.
Originally designed to create dynamic web pages, PHP's principal focus is server-side scripting, and it is similar to other server-side scripting languages that provide dynamic content from a web server to a client, such as Microsoft's ASP.NET system, Sun Microsystems' JavaServer Pages, and mod_perl.
PHP has also attracted the development of many frameworks that provide building blocks and a design structure to promote rapid application development (RAD).
Some of these include CakePHP, PRADO, Symfony and Zend Framework, offering features similar to other web application frameworks.
</p>
<p>We will not discuss the specifics of PHP here but instead refer the reader to the
<a href="http://www.php.net/">PHP</a> website.<br>
An introduction to PHP can be found at <a href="http://www.w3schools.com/php/default.asp">w3schools</a>
</p>
<a name="PHP_and_lpsolve"></a>
<h3>PHP and lpsolve</h3>
<p>lpsolve is callable from PHP via an extension or module. As such, it looks like lpsolve is fully integrated
with PHP. Matrices can directly be transferred between PHP 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
PHP (especially for matrix support). So you have full control to the complete lpsolve functionality via the lpsolve
PHP driver.
If you find that this involves too much work to solve an lp model then you can also work via higher-level
script files that can make things a lot easier. See further in this article.
<p>
<a name="Installation"></a>
<h3>Installation</h3>
<p>To make this possible, a driver program is needed: php_phplpsolve55.dll (windows) or phplpsolve55.so (Unix/Linux).<br>
Secondly in the file php.ini, which is a PHP configuration file, the location of the driver must be specified.
The location of this ini file depends on the environment.<br>
Under windows it is commonly \Program Files\php\php.ini<br>
Under Unix/Linux it was found under /etc/php5/cli and /etc/php5/apache2<br>
In that file there is an item extension_dir=. The driver program must be put in the directory specified by that item.<br>
Then an extra entry 'extension' must be added.<br>
Under windows it must be: extension=php_phplpsolve55.dll<br>
Under Unix/Linux it must be: extension=phplpsolve55.so
</p>
<p>Note that there is an alternative way but it is not always working, especially as webservice.
In the php.ini file, specify enable_dl=on<br>
Then in the PHP code, use the following command to load the lpsolve driver: dl('lpsolve.so');<br>
However, this was not tested so use it at own risk.
</p>
<p>To take these changes in effect, the webservice has to be restarted.<br>
Under windows, this is done by restarting the service.<br>
Under Unix/Linux it depends on the system. For example in Ubuntu the command is: sudo /etc/init.d/apache2 restart<br>
This must be done with root privileges: sudo su -
</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.0.15_dev.zip/lp_solve_5.5.0.15_dev.tar.gz). This has the advantage that the lpsolve 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 PHP lpsolve driver that is called (php_)phplpsolve55 and the lpsolve library that implements the
API that is called lpsolve55.</p>
<a name="Testing_the_installation"></a>
<h3>Testing the installation</h3>
<p>Note. In the following text PHP commands are given. They are just provided as is.
However in a real PHP application, all PHP commands must be between <?php and ?>
</p>
<p>Note. Some of these commands return new lines to continue on the next line.
This is fine under CLI (php executed in a command line), but when PHP is used in a web environment and shown in
html, then these newlines are by default just ignored by html.
This gives an output that is not always that readable.
Therefore you can put everything between <pre> </pre><br>
This combined with the note from above, put the commands in following block:
</p>
<pre>
<?php
echo "<pre>";
// your php commands
echo "</pre>";
?>
</pre>
<p>To test if everything is installed correctly, execute the following statement in PHP.</p>
<pre>lpsolve();
</pre>
<p>Note. As stated above, these commands must be between <?php and ?> and when used in html between a pre block as shown below:<p>
<pre>
<?php
echo "<pre>";
lpsolve();
echo "</pre>";
?>
</pre>
<p>Keep this in mind. This will not be repeated in the following text.</p>
<p>If it gives the following, then everything is ok:</p>
<pre>lpsolve PHP Interface version 5.5.0.6
using lpsolve version 5.5.0.15
Usage: ret = lpsolve("functionname", arg1, arg2, ...)
</pre>
<p>If you get the following:</p>
Windows:
<pre>
PHP Warning: PHP Startup: Unable to load dynamic library 'F:\php-5.2.6\Release_TS\php_phplpsolve55.dll' - The specified module could not be found. in Unknown on line 0
PHP Fatal error: Call to undefined function lpsolve() in Command line code on line 1
</pre>
Possible also with a messagebox saying:
<pre>
---------------------------
php.exe - Unable To Locate Component
---------------------------
This application has failed to start because lpsolve55.dll was not found. Re-installing the application may fix this problem.
</pre>
or
Unix/Linux:
<pre>
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20060613+lfs/phplpsolve55.so' - liblpsolve55.so: cannot open shared object file: No such file or directory in Unknown on line 0
Fatal error: Call to undefined function lpsolve() in Command line code on line 1
</pre>
<p>
Note that the PHP Warning is not always shown. The Fatal error is.
This was specifically noted in the web environment.
</p>
<p>Then PHP can find the lpsolve driver program, but the driver program cannot find the lpsolve library
that contains the lpsolve implementation.
This library is called lpsolve55.dll under Windows and liblpsolve55.so under Unix/Linux.<br>
Under Windows, the lpsolve55.dll file must be in a directory that in the PATH environment variable.
This path can be shown via the following command in a command prompt: PATH<br>
It is common to place this in the WINDOWS\system32 folder.<br>
<br>
Under Unix/Linux, the liblpsolve55.so shared library must be either in the directories /lib or /usr/lib or in
a directory specified by the LD_LIBRARY_PATH environment variable.
</p>
<p>Note that in a web environment the webserver may need to be restarted after
making changes in the configuration. For example on Ubuntu this is done by the
following command:
</p>
<pre>
sudo /etc/init.d/apache2 restart
</pre>
<p>Another way to check if the lpsolve extension is available in PHP is by
entering the following command:
</p>
<pre>
print_r(get_extension_funcs("lpsolve"));
</pre>
<p>This must return:</p>
<pre>
Array
(
[0] => lpsolve
)
</pre>
<p>To return the version of lpsolve, the following PHP command can be executed:</p>
<pre>
echo phpversion("lpsolve");
</pre>
<p>This must return:</p>
<pre>5.5.0.6</pre>
<p>Note that this is the version of the PHP driver, not the version of lpsolve itself.</p>
<a name="Solve_an_lp_model_from_PHP_via_lpsolve"></a>
<h3>Solve an lp model from PHP via lpsolve</h3>
<p>To call an lpsolve function, the following syntax must be used:</p>
<pre>ret = lpsolve('functionname', arg1, arg2, ...);</pre>
<p>The return value is optional and depend on the function called. Sometimes it is a single value, sometimes a vector and sometimes a vector of vector. functionname must always be enclosed between single or double
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 PHP specific functions.
Most of the lpsolve API routines use or return an lprec structure. To make things more robust in PHP, 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 PHP 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 PHP, nothing must
be dimensioned in advance. The lpsolve driver takes care of dimensioning all return variables and they are
always returned as return value of the call to lpsolve. Never as argument to the routine. This can be a single
value as for get_objective 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>
<p>Note that you can get a usage of lpsolve, its arguments and the constants that it defines by entering the following in PHP:</p>
<pre>
lpsolve();
$a=get_defined_constants(true); print_r($a[lpsolve]);
</pre>
<p>This will give:</p>
<pre>
lpsolve PHP Interface version 5.5.0.6
using lpsolve version 5.5.0.15
Usage: ret = lpsolve("functionname", arg1, arg2, ...)
Array
(
[LE] => 1
[EQ] => 3
[GE] => 2
[FR] => 0
[SCALE_NONE] => 0
[SCALE_EXTREME] => 1
[SCALE_RANGE] => 2
[SCALE_MEAN] => 3
[SCALE_GEOMETRIC] => 4
[SCALE_CURTISREID] => 7
[SCALE_QUADRATIC] => 8
[SCALE_LOGARITHMIC] => 16
[SCALE_USERWEIGHT] => 31
[SCALE_POWER2] => 32
[SCALE_EQUILIBRATE] => 64
[SCALE_INTEGERS] => 128
[SCALE_DYNUPDATE] => 256
[SCALE_ROWSONLY] => 512
[SCALE_COLSONLY] => 1024
[IMPROVE_NONE] => 0
[IMPROVE_SOLUTION] => 1
[IMPROVE_DUALFEAS] => 2
[IMPROVE_THETAGAP] => 4
[IMPROVE_BBSIMPLEX] => 8
[PRICER_FIRSTINDEX] => 0
[PRICER_DANTZIG] => 1
[PRICER_DEVEX] => 2
[PRICER_STEEPESTEDGE] => 3
[PRICE_PRIMALFALLBACK] => 4
[PRICE_MULTIPLE] => 8
[PRICE_PARTIAL] => 16
[PRICE_ADAPTIVE] => 32
[PRICE_RANDOMIZE] => 128
[PRICE_AUTOPARTIAL] => 256
[PRICE_LOOPLEFT] => 1024
[PRICE_LOOPALTERNATE] => 2048
[PRICE_HARRISTWOPASS] => 4096
[PRICE_TRUENORMINIT] => 16384
[PRESOLVE_NONE] => 0
[PRESOLVE_ROWS] => 1
[PRESOLVE_COLS] => 2
[PRESOLVE_LINDEP] => 4
[PRESOLVE_SOS] => 32
[PRESOLVE_REDUCEMIP] => 64
[PRESOLVE_KNAPSACK] => 128
[PRESOLVE_ELIMEQ2] => 256
[PRESOLVE_IMPLIEDFREE] => 512
[PRESOLVE_REDUCEGCD] => 1024
[PRESOLVE_PROBEFIX] => 2048
[PRESOLVE_PROBEREDUCE] => 4096
[PRESOLVE_ROWDOMINATE] => 8192
[PRESOLVE_COLDOMINATE] => 16384
[PRESOLVE_MERGEROWS] => 32768
[PRESOLVE_IMPLIEDSLK] => 65536
[PRESOLVE_COLFIXDUAL] => 131072
[PRESOLVE_BOUNDS] => 262144
[PRESOLVE_DUALS] => 524288
[PRESOLVE_SENSDUALS] => 1048576
[ANTIDEGEN_NONE] => 0
[ANTIDEGEN_FIXEDVARS] => 1
[ANTIDEGEN_COLUMNCHECK] => 2
[ANTIDEGEN_STALLING] => 4
[ANTIDEGEN_NUMFAILURE] => 8
[ANTIDEGEN_LOSTFEAS] => 16
[ANTIDEGEN_INFEASIBLE] => 32
[ANTIDEGEN_DYNAMIC] => 64
[ANTIDEGEN_DURINGBB] => 128
[ANTIDEGEN_RHSPERTURB] => 256
[ANTIDEGEN_BOUNDFLIP] => 512
[CRASH_NONE] => 0
[CRASH_MOSTFEASIBLE] => 2
[CRASH_LEASTDEGENERATE] => 3
[SIMPLEX_PRIMAL_PRIMAL] => 5
[SIMPLEX_DUAL_PRIMAL] => 6
[SIMPLEX_PRIMAL_DUAL] => 9
[SIMPLEX_DUAL_DUAL] => 10
[NODE_FIRSTSELECT] => 0
[NODE_GAPSELECT] => 1
[NODE_RANGESELECT] => 2
[NODE_FRACTIONSELECT] => 3
[NODE_PSEUDOCOSTSELECT] => 4
[NODE_PSEUDONONINTSELECT] => 5
[NODE_PSEUDORATIOSELECT] => 6
[NODE_USERSELECT] => 7
[NODE_WEIGHTREVERSEMODE] => 8
[NODE_BRANCHREVERSEMODE] => 16
[NODE_GREEDYMODE] => 32
[NODE_PSEUDOCOSTMODE] => 64
[NODE_DEPTHFIRSTMODE] => 128
[NODE_RANDOMIZEMODE] => 256
[NODE_GUBMODE] => 512
[NODE_DYNAMICMODE] => 1024
[NODE_RESTARTMODE] => 2048
[NODE_BREADTHFIRSTMODE] => 4096
[NODE_AUTOORDER] => 8192
[NODE_RCOSTFIXING] => 16384
[NODE_STRONGINIT] => 32768
[NOMEMORY] => -2
[OPTIMAL] => 0
[SUBOPTIMAL] => 1
[INFEASIBLE] => 2
[UNBOUNDED] => 3
[DEGENERATE] => 4
[NUMFAILURE] => 5
[USERABORT] => 6
[TIMEOUT] => 7
[PRESOLVED] => 9
[PROCFAIL] => 10
[PROCBREAK] => 11
[FEASFOUND] => 12
[NOFEASFOUND] => 13
[BRANCH_CEILING] => 0
[BRANCH_FLOOR] => 1
[BRANCH_AUTOMATIC] => 2
[BRANCH_DEFAULT] => 3
[MSG_PRESOLVE] => 1
[MSG_LPFEASIBLE] => 8
[MSG_LPOPTIMAL] => 16
[MSG_MILPEQUAL] => 256
[MSG_MILPFEASIBLE] => 128
[MSG_MILPBETTER] => 512
[NEUTRAL] => 0
[CRITICAL] => 1
[SEVERE] => 2
[IMPORTANT] => 3
[NORMAL] => 4
[DETAILED] => 5
[FULL] => 6
[Infinite] => 1.0E+30
)
</pre>
<p>The array shows all constants defined by the lpsolve driver and are all
the constants of the lpsolve API. That way one can enter these symbols instead
of their numerical values.
</p>
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 executing script example1.php)</p>
<pre>
$lp = lpsolve('make_lp', 0, 4);
lpsolve('set_verbose', $lp, IMPORTANT);
$ret = lpsolve('set_obj_fn', $lp, Array(1, 3, 6.24, 0.1));
$ret = lpsolve('add_constraint', $lp, Array(0, 78.26, 0, 2.9), GE, 92.3);
$ret = lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), LE, 14.8);
$ret = lpsolve('add_constraint', $lp, Array(12.68, 0, 0.08, 0.9), GE, 4);
$ret = lpsolve('set_lowbo', $lp, 1, 28.6);
$ret = lpsolve('set_lowbo', $lp, 4, 18);
$ret = lpsolve('set_upbo', $lp, 4, 48.98);
$ret = lpsolve('set_col_name', $lp, 1, 'COLONE');
$ret = lpsolve('set_col_name', $lp, 2, 'COLTWO');
$ret = lpsolve('set_col_name', $lp, 3, 'COLTHREE');
$ret = lpsolve('set_col_name', $lp, 4, 'COLFOUR');
$ret = lpsolve('set_row_name', $lp, 1, 'THISROW');
$ret = lpsolve('set_row_name', $lp, 2, 'THATROW');
$ret = lpsolve('set_row_name', $lp, 3, 'LASTROW');
$ret = lpsolve('write_lp', $lp, 'a.lp');
print lpsolve('get_mat', $lp, 1, 2) . "\n";
print lpsolve('solve', $lp) . "\n";
print lpsolve('get_objective', $lp) . "\n";
print_r(lpsolve('get_variables', $lp));
print_r(lpsolve('get_constraints', $lp));
lpsolve('delete_lp', $lp);
</pre>
<p>This gives as output:
</p>
<pre>
78.26
0
31.7827586207
Array
(
[0] => Array
(
[0] => 28.6
[1] => 0
[2] => 0
[3] => 31.8275862069
)
[1] => 1
)
Array
(
[0] => Array
(
[0] => 92.3
[1] => 6.864
[2] => 391.292827586
)
[1] => 1
)
</pre>
<p>Note that get_variables and get_constraints return two results: The result vector and a status. If only the
vector is needed, then variable indexing must be used. For example:</p>
<pre>
$ret = lpsolve('get_variables', $lp);
$x = $ret[0];
$ret = $ret[1];
print_r($x);
print $ret . "\n";
</pre>
<p>Variable x will contain the result vector and ret the return status of the call:</p>
<pre>
Array
(
[0] => 28.6
[1] => 0
[2] => 0
[3] => 31.8275862069
)
1
</pre>
<p>Don't forget to free the handle and its associated memory when you are done:</p>
<pre>lpsolve('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>
$lp = lpsolve('make_lp', 0, 4);
$ret = lpsolve('set_lp_name', $lp, 'mymodel');
lpsolve('set_verbose', 'mymodel', IMPORTANT);
$ret = lpsolve('set_obj_fn', 'mymodel', Array(1, 3, 6.24, 0.1));
$ret = lpsolve('add_constraint', 'mymodel', Array(0, 78.26, 0, 2.9), GE, 92.3);
$ret = lpsolve('add_constraint', 'mymodel', Array(0.24, 0, 11.31, 0), LE, 14.8);
$ret = lpsolve('add_constraint', 'mymodel', Array(12.68, 0, 0.08, 0.9), GE, 4);
$ret = lpsolve('set_lowbo', 'mymodel', 1, 28.6);
$ret = lpsolve('set_lowbo', 'mymodel', 4, 18);
$ret = lpsolve('set_upbo', 'mymodel', 4, 48.98);
$ret = lpsolve('set_col_name', 'mymodel', 1, 'COLONE');
$ret = lpsolve('set_col_name', 'mymodel', 2, 'COLTWO');
$ret = lpsolve('set_col_name', 'mymodel', 3, 'COLTHREE');
$ret = lpsolve('set_col_name', 'mymodel', 4, 'COLFOUR');
$ret = lpsolve('set_row_name', 'mymodel', 1, 'THISROW');
$ret = lpsolve('set_row_name', 'mymodel', 2, 'THATROW');
$ret = lpsolve('set_row_name', 'mymodel', 3, 'LASTROW');
$ret = lpsolve('write_lp', 'mymodel', 'a.lp');
print lpsolve('get_mat', 'mymodel', 1, 2) . "\n";
print lpsolve('solve', 'mymodel') . "\n";
print lpsolve('get_objective', 'mymodel') . "\n";
print_r(lpsolve('get_variables', 'mymodel'));
print_r(lpsolve('get_constraints', 'mymodel'));
lpsolve('delete_lp', 'mymodel');
</pre>
<p>This gives:</p>
<pre>
78.26
0
31.7827586207
Array
(
[0] => Array
(
[0] => 28.6
[1] => 0
[2] => 0
[3] => 31.8275862069
)
[1] => 1
)
Array
(
[0] => Array
(
[0] => 92.3
[1] => 6.864
[2] => 391.292827586
)
[1] => 1
)
</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 PHP routine to get the handle from the model name: <a href="#get_handle">get_handle</a>.<br>
For example:</p>
<pre>
$lp = lpsolve('get_handle', 'mymodel');
print $lp;
</pre>
<p>This gives:</p>
<pre>
0
</pre>
<p>Don't forget to free the handle and its associated memory when you are done:</p>
<pre>lpsolve('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>
lpsolve uses PHP arrays to represent matrices (and vectors).<br>
For example:
<pre>lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), 1, 14.8);</pre>
<p>Most of the time, variables are used to provide the data:</p>
<pre>lpsolve('add_constraint', $lp, $a1, 1, 14.8);</pre>
<p>Where $a1 is a variable of type array. Sometimes a two-dimensional matrix is used.
In PHP, that is an array of arrays:</p>
<pre>lpsolve('set_mat', $lp, Array(Array(1, 2, 3), Array(4, 5, 6)));</pre>
<p>Array(1, 2, 3) is the first row and Array(4, 5, 6) is the second row.</p>
<p>PHP also supports sparse matrices because of the way arrays are internally supported.<br>
For example:
</p>
<pre>
$a[2] = 3;
$a[5] = 5;
print_r($a);
</pre>
<p>This gives:</p>
<pre>
Array
(
[2] => 3
[5] => 5
)
</pre>
<p>The lpsolve driver accepts these as is. No conversion or so is needed. The non-provided
elements are seen as zero-values.
</p>
<p>In fact, the lpsolve driver sees all provided matrices as sparse matrices. lpsolve uses sparse matrices
internally and data can be provided sparse via the ex routines. For example add_constraintex. The lpsolve
driver always uses the ex routines to provide the data to lpsolve. Even if you call from PHP the routine
names that would require a dense matrix (for example add_constraint), the lpsolve driver will always call the
sparse version of the routine (for example add_constraintex). This results in the most performing behaviour.</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 PHP 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 PHP matrix.</p>
<a name="Maximum_usage_of_matrices_with_lpsolve"></a>
<h3>Maximum usage of matrices with lpsolve</h3>
<p>Because PHP has the array possibility to represent vectors, all lpsolve API routines that need a column or row number to get/set information for that
column/row are extended in the lpsolve PHP driver to also work with vectors. 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 lpsolve PHP driver however also allows specifying a vector to set the integer
status of all variables at once. The API call is: $return = lpsolve('set_int', $lp, $column, $must_be_int);. The
matrix version of this call is: $return = lpsolve('set_int', $lp, $must_be_int);.
Here $must_be_int must be an array variable.
The API call to return the integer status of a variable is: $return = lpsolve('is_int', $lp, $column);. The
matrix version of this call is: $is_int = lpsolve('is_int', $lp);<br>
$is_int is again an array variable in this case.
Also note the get_mat and set_mat routines. In PHP 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 executing script example2.php)
</p>
<pre>
$lp = lpsolve('make_lp', 0, 4);
lpsolve('set_verbose', $lp, IMPORTANT);
$ret = lpsolve('set_obj_fn', $lp, Array(1, 3, 6.24, 0.1));
$ret = lpsolve('add_constraint', $lp, Array(0, 78.26, 0, 2.9), GE, 92.3);
$ret = lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), LE, 14.8);
$ret = lpsolve('add_constraint', $lp, Array(12.68, 0, 0.08, 0.9), GE, 4);
$ret = lpsolve('set_lowbo', $lp, Array(28.6, 0, 0, 18));
$ret = lpsolve('set_upbo', $lp, Array(Infinite, Infinite, Infinite, 48.98));
$ret = lpsolve('set_col_name', $lp, Array('COLONE', 'COLTWO', 'COLTHREE', 'COLFOUR'));
$ret = lpsolve('set_row_name', $lp, Array('THISROW', 'THATROW', 'LASTROW'));
$ret = lpsolve('write_lp', $lp, 'a.lp');
print_r(lpsolve('get_mat', $lp));
print lpsolve('solve', $lp) . "\n";
print lpsolve('get_objective', $lp) . "\n";
print_r(lpsolve('get_variables', $lp));
print_r(lpsolve('get_constraints', $lp));
lpsolve('delete_lp', $lp);
</pre>
<p>This gives:</p>
<pre>
Array
(
[0] => Array
(
[0] => Array
(
[0] => 0
[1] => 78.26
[2] => 0
[3] => 2.9
)
[1] => Array
(
[0] => 0.24
[1] => 0
[2] => 11.31
[3] => 0
)
[2] => Array
(
[0] => 12.68
[1] => 0
[2] => 0.08
[3] => 0.9
)
)
[1] => 1
)
0
31.7827586207
Array
(
[0] => Array
(
[0] => 28.6
[1] => 0
[2] => 0
[3] => 31.8275862069
)
[1] => 1
)
Array
(
[0] => Array
(
[0] => 92.3
[1] => 6.864
[2] => 391.292827586
)
[1] => 1
)
</pre>
<p>Note the usage of Infinite in set_upbo. This stands for 'infinity'. Meaning an infinite upper bound.
It is also possible to use -Infinite to express minus infinity. This can for example be used to create a free variable.
Infinite is a constant defined by the lpsolve library.</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. Note that PHP doesn't support much matrix calculations on arrays.
We only need a matrix multiplication routine to demonstrate the following.
For this the following routine can be used. Include it in the next code to
perform the matrixmultiply:
</p>
<pre>
function matrixmultiply($Array1, $Array2) {
$rows2 = count($Array2);
if (is_array($Array2[0])) {
$dim2 = 2;
$columns2 = count($Array2[0]);
}
else {
$dim2 = 1;
$columns2 = 1;
}
$rows1 = count($Array1);
if (is_array($Array1[0])) {
$dim1 = 2;
$columns1 = count($Array1[0]);
}
else {
$dim1 = 1;
if ($rows2 == 1)
$columns1 = 1;
else {
$columns1 = $rows1;
$rows1 = 1;
}
}
for($i=0; $i<$rows1; $i++){
for($j=0; $j<$columns2; $j++){
$a = 0;
for($M=0;$M<$columns1;$M++){
if ($dim1 == 2)
$b = $Array1[$i][$M];
else if ($rows2 == 1)
$b = $Array1[$i];
else
$b = $Array1[$M];
$c = $Array2[$M];
if ($dim2 == 2)
$c = $c[$j];
$a = $a + $b * $c;
}
if ($dim2 == 2)
$ArrayMultipli[$i][$j] = $a;
else
$ArrayMultipli[$i] = $a;
}
}
return $ArrayMultipli;
}
</pre>
<p>Now do the following calculations:</p>
<pre>
$lp = lpsolve('make_lp', 0, 4);
lpsolve('set_verbose', $lp, IMPORTANT);
$ret = lpsolve('set_obj_fn', $lp, Array(1, 3, 6.24, 0.1));
$ret = lpsolve('add_constraint', $lp, Array(0, 78.26, 0, 2.9), GE, 92.3);
$ret = lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), LE, 14.8);
$ret = lpsolve('add_constraint', $lp, Array(12.68, 0, 0.08, 0.9), GE, 4);
$ret = lpsolve('set_lowbo', $lp, Array(28.6, 0, 0, 18));
$ret = lpsolve('set_upbo', $lp, Array(Infinite, Infinite, Infinite, 48.98));
$ret = lpsolve('set_col_name', $lp, Array('COLONE', 'COLTWO', 'COLTHREE', 'COLFOUR'));
$ret = lpsolve('set_row_name', $lp, Array('THISROW', 'THATROW', 'LASTROW'));
$ret = lpsolve('write_lp', $lp, 'a.lp');
lpsolve('solve', $lp);
$A = lpsolve('get_mat', $lp);
$A = $A[0];
print_r($A);
$X = lpsolve('get_variables', $lp);
$X = $X[0];
print_r($X);
$B = matrixmultiply($A, $X);
print_r($B);
$C = lpsolve('get_obj_fn', $lp);
$C = $C[0];
print_r($C);
$X = lpsolve('get_variables', $lp);
$X = $X[0];
$obj = matrixmultiply($C, $X);
print_r($obj);
</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.<br>
This gives:
</p>
<pre>
Array
(
[0] => Array
(
[0] => 0
[1] => 78.26
[2] => 0
[3] => 2.9
)
[1] => Array
(
[0] => 0.24
[1] => 0
[2] => 11.31
[3] => 0
)
[2] => Array
(
[0] => 12.68
[1] => 0
[2] => 0.08
[3] => 0.9
)
)
Array
(
[0] => 28.6
[1] => 0
[2] => 0
[3] => 31.8275862069
)
Array
(
[0] => 92.3
[1] => 6.864
[2] => 391.292827586
)
Array
(
[0] => 1
[1] => 3
[2] => 6.24
[3] => 0.0:
)
Array
(
[0] => 31.7827586207
)
</pre>
<p>Now take a look at the values of the constraints that lpsolve has found:</p>
<pre>
print_r(lpsolve('get_constraints', $lp));
</pre>
<p>That gives:</p>
<pre>
Array
(
[0] => Array
(
[0] => 92.3
[1] => 6.864
[2] => 391.292827586
)
[1] => 1
)
</pre>
<p>Exactly the same as the calculated B vector, as expected.</p>
<p>Also the value of the objective is calculated in $obj.
What we have done 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>
print lpsolve('get_objective', $lp);
</pre>
<p>That gives:</p>
<pre>
31.7827586207
</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.0.15 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>$lp=lpsolve('make_lp', 0, 4);
lpsolve('set_verbose', $lp, IMPORTANT);
lpsolve('add_constraint', $lp, Array(0, 78.26, 0, 2.9), GE, 92.3);
lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), LE, 14.8);
lpsolve('add_constraint', $lp, Array(12.68, 0, 0.08, 0.9), 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 PHP as is
done in the lpsolve driver 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=lpsolve('make_lp', 0, 4);
lpsolve('set_verbose', $lp, 'IMPORTANT');
lpsolve('add_constraint', $lp, Array(0, 78.26, 0, 2.9), 'GE', 92.3);
lpsolve('add_constraint', $lp, Array(0.24, 0, 11.31, 0), 'LE', 14.8);
lpsolve('add_constraint', $lp, Array(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>lpsolve('set_verbose', $lp, 'blabla');
PHP Fatal error: lpsolve() [<a href='function.lpsolve'>function.lpsolve</a>]:
BLABLA: Unknown.
lpsolve('set_verbose', $lp, 'GE');
PHP Fatal error: lpsolve() [<a href='function.lpsolve'>function.lpsolve</a>]:
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>lpsolve('set_constr_type', $lp, Array('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>lpsolve('set_scaling', $lp, 3+128);</pre>
<p>With the string version of constants this can be done as following:</p>
<pre>lpsolve('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>lpsolve('set_scaling', $lp, 'SCALE_MEAN|SCALE_RANGE');
PHP Fatal error: lpsolve() [<a href='function.lpsolve'>function.lpsolve</a>]:
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>