mirror of https://github.com/pyodide/pyodide.git
PKG Add optlang package (#1093)
This commit is contained in:
parent
f772ca8ebf
commit
65aa334351
|
@ -0,0 +1,13 @@
|
|||
package:
|
||||
name: glpk
|
||||
version: 4.65
|
||||
|
||||
source:
|
||||
sha256: 4281e29b628864dfe48d393a7bedd781e5b475387c20d8b0158f329994721a10
|
||||
url: https://ftp.gnu.org/gnu/glpk/glpk-4.65.tar.gz
|
||||
|
||||
build:
|
||||
library: true
|
||||
script: |
|
||||
CFLAGS="-fPIC" emconfigure ./configure --prefix=$PWD
|
||||
emmake make -j ${PYODIDE_JOBS:-3}
|
|
@ -0,0 +1,19 @@
|
|||
package:
|
||||
name: optlang
|
||||
version: 1.4.7
|
||||
|
||||
source:
|
||||
sha256: 0c9d7aae9babd5f9eac296b6f975a0e475545ac7ced9790f43796671157eb017
|
||||
url: https://files.pythonhosted.org/packages/47/85/9d028fbc971b1db9b8c3e58072237e8a7fda5cd936dd73f0b179805b5464/optlang-1.4.7.tar.gz
|
||||
|
||||
requirements:
|
||||
run:
|
||||
- sympy
|
||||
- six
|
||||
- swiglpk
|
||||
|
||||
test:
|
||||
imports:
|
||||
- optlang
|
||||
- optlang.glpk_interface
|
||||
- optlang.symbolics
|
|
@ -0,0 +1,40 @@
|
|||
import pytest
|
||||
|
||||
|
||||
def test_optlang(selenium):
|
||||
selenium.load_package("optlang")
|
||||
selenium.run(
|
||||
"""
|
||||
from optlang import Model, Variable, Constraint, Objective
|
||||
|
||||
# All the (symbolic) variables are declared, with a name and optionally a lower and/or upper bound.
|
||||
x1 = Variable('x1', lb=0)
|
||||
x2 = Variable('x2', lb=0)
|
||||
x3 = Variable('x3', lb=0)
|
||||
|
||||
# A constraint is constructed from an expression of variables and a lower and/or upper bound (lb and ub).
|
||||
c1 = Constraint(x1 + x2 + x3, ub=100)
|
||||
c2 = Constraint(10 * x1 + 4 * x2 + 5 * x3, ub=600)
|
||||
c3 = Constraint(2 * x1 + 2 * x2 + 6 * x3, ub=300)
|
||||
|
||||
# An objective can be formulated
|
||||
obj = Objective(10 * x1 + 6 * x2 + 4 * x3, direction='max')
|
||||
|
||||
# Variables, constraints and objective are combined in a Model object, which can subsequently be optimized.
|
||||
model = Model(name='Simple model')
|
||||
model.objective = obj
|
||||
model.add([c1, c2, c3])
|
||||
|
||||
status = model.optimize()
|
||||
"""
|
||||
)
|
||||
result = selenium.run("model.status")
|
||||
assert result == "optimal"
|
||||
result = selenium.run("model.objective.value")
|
||||
assert result == pytest.approx(733.3333, abs=1e-4)
|
||||
result = selenium.run("model.variables['x1'].primal")
|
||||
assert result == pytest.approx(33.3333, abs=1e-4)
|
||||
result = selenium.run("model.variables['x2'].primal")
|
||||
assert result == pytest.approx(66.6667, abs=1e-4)
|
||||
result = selenium.run("model.variables['x3'].primal")
|
||||
assert result == pytest.approx(0.0000, abs=1e-4)
|
|
@ -0,0 +1,20 @@
|
|||
package:
|
||||
name: swiglpk
|
||||
version: 4.65.1
|
||||
|
||||
source:
|
||||
sha256: 0216db2930a6fe2c07ac7f0e28e76e9a2711a647836a3a4067113091c7ae221e
|
||||
url: https://files.pythonhosted.org/packages/0e/c9/9b0ec3e2ca942b6d067e84e52d72818fa9c3a1a1524671fcbec4841b54b2/swiglpk-4.65.1.tar.gz
|
||||
extras:
|
||||
- [../glpk/build/glpk-4.65/src/glpk.h, ./glpk.h]
|
||||
|
||||
build:
|
||||
ldflags: -L../../../glpk/build/glpk-4.65/src/.libs/
|
||||
|
||||
requirements:
|
||||
run:
|
||||
- glpk
|
||||
|
||||
test:
|
||||
imports:
|
||||
- swiglpk
|
|
@ -0,0 +1,48 @@
|
|||
def test_swiglpk(selenium):
|
||||
selenium.load_package("swiglpk")
|
||||
selenium.run(
|
||||
"""
|
||||
from swiglpk import *
|
||||
|
||||
ia = intArray(1+1000); ja = intArray(1+1000);
|
||||
ar = doubleArray(1+1000);
|
||||
lp = glp_create_prob();
|
||||
glp_set_prob_name(lp, "sample");
|
||||
glp_set_obj_dir(lp, GLP_MAX);
|
||||
glp_add_rows(lp, 3);
|
||||
glp_set_row_name(lp, 1, "p");
|
||||
glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 100.0);
|
||||
glp_set_row_name(lp, 2, "q");
|
||||
glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 600.0);
|
||||
glp_set_row_name(lp, 3, "r");
|
||||
glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 300.0);
|
||||
glp_add_cols(lp, 3);
|
||||
glp_set_col_name(lp, 1, "x1");
|
||||
glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
|
||||
glp_set_obj_coef(lp, 1, 10.0);
|
||||
glp_set_col_name(lp, 2, "x2");
|
||||
glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
|
||||
glp_set_obj_coef(lp, 2, 6.0);
|
||||
glp_set_col_name(lp, 3, "x3");
|
||||
glp_set_col_bnds(lp, 3, GLP_LO, 0.0, 0.0);
|
||||
glp_set_obj_coef(lp, 3, 4.0);
|
||||
ia[1] = 1; ja[1] = 1; ar[1] = 1.0; # a[1,1] = 1
|
||||
ia[2] = 1; ja[2] = 2; ar[2] = 1.0; # a[1,2] = 1
|
||||
ia[3] = 1; ja[3] = 3; ar[3] = 1.0; # a[1,3] = 1
|
||||
ia[4] = 2; ja[4] = 1; ar[4] = 10.0; # a[2,1] = 10
|
||||
ia[5] = 3; ja[5] = 1; ar[5] = 2.0; # a[3,1] = 2
|
||||
ia[6] = 2; ja[6] = 2; ar[6] = 4.0; # a[2,2] = 4
|
||||
ia[7] = 3; ja[7] = 2; ar[7] = 2.0; # a[3,2] = 2
|
||||
ia[8] = 2; ja[8] = 3; ar[8] = 5.0; # a[2,3] = 5
|
||||
ia[9] = 3; ja[9] = 3; ar[9] = 6.0; # a[3,3] = 6
|
||||
glp_load_matrix(lp, 9, ia, ja, ar);
|
||||
glp_simplex(lp, None);
|
||||
Z = glp_get_obj_val(lp);
|
||||
x1 = glp_get_col_prim(lp, 1);
|
||||
x2 = glp_get_col_prim(lp, 2);
|
||||
x3 = glp_get_col_prim(lp, 3);
|
||||
glp_delete_prob(lp);
|
||||
"""
|
||||
)
|
||||
result = selenium.run("""'Z = %g; x1 = %g; x2 = %g; x3 = %g' % (Z, x1, x2, x3)""")
|
||||
assert result == "Z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0"
|
Loading…
Reference in New Issue