-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc_flow.py
213 lines (165 loc) · 7.71 KB
/
qc_flow.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
209
210
211
212
import numpy as np
from collections import defaultdict
import threading
from cost_function import *
import pandas as pd
from utils import *
from Models.Flights import Flight
from feasible_flights import *
import time
import os
from handle_city_pairs import *
import dimod
from dwave.system import LeapHybridCQMSampler
# import dwave.inspector
from dimod import ConstrainedQuadraticModel, BinaryQuadraticModel, QuadraticModel
from dimod import Real
from dotenv import load_dotenv
import pprint
pp = pprint.PrettyPrinter(indent=4)
assignments=defaultdict(list)
lock=threading.Lock()
total_cost=0
# Load the .env file
load_dotenv()
# Access the API key
dwave_token = os.getenv('DWAVE_TOKEN')
def Flow(PNR_list,flight_cabin_tuple,sampler):
CQM=dimod.ConstrainedQuadraticModel()
CQM_obj = 0
X_PNR_Constraint = defaultdict(list)
X_Flight_Capacity_Constraint = defaultdict(list)
X = {}
variable_cnt=0
flight_object = flight_cabin_tuple[0]
Cabin=flight_cabin_tuple[1]
order=flight_cabin_tuple[2]
my_dict={}
if(Cabin=='FC'):
for PNR in PNR_list:
for key,value in flight_object.fc_class_dict.items():
X[(PNR,key)]=dimod.Integer(f'X_{variable_cnt}')
my_dict[f'X_{variable_cnt}'] = (PNR,key)
X_PNR_Constraint[PNR].append(X[(PNR,key)])
X_Flight_Capacity_Constraint[key].append(X[((PNR,key))])
variable_cnt+=1
# for flight_index,flight in enumerate(FT): # Flight is a object
# for cabin in cabins_tuple: # cabin is a tuple Eg: ('FC','PC') and cabins_tuple = list of cabins
# X_Flight_Capacity_Constraint[flight][cabin[flight_index]].append(X[(PNR, FT, cabin)] * PNR.PAX)
for PNR in PNR_list :
if(len(X_PNR_Constraint[PNR])==0):
continue
CQM.add_constraint(sum(X_PNR_Constraint[PNR]) == PNR.PAX)
for key,value in flight_object.fc_class_dict.items():
CQM.add_constraint(sum(X_Flight_Capacity_Constraint[key]) <= value)
elif(Cabin=="BC"):
for PNR in PNR_list:
for key,value in flight_object.bc_class_dict.items():
X[(PNR,key)]=dimod.Integer(f'X_{variable_cnt}')
my_dict[f'X_{variable_cnt}'] = (PNR,key)
X_PNR_Constraint[PNR].append(X[(PNR,key)])
X_Flight_Capacity_Constraint[key].append(X[((PNR,key))])
variable_cnt+=1
# for flight_index,flight in enumerate(FT): # Flight is a object
# for cabin in cabins_tuple: # cabin is a tuple Eg: ('FC','PC') and cabins_tuple = list of cabins
# X_Flight_Capacity_Constraint[flight][cabin[flight_index]].append(X[(PNR, FT, cabin)] * PNR.PAX)
for PNR in PNR_list :
if(len(X_PNR_Constraint[PNR])==0):
continue
CQM.add_constraint(sum(X_PNR_Constraint[PNR]) == PNR.PAX)
for key,value in flight_object.bc_class_dict.items():
CQM.add_constraint(sum(X_Flight_Capacity_Constraint[key]) <= value)
elif(Cabin=="PC"):
for PNR in PNR_list:
for key,value in flight_object.pc_class_dict.items():
X[(PNR,key)]=dimod.Integer(f'X_{variable_cnt}')
my_dict[f'X_{variable_cnt}'] = (PNR,key)
X_PNR_Constraint[PNR].append(X[(PNR,key)])
X_Flight_Capacity_Constraint[key].append(X[((PNR,key))])
variable_cnt+=1
# for flight_index,flight in enumerate(FT): # Flight is a object
# for cabin in cabins_tuple: # cabin is a tuple Eg: ('FC','PC') and cabins_tuple = list of cabins
# X_Flight_Capacity_Constraint[flight][cabin[flight_index]].append(X[(PNR, FT, cabin)] * PNR.PAX)
for PNR in PNR_list :
if(len(X_PNR_Constraint[PNR])==0):
continue
CQM.add_constraint(sum(X_PNR_Constraint[PNR]) == PNR.PAX)
for key,value in flight_object.pc_class_dict.items():
CQM.add_constraint(sum(X_Flight_Capacity_Constraint[key]) <= value)
else:
for PNR in PNR_list:
for key,value in flight_object.ec_class_dict.items():
X[(PNR,key)]=dimod.Integer(f'X_{variable_cnt}')
my_dict[f'X_{variable_cnt}'] = (PNR,key)
X_PNR_Constraint[PNR].append(X[(PNR,key)])
X_Flight_Capacity_Constraint[key].append(X[((PNR,key))])
variable_cnt+=1
# for flight_index,flight in enumerate(FT): # Flight is a object
# for cabin in cabins_tuple: # cabin is a tuple Eg: ('FC','PC') and cabins_tuple = list of cabins
# X_Flight_Capacity_Constraint[flight][cabin[flight_index]].append(X[(PNR, FT, cabin)] * PNR.PAX)
for PNR in PNR_list :
if(len(X_PNR_Constraint[PNR])==0):
continue
CQM.add_constraint(sum(X_PNR_Constraint[PNR]) == PNR.PAX)
for key,value in flight_object.ec_class_dict.items():
CQM.add_constraint(sum(X_Flight_Capacity_Constraint[key]) <= value)
for key,value in X.items():
Cost=cabin_to_class_cost(key[0],key[1])
CQM_obj+=(value*Cost)
start=time.time()
CQM.set_objective(-1*CQM_obj)
sampleset = sampler.sample_cqm(CQM).aggregate()
end_time_sampling = time.time()
start_agg = time.time()
feasible_sampleset = sampleset.filter(lambda row: row.is_feasible)
end_agg = time.time()
print("TYPE OF SAMPLESET IS " , type(sampleset) )
print("Total Filter time " , end_agg - start_agg)
print("{} feasible solutions of {}.".format(len(feasible_sampleset), len(sampleset)))
best = feasible_sampleset.first.sample
print("Total No. of Quantum Solutions are " , len(feasible_sampleset))
solution_count= 0
Final_Quantum_Solutions =[]
QSol_count=1
for idx,sample in enumerate(feasible_sampleset.truncate(QSol_count)):
# print("NEXT SOLUTION\n")
if(idx >=QSol_count):
break
Final_Quantum_Solutions.append(sample)
start_cost_cal = time.time()
print("Total sampling time " , start_cost_cal - start)
Sol=Final_Quantum_Solutions[0]
num_acc={}
for pnr in PNR_list:
num_acc[pnr.pnr_number]=0
for key,value in Sol.items():
while(value):
final_tuple=(my_dict[key][0],flight_object,Cabin,my_dict[key][1],int(num_acc[my_dict[key][0].pnr_number])+1)
assignments[my_dict[key][0].pnr_number].append([final_tuple,order])
num_acc[my_dict[key][0].pnr_number]+=1
value-=1
def Cabin_to_Class_1(Assignment_list):
"""
Input: Assignment_list is the list of (PNR,Flight_tuple,Cabin_tuple)
Returns: A dictionery , in which keys are PNR numbers and the values are list of (PNR_Object,Flight_object,Cabin,Class, Passenger number)
and the list contains all possible flights of the PNR in order.
"""
flow_map=defaultdict(list)
for assignment in Assignment_list:
i=0
while(i<len(assignment[1])):
flow_map[(assignment[1][i],assignment[2][i],i)].append(assignment[0])
i+=1
sampler = LeapHybridCQMSampler(token=dwave_token)
for flight_cabin_tuple,PNR_Object in flow_map.items():
Flow(PNR_Object,flight_cabin_tuple,sampler)
final_assignments=[]
for key1, value_list in assignments.items():
assignments[key1] = sorted(value_list, key=lambda x: x[1])
for key1, value_list in assignments.items():
temp=[]
for flights in value_list:
temp.append(flights[0])
assignments[key1]=temp
#pp.pprint(assignments)
return assignments