1. Go to this page and download the library: Download kerigard/lpsolve library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kerigard / lpsolve example snippets
use Kerigard\LPSolve\Constraint;
use Kerigard\LPSolve\Problem;
use Kerigard\LPSolve\Solver;
$problem = new Problem(
objective: [143, 60, 195],
constraints: [
new Constraint([120, 210, 150.75], LE, 15000),
new Constraint([110, 30, 125], LE, 4000),
new Constraint([1, 1, 1], LE, 75),
]
);
$solver = new Solver(Solver::MAX);
$solution = $solver->solve($problem);
var_dump($solution->getStatus()); // OPTIMAL solution
var_dump($solution->getCount()); // 1
var_dump($solution->getIterations()); // 2
if ($solution->getCode() === OPTIMAL) {
var_dump($solution->getObjective()); // 6986.842105...
var_dump($solution->getVariables()); // [0, 56.578947..., 18.421052...]
}
$problem = new Problem(
objective: [-1, -2, 0.1, 3],
constraints: [
new Constraint([1, 1, 0, 0], LE, 5),
new Constraint([2, -1, 0, 0], GE, 0),
new Constraint([-1, 3, 0, 0], GE, 0),
new Constraint([0, 0, 1, 1], GE, 0.5),
],
lowerBounds: [0, 0, 1.1, 0],
upperBounds: []
);
$solver = new Solver(Solver::MIN);
$solution = $solver->setScaling(SCALE_MEAN | SCALE_INTEGERS)->solve($problem);
var_dump($solution->getObjective()); // -8.223333...
$problem = new Problem(
objective: [-1, -2, 0.1, 3],
constraints: [
new Constraint([1, 1, 0, 0], LE, 5),
new Constraint([2, -1, 0, 0], GE, 0),
new Constraint([-1, 3, 0, 0], GE, 0),
new Constraint([0, 0, 1, 1], GE, 0.5),
],
integerVariables: [0, 0, 1, 0], // integer variables (only variable #3 is integer)
binaryVariables: [1, 0, 0, 1] // binary variables (variables #1 and #4 are binary)
);
$solver = new Solver(Solver::MIN);
$solution = $solver->setVerbose(DETAILED)->solve($problem);
var_dump($solution->getVariables()); // [1, 2, 1, 0]