-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlpdemo.py
127 lines (105 loc) · 2.49 KB
/
lpdemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Script to demonstrate use of the lp_solve toolkit
from lpsolve55 import *
lp=lpsolve('make_lp',0,4)
lpsolve('add_constraint',lp,[3, 2, 2, 1],3,4)
lpsolve('add_constraint',lp,[0, 4, 3, 1],2,3)
lpsolve('set_obj_fn',lp,[2, 3, -2, 3])
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
# Change a single element, and maximize
lpsolve('set_mat',lp,2,1,0.5)
lpsolve('set_maxim',lp)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
# Change RHS
lpsolve('set_rh',lp,1,7.45)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
# Set var 4 to an integer
lpsolve('set_int',lp,4,1)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
# Put in lower and upper bounds
lpsolve('set_lowbo',lp,2,2)
lpsolve('set_upbo',lp,4,5.3)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
# Delete a constraint
lpsolve('del_constraint',lp,1)
lpsolve('add_constraint',lp,[1, 2, 1, 4],3,8)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
lpsolve('delete_lp',lp)
#%%%%%%%%%%%%
# More examples
# ex1.lp from the lp_solve distribution
lp=lpsolve('make_lp',2,2)
lpsolve('set_mat',lp,[[2, 1],[-4, 4]])
lpsolve('set_obj_fn',lp,[-1, 2])
lpsolve('set_int',lp,[1,1])
lpsolve('set_rh_vec',lp,[5, 5])
lpsolve('set_maxim',lp)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
lpsolve('delete_lp',lp)
# Example 2
f = [50, 100]
A = [[10, 5],[4, 10], [1, 1.5]]
b = [2500, 2000, 450]
e = [-1, -1, -1]
m = len(A)
n = len(A[0])
lp=lpsolve('make_lp',m,n)
lpsolve('set_obj_fn',lp,f)
lpsolve('set_mat',lp,A)
lpsolve('set_rh_vec',lp,b)
lpsolve('set_maxim',lp)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
lpsolve('delete_lp',lp)
# Example 3
f = [-40, -36]
vub = [8, 10]
A = [[5, 3]]
b = [45]
e = 1
m = len(A)
n = len(A[0])
lp=lpsolve('make_lp',m,n)
lpsolve('set_obj_fn',lp,f)
lpsolve('set_mat',lp,A)
lpsolve('set_rh_vec',lp,b)
lpsolve('set_constr_type',lp,1,2)
lpsolve('set_upbo',lp,1,8)
lpsolve('set_upbo',lp,2,10)
lpsolve('set_maxim',lp)
result=lpsolve('solve',lp)
obj=lpsolve('get_objective', lp)
print obj
x=lpsolve('get_variables', lp)[0]
print x
lpsolve('delete_lp',lp)