11 from gams
import GamsWorkspace, GamsException, GamsExceptionExecution, GamsExitCode
21 $SetDDList warehouse store fixed disaggregate // acceptable defines
22 $if not set warehouse $set warehouse 10
23 $if not set store $set store 50
24 $if not set fixed $set fixed 20
25 $if not set disaggregate $set disaggregate 1 // indicator for tighter bigM constraint
26 $ife %store%<=%warehouse% $abort Increase number of stores (>%warehouse)
28 set res respond codes / 0 Normal
34 Sets Warehouse /w1*w%warehouse% /
36 Alias (Warehouse,w), (Store,s);
38 fixed fixed cost for opening a warehouse / %fixed% /
41 supplyCost(Store,Warehouse);
43 $eval storeDIVwarehouse trunc(card(store)/card(warehouse))
44 capacity(w) = %storeDIVwarehouse% + mod(ord(w),%storeDIVwarehouse%);
45 supplyCost(s,w) = 1+mod(ord(s)+10*ord(w), 100);
49 supply(Store,Warehouse)
51 Binary variables open, supply;
58 defobj.. obj =e= sum(w, fixed*open(w)) + sum((w,s), supplyCost(s,w)*supply(s,w));
60 oneWarehouse(s).. sum(w, supply(s,w)) =e= 1;
62 defopen(w).. sum(s, supply(s,w)) =l= open(w)*capacity(w);
64 $ifthen %disaggregate%==1
67 defopen2(s,w).. supply(s,w) =l= open(w);
71 solve distrib min obj using mip;
73 $macro setResult(n) option clear=ares; ares(n) = yes;
74 if (distrib.modelstat=%ModelStat.LicensingProblem% or
75 distrib.solvestat=%Solvestat.LicensingProblems%,
77 abort 'License Error';
79 if (distrib.solvestat<>%SolveStat.NormalCompletion% or
80 distrib.modelstat<>%ModelStat.Optimal% and
81 distrib.modelstat<>%ModelStat.IntegerSolution%,
92 opt = workspace.add_options()
93 opt.all_model_types =
"cplex"
94 opt.defines[
"Warehouse"] = str(number_of_warehouses)
95 opt.defines[
"Store"] =
"65"
96 opt.defines[
"fixed"] =
"22"
97 opt.defines[
"disaggregate"] =
"0"
106 result[
"objrep"].add_record(str(number_of_warehouses)).value = job.out_db[
"obj"][()].level
108 for supply_rec
in job.out_db[
"supply"]:
109 if supply_rec.level > 0.5:
111 result[
"supplyMap"].add_record((str(number_of_warehouses), supply_rec.key(0), supply_rec.key(1)))
113 except GamsExceptionExecution
as e:
115 if e.rc == GamsExitCode.ExecutionError:
117 status_string = job.out_db[
"res"]
118 status_string = status_string.find_record(job.out_db[
"ares"].first_record().
key(0)).text
123 except GamsException
as e:
128 except Exception
as e:
134 if __name__ ==
"__main__":
135 if len(sys.argv) > 1:
136 ws = GamsWorkspace(system_directory = sys.argv[1])
141 result_db = ws.add_database()
142 result_db.add_parameter(
"objrep" ,1 ,
"Objective value")
143 result_db.add_set(
"supplyMap" ,3 ,
"Supply connection with level")
150 db_lock = threading.Lock()
152 for i
in range(10,22):
153 threads[i] = threading.Thread(target=solve_warehouse, args=(ws, i, result_db, db_lock))
155 for t
in threads.values():
158 raise GamsExceptionExecution(
"Error when running GAMS: " + str(status) +
" " + status_string, status);
160 raise GamsException(
"Error in GAMS API")
165 result_db.export(
"/tmp/result.gdx")
167 except GamsException
as e:
168 print(
"GamsException occured: " , e)
169 except Exception
as e: