forked from mahmad3397/EVCSPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpevcs.py
208 lines (174 loc) · 5.64 KB
/
cpevcs.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import time
import cplex
from input import *
start_time = time.time()
# ============================================================
# This file gives us a sample to use Cplex Python API to
# establish a Mixed Integer Linear Programming model and then solve it.
# The problem displayed bellow is as:
# min z = cx
# ============================================================
# Input all the data and parameters here
'''
num_decision_var = 8
D=6
d = [[0,6,12,3,4,10,14,16],[6,0,6,9,10,4,8,10],[12,6,0,15,16,10,6,4],[3,9,15,0,1,7,11,13],[4,10,16,1,0,6,11,12],[10,4,10,7,6,0,4,6],[10,8,6,11,10,4,0,2],[16,10,4,13,12,6,2,0]]'''
con=[[0 for i in range(num_decision_var)] for j in range(num_decision_var)]
for i in range(num_decision_var):
for j in range(num_decision_var):
if(d[i][j]<=D and d[i][j]>0):
con[i][j]=1
print(con)
E=[]
k=[]
for i in range(num_decision_var):
for j in range(num_decision_var):
if(con[i][j]==1):
E.append("Y"+str(i)+str(j))
k.append(j)
print(E)
#print(k)
'''
f = [1,1,1,1,1,1,1,1]#0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5
F = [1,1,1,1,1,1,1,1] # F values
c = [0.11, 0.41, 0.30, 0.08,0.2,0.5,0.35,0.4] #cost of constructions
x = ["x0", "x1", "x2", "x3","x4","x5","x6","x7"]'''
# ============================================================
# Establish the Linear Programming Model
myProblem = cplex.Cplex()
# Add objective function and set its sense
myProblem.variables.add(obj=c, names=x)
myProblem.objective.set_sense(myProblem.objective.sense.minimize)
# Add the decision variable
indices = myProblem.variables.add(names = E)
myProblem.variables.add(ub=[num_decision_var],names = ["x01"])
myProblem.variables.add(ub=[num_decision_var],names = ["y01"])
# Set the type of each variables
for i in range(num_decision_var):
myProblem.variables.set_types(i, myProblem.variables.type.binary)
for i in range(len(x),myProblem.variables.get_num()):
myProblem.variables.set_types(i, myProblem.variables.type.integer)
# Add constraints
#xj.fj>=Fi
for i in range(num_decision_var):
rows = [[], []]
for j in range(num_decision_var):
rows[0].append(x[j])
if(d[i][j]<=D):
rows[1].append(f[j])
else:
rows[1].append(0)
myProblem.linear_constraints.add(lin_expr=[rows], senses="G",
rhs=[F[i]])
#Yjk<=n.Xk
for i in range(len(E)):
rows=[[E[i],k[i]],[1,-num_decision_var]]
myProblem.linear_constraints.add(lin_expr=[rows], senses="L",
rhs=[0])
#sum(Yjk)=Xk+sum(Ykl)
for i in range(num_decision_var):
rows=[[],[]]
for j in range(num_decision_var):
if(con[j][i]==1):
rows[0].append("Y"+str(j)+str(i))
rows[1].append(1)
if(i==5):
rows[0].append("y01")
rows[1].append(1)
rows[0].append("x"+str(i))
rows[1].append(-1)
for j in range(num_decision_var):
if(con[i][j]==1):
rows[0].append("Y"+str(i)+str(j))
rows[1].append(-1)
myProblem.linear_constraints.add(lin_expr=[rows], senses="E",
rhs=[0])
#X01+Y01=n
rows=[[],[]]
rows[0].append("x01")
rows[0].append("y01")
rows[1].append(1)
rows[1].append(1)
myProblem.linear_constraints.add(lin_expr=[rows], senses="E",
rhs=[num_decision_var])
#sum(xj)=y01
rows=[[],[]]
for i in range(len(x)):
rows[0].append(x[i])
rows[1].append(1)
rows[0].append("y01")
rows[1].append(-1)
myProblem.linear_constraints.add(lin_expr=[rows], senses="E",
rhs=[0])
#fixing i value to attach source node
rows=[[],[]]
rows[0].append("x0")
rows[1].append(1)
myProblem.linear_constraints.add(lin_expr=[rows], senses="E",
rhs=[1])
'''
#x0+y0=n
rows = [["x0", "x1", "x2", "x3","x4","x5","x6","x7"], [1.0, 1.0, 1.0, 1.0,1.0, 1.0, 1.0, 1.0]]
myProblem.linear_constraints.add(lin_expr=[rows], senses="E",
rhs=[n-x])
'''
'''
#Yjk<=n.Xk
for i in range(num_decision_var):
rows = [["x0", "x1", "x2", "x3"], [0.0, 0.0, 0.0, 0.0]]
for j in range(i):
rows[1][j]=1/n
rows[1][i]=1
myProblem.linear_constraints.add(lin_expr=[rows], senses="G",
rhs=[1])
for i in range(num_decision_var):
rows = [["x0", "x1", "x2", "x3"], [0.0, 0.0, 0.0, 0.0]]
for j in range(i):
rows[1][j]=1
myProblem.linear_constraints.add(lin_expr=[rows], senses="L",
rhs=[n])
'''
''' Code to compute Yjk value; here in Yi, j=i-1 and k=i'''
#for i in range(num_decision_var):
#rows = [["x0", "x1", "x2", "x3"], [0.0, 0.0, 0.0, 0.0]]
#for j in range(i):
#rows[1][j]=1
'''
# Add the decision variables and set their lower bound and upper bound (if necessary)
myProblem.variables.add(names= ["x"+str(i) for i in range(num_decision_var)])
# Set the type of each variables
for i in range(num_decision_var):
myProblem.variables.set_types(i, myProblem.variables.type.binary)
#print(myProblem.variables.get_names(0))
#a=0
#for i in range(2):
#a+=myProblem.variables.get_names(i)
#print(a)
# Add constraints
for i in range(num_constraints):
myProblem.linear_constraints.add(
lin_expr= [cplex.SparsePair(ind= [j for j in range(num_decision_var)], val= f[i])],
rhs= [F[i]],
names = ["c"+str(i)],
senses = [constraint_type[i]]
)
# Add objective function and set its sense
for i in range(num_decision_var):
myProblem.objective.set_linear([(i, c[i])])
myProblem.objective.set_sense(myProblem.objective.sense.maximize)
'''
# Solve the model and print the answer
myProblem.solve()
res=myProblem.solution.get_values()
print(res)
print(x,end='')
print(E,end='')
print(",[x01,y01]")
cost=0
for i in range(len(x)):
cost+=c[i]*res[i]
print("Total cost is ",end="")
print(cost)
end_time = time.time()
print("Computation time (s) ", end="")
print(end_time-start_time)