-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlp_interface_noiseless.py
71 lines (47 loc) · 1.69 KB
/
lp_interface_noiseless.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
import cplex
alpha = 1
def solve(y, A, nvar, noiseless):
#get float for CPLEX
A = [[float(a_ij) for a_ij in a_i] for a_i in A]
y = [float(y_i) for y_i in y]
A_i = []
A_j = []
Y_i = [item for item in y if item == 1]
for i in range(len(y)):
if y[i] == 1.0:
A_i.append((A[i]))
else:
A_j.append((A[i]))
#variables name
r_x = ["x" + str(i) for i in range(1, nvar +1)]
#objective function coefficient
objective = [1 for i in range(nvar)]
#upper bounds x<=1
upper_bounds = [1 for i in range(nvar)]
#lower bounds x>= 0
lower_bounds = [0 for i in range(nvar)]
#constraints
m_constraints = [[r_x, a] for a in A_i] + [[r_x[0:nvar], a] for a in A_j]
m_senses= ["G" for a in A_i] + ["E" for a in A_j]
m_rhs = [y_i for y_i in Y_i] + [0 for a in A_j]
m_names = ["c_" + str(i) for i in range(len(m_constraints))]
#Create instance of the problem
problem = cplex.Cplex()
problem.objective.set_sense(problem.objective.sense.minimize)
problem.variables.add(obj=objective,
lb = lower_bounds,
ub = upper_bounds,
names = r_x)
problem.linear_constraints.add(lin_expr = m_constraints,
senses = m_senses ,
rhs = m_rhs,
names = m_names)
problem.set_log_stream(None)
problem.set_error_stream(None)
problem.set_warning_stream(None)
problem.set_results_stream(None)
#Call solve
problem.solve()
#get the solution
recovered_x = problem.solution.get_values()
return recovered_x