PHPackages                             kerigard/lpsolve - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kerigard/lpsolve

ActiveLibrary

kerigard/lpsolve
================

LPSolve extension as simple PHP library

v1.1.3(5mo ago)210.7k↓30.3%MITPHPPHP &gt;=5.6CI passing

Since Mar 13Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/Kerigard/lpsolve)[ Packagist](https://packagist.org/packages/kerigard/lpsolve)[ Docs](https://github.com/kerigard/lpsolve)[ RSS](/packages/kerigard-lpsolve/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

LPSolve
=======

[](#lpsolve)

 [![Build Status](https://github.com/Kerigard/lpsolve/workflows/tests/badge.svg)](https://github.com/Kerigard/lpsolve/actions) [![Total Downloads](https://camo.githubusercontent.com/fb320883db7ca0293c1be47650125dfc35f4a43cef5545a578acbb71d7262569/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4b657269676172642f6c70736f6c7665)](https://packagist.org/packages/Kerigard/lpsolve) [![Latest Stable Version](https://camo.githubusercontent.com/21f651408c62f853850d4bf46fc48eebfc075402ce338ab73ab50211788c038b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4b657269676172642f6c70736f6c7665)](https://packagist.org/packages/Kerigard/lpsolve) [![License](https://camo.githubusercontent.com/297330710705c84ed9a73c01aaac1ca05ea3b6c4b3c5db53b67308498e96cdc0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f4b657269676172642f6c70736f6c7665)](https://packagist.org/packages/Kerigard/lpsolve)

PHP wrapper around [LPSolve 5.5](https://lpsolve.sourceforge.net), providing a safe and convenient interface for building and solving linear optimization problems.

Installation
------------

[](#installation)

Install via Composer

```
composer require kerigard/lpsolve
```

Load the Composer autoloader

```
require 'vendor/autoload.php'
```

Important

This library requires the lp\_solve PHP extension.

Note

The official lp\_solve extension only supports PHP 5, so it needs to be compiled manually for modern PHP versions.

Building the lp\_solve extension
--------------------------------

[](#building-the-lp_solve-extension)

*(for PHP 5.6 - 8.x)*

The repository [Kerigard/lp-solve-php-docker](https://github.com/Kerigard/lp-solve-php-docker) contains maintained source directories for building the lp\_solve extension for different PHP versions:

BranchPHP version`5.x`PHP 5.x`7.x`PHP 7.x`8.x`PHP 8.x### 1. Install lp-solve system package

[](#1-install-lp-solve-system-package)

```
apt update && apt install -y lp-solve
```

### 2. Download source code

[](#2-download-source-code)

```
curl -fsSL https://github.com/Kerigard/lp-solve-php-docker/archive/8.x.tar.gz \
    | tar -xz -C /tmp --strip-components=1
```

Replace `8.x` with the desired branch.

### 3. Build and install

[](#3-build-and-install)

```
(cd /tmp/lp-solve/extra/PHP && phpize && ./configure && make && make install)
rm -r /tmp/lp-solve
```

### 4. Enable the extension

[](#4-enable-the-extension)

Add to your `php.ini`:

```
extension=phplpsolve55.so

```

Usage
-----

[](#usage)

### Maximization with constraints

[](#maximization-with-constraints)

```
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...]
}
```

### Minimization with bounds

[](#minimization-with-bounds)

```
$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...
```

### Integer and Binary Variables

[](#integer-and-binary-variables)

```
$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]
```

If you pass `true` instead of an array, all variables will become integer or binary.

### Error handling

[](#error-handling)

Invalid problem definition

```
$problem = new Problem(
    [1],
    [new Constraint([0, 78.26, 0, 2.9], GE, 92.3)]
);

try {
    $solver = new Solver();
    $solver->setTimeout(0)->solve($problem);
} catch (\LPSolveException $e) {
    var_dump($e->getMessage()); // Invalid vector
}
```

Throwing exceptions for non-optimal solutions

```
$problem = new Problem(
    [10, 10],
    [
        Constraint::fromString('1x + 1y = 20'),
        Constraint::fromString('0x + 1y
