13from threading
import Lock, Thread
14from gams
import GamsWorkspace, GamsException, GamsExceptionExecution, GamsExitCode
18$SetDDList warehouse store fixed disaggregate // acceptable defines
19$if not set warehouse $set warehouse 10
20$if not set store $set store 50
21$
if not set fixed $set fixed 20
22$
if not set disaggregate $set disaggregate 1 // indicator
for tighter bigM constraint
24$ife %store%<=%warehouse% $abort Increase number of stores (>%warehouse)
33 Warehouse / w1*w%warehouse% /
34 Store / s1*s%store% /;
40Scalar fixed
'fixed cost for opening a warehouse' / %fixed% /;
44 supplyCost(Store,Warehouse);
46$eval storeDIVwarehouse trunc(card(store)/card(warehouse))
48capacity(w) = %storeDIVwarehouse% + mod(ord(w), %storeDIVwarehouse%);
49supplyCost(s,w) = 1 + mod(ord(s) + 10*ord(w), 100);
53 supply(Store,Warehouse)
56Binary Variable open, supply;
63defobj.. obj =e= sum(w, fixed*open(w)) + sum((w,s), supplyCost(s,w)*supply(s,w));
65oneWarehouse(s).. sum(w, supply(s,w)) =e= 1;
67defopen(w).. sum(s, supply(s,w)) =l= open(w)*capacity(w);
69$ifthen %disaggregate%==1
70Equation defopen2(s,w);
71defopen2(s,w).. supply(s,w) =l= open(w);
75solve distrib min obj using mip;
77$macro setResult(n) option clear=ares; ares(n) = yes;
79if (distrib.modelstat=%ModelStat.LicensingProblem%
or
80 distrib.solvestat=%Solvestat.LicensingProblems%,
82 abort
'License Error';
85if (distrib.solvestat<>%SolveStat.NormalCompletion%
or
86 distrib.modelstat<>%ModelStat.Optimal%
and
87 distrib.modelstat<>%ModelStat.IntegerSolution%,
96def solve_warehouse(workspace, nr_warehouses, result, lock):
101 opt = workspace.add_options()
102 opt.all_model_types =
"cplex"
103 opt.defines[
"Warehouse"] = str(nr_warehouses)
104 opt.defines[
"Store"] =
"65"
105 opt.defines[
"fixed"] =
"22"
106 opt.defines[
"disaggregate"] =
"0"
110 job = workspace.add_job_from_string(GAMS_MODEL)
115 result[
"objrep"].add_record(str(nr_warehouses)).value = (
116 job.out_db[
"obj"].first_record().level
119 for supply_rec
in job.out_db[
"supply"]:
120 if supply_rec.level > 0.5:
122 result[
"supplyMap"].add_record(
123 (str(nr_warehouses), supply_rec.key(0), supply_rec.key(1))
126 except Exception
as e:
128 if isinstance(e, GamsExceptionExecution):
130 if e.rc == GamsExitCode.ExecutionError:
133 .find_record(job.out_db[
"ares"].first_record().key(0))
139 status = -1
if isinstance(e, GamsException)
else -2
143if __name__ ==
"__main__":
144 sys_dir = sys.argv[1]
if len(sys.argv) > 1
else None
145 ws = GamsWorkspace(system_directory=sys_dir)
148 result_db = ws.add_database()
149 result_db.add_parameter(
"objrep", 1,
"Objective value")
150 result_db.add_set(
"supplyMap", 3,
"Supply connection with level")
158 for i
in range(10, 22):
159 t = Thread(target=solve_warehouse, args=(ws, i, result_db, lock))
165 raise GamsExceptionExecution(
166 f
"GamsException occurred: Error when running GAMS: {status} {status_string}",
171 raise GamsException(
"GamsException occurred: Error in GAMS API", ws)
173 raise Exception(
"Error occurred")
176 result_db.export(os.path.join(os.getcwd(),
"result.gdx"))