-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding simple example using numpy linear algebra (#143)
- Loading branch information
1 parent
f1c8bb3
commit 9ece974
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |