| 1 |
# Example of export OpenOpt MILP to MPS file |
|---|
| 2 |
# you should have lpsolve and its Python binding properly installed |
|---|
| 3 |
# (you may take a look at the instructions from openopt.org/LP) |
|---|
| 4 |
|
|---|
| 5 |
# You can solve problems defined in MPS files |
|---|
| 6 |
# with a variety of solvers at NEOS server for free |
|---|
| 7 |
# http://neos.mcs.anl.gov/ |
|---|
| 8 |
# BTW they have Python API along with web API and other |
|---|
| 9 |
|
|---|
| 10 |
from numpy import * |
|---|
| 11 |
from openopt import MILP |
|---|
| 12 |
|
|---|
| 13 |
f = [1, 2, 3, 4, 5, 4, 2, 1] |
|---|
| 14 |
|
|---|
| 15 |
# indexing starts from ZERO! |
|---|
| 16 |
# while in native lpsolve-python wrapper from 1 |
|---|
| 17 |
# so if you used [5,8] for native lp_solve python binding |
|---|
| 18 |
# you should use [4,7] instead |
|---|
| 19 |
intVars = [4, 7] |
|---|
| 20 |
|
|---|
| 21 |
lb = -1.5 * ones(8) |
|---|
| 22 |
ub = 15 * ones(8) |
|---|
| 23 |
A = zeros((5, 8)) |
|---|
| 24 |
b = zeros(5) |
|---|
| 25 |
for i in xrange(5): |
|---|
| 26 |
for j in xrange(8): |
|---|
| 27 |
A[i,j] = -8+sin(8*i) + cos(15*j) |
|---|
| 28 |
b[i] = -150 + 80*sin(80*i) |
|---|
| 29 |
|
|---|
| 30 |
p = MILP(f=f, lb=lb, ub=ub, A=A, b=b, intVars=intVars) |
|---|
| 31 |
|
|---|
| 32 |
# if file name not ends with '.MPS' or '.mps' |
|---|
| 33 |
# then '.mps' will be appended |
|---|
| 34 |
success = p.exportToMPS('/home/dmitrey/PyTest/milp_1') |
|---|
| 35 |
# or write into current dir: |
|---|
| 36 |
# success = p.exportToMPS('milp') |
|---|
| 37 |
# success is False if a error occurred (read-only file system, no write access, etc) |
|---|
| 38 |
# elseware success is True |
|---|
| 39 |
|
|---|
| 40 |
# f_opt is 25.801450769161505 |
|---|
| 41 |
# x_opt is [ 15. 10.15072538 -1.5 -1.5 -1. -1.5 -1.5 15.] |
|---|