Skip to content

Commit

Permalink
README: included example.php & improved instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Mar 12, 2023
1 parent fcd7eed commit 5e62948
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ci: phplint tester ## Runs complete CI suite
.PHONY: phplint
phplint: install
@echo '> PHP linter ...'
@php vendor/bin/parallel-lint Simplex/ tests/ example.php --colors
@php vendor/bin/parallel-lint Simplex/ tests/ --colors
@echo ''

.PHONY: tester
Expand Down
101 changes: 91 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
Simplex Calculator
==================
# Simplex Calculator

Live demo: http://simplex.tode.cz
## About

This is a simple PHP tool for linear programming problems solving using dual simplex algorithm.
PHP library for solving linear programming problems using dual simplex algorithm.

It can detect none, one and only and more optimal solutions to any linear problem you tell it to solve.
**NOTE:** Basis cycling detection is not implemented.

However, the only thing that has been left unimplemented is basis cycling detection. Feel free to post pull requests ;-)

Enjoy.
## Installation

The recommended way is to use [Composer](https://getcomposer.org/):

Conventions
-----------
```bash
composer require uestla/simplex-calculator
```

Please name your variables as **x<n>** where *<n>* is a natural number (see [example.php](https://github.com/uestla/Simplex-Calculator/blob/master/example.php) for details).
You can also download the latest [release](https://github.com/uestla/Simplex-Calculator/tags) as a ZIP file.


## Conventions

Please name all your input variables as `x<n>` where `<n>` is a natural number (see [example below](#defining-and-solving-simplex-tasks) for details).


## Usage

### Loading library

```php
// using Composer
require_once __DIR__ . '/vendor/autoload.php';

// using manual download
require_once __DIR__ . '/Simplex/simplex.php';
```

### Defining and solving simplex tasks

```php
// define task: Maximize x1 + 2x2
$task = new Simplex\Task(new Simplex\Func(array(
'x1' => 1,
'x2' => 2,
)));

// add constraints

// 3x1 + 2x2 <= 24
$task->addRestriction(new Simplex\Restriction(array(
'x1' => 3,
'x2' => 2,

), Simplex\Restriction::TYPE_LOE, 24));

// -2x1 - 4x2 >= -32
$task->addRestriction(new Simplex\Restriction(array(
'x1' => -2,
'x2' => -4,

), Simplex\Restriction::TYPE_GOE, -32));

// create solver
$solver = new Simplex\Solver($task);

// get solutions
$solution = $solver->getSolution(); // array('x1' => 0, 'x2' => 8, 'x3' => 8, 'x4' => 0)
$alternativeSolutions = $solver->getAlternativeSolutions(); // array(array('x1' => 4, 'x2' => 6, 'x3' => 0, 'x4' => 0))

// get optimal value
$optimum = $solver->getSolutionValue($solution); // 16

// print solutions
$printer = new Simplex\Printer;
$printer->printSolution($solver);

// or print the whole solution process
$printer->printSolver($solver);
```


### Solutions

`$solver->getSolutions()` returns primary optimal solution. The value can be of 3 types:

- `array` - vector with optimal coefficients to each input variable
- `false` - optimal solution does not exist
- `null` - not enough steps to find solution

By default, solver stops the calculation after 16 simplex tables. You can increase the maximum steps limit with the second parameter:

```php
$solver = new Simplex\Solver($task, 32);
```

`$solver->getAlternativeSolutions()` returns array of other optimal solutions if any have been found. It may return:

- `array` - array of vectors with alternative optimal solutions
- `null` - no alternative optimal solution found
38 changes: 0 additions & 38 deletions example.php

This file was deleted.

0 comments on commit 5e62948

Please sign in to comment.