Skip to content

Commit

Permalink
adding simple example using numpy linear algebra (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
pstjohn authored and KristianJensen committed Nov 7, 2017
1 parent f1c8bb3 commit 9ece974
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/simple_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
from optlang import Model, Variable, Constraint, Objective

# All the (symbolic) variables are declared, with a name and optionally a lower
# and/or upper bound.
x = np.array([Variable('x{}'.format(i), lb=0) for i in range(1, 4)])

bounds = [100, 600, 300]

A = np.array([[ 1, 1, 1],
[10, 4, 5],
[ 2, 2, 6]])

w = np.array([ 10, 6, 4])

obj = Objective(w.dot(x), direction='max')

c = np.array([Constraint(row, ub=bound) for row, bound in zip(A.dot(x), bounds)])

model = Model(name='Numpy model')
model.objective = obj
model.add(c)

status = model.optimize()

print("status:", model.status)
print("objective value:", model.objective.value)
print("----------")
for var_name, var in model.variables.iteritems():
print(var_name, "=", var.primal)

0 comments on commit 9ece974

Please sign in to comment.