Loading...
Searching...
No Matches
millco.py
Go to the documentation of this file.
17
18if __name__ == "__main__":
19
20 # [1]
21 from gams.magic import GamsInteractive
22 gams = GamsInteractive()
23
24 # [2]
25 import numpy as np
26 m = gams.exchange_container
27
28 # Model sets and parameters
29 sites = m.addSet('sites', records = ["1", "2", "3"])
30 mills = m.addSet('mills', records = ["Mill A","Mill B", "Mill C"])
31 dist = m.addParameter('dist', [sites, mills], records = np.array([[ 8,15,50],
32 [10,17,20],
33 [30,26,15]]))
34 supply = m.addParameter('supply', [sites], records = np.array([20,30,45]))
35 demand = m.addParameter('demand', [mills], records = np.array([30,35,30]))
36 cost_per_haul = m.addParameter('cost_per_haul', records = 4)
37
38 # Model variables
39 m.addAlias('s',sites)
40 m.addAlias('m', mills)
41 ship = m.addVariable('ship', 'positive', [sites, mills])
42 obj = m.addVariable('obj', 'free')
43
44 # [3]
45 gams.gams('''
46# GAMS Model
47Equation defcost; defcost.. cost_per_haul*sum((s,m), ship(s,m)*dist(s,m)) =e= obj;
48Equation defsupply; defsupply(s).. sum(m, ship(s,m)) =e= supply(s);
49Equation defdemand; defdemand(m).. sum(s, ship(s,m)) =e= demand(m);
50
51model millco / all /;''')
52
53 # [4]
54 gams.gams('solve millco min obj using lp;')
55
56 # [5]
57 print(m["ship"].pivot())
58 print(f'Total cost will be {obj.records["level"][0]}')
59
60 # [6]
61 gams.create('genmodel')
62 m = gams.exchange_container
63
64 # Generic Model sets and parameters
65 i = m.addSet('i', records = ['i'+str(i) for i in range(6)], description = 'equation index')
66 j = m.addSet('j', records = ['j'+str(j) for j in range(9)], description = 'variable index')
67 A = m.addParameter('A', [i,j], records = np.array([[1,1,1,0,0,0,0,0,0],
68 [0,0,0,1,1,1,0,0,0],
69 [0,0,0,0,0,0,1,1,1],
70 [1,0,0,1,0,0,1,0,0],
71 [0,1,0,0,1,0,0,1,0],
72 [0,0,1,0,0,1,0,0,1]]))
73 b = m.addParameter('b', [i], records = np.array([20,30,45,30,35,30]))
74 c = m.addParameter('c', [j], records = np.array([8,15,50,10,17,20,30,26,15]))
75 x = m.addVariable('x', 'positive', [j])
76 obj = m.addVariable('obj', 'free')
77
78 # [7]
79 gams.gams('''
80# Generic GAMS Model
81Equation defobj; defobj.. 4*sum(j, c(j)*x(j)) =e= obj;
82Equation e(i); e(i).. sum(j, A(i,j)*x(j)) =e= b(i);
83
84model gen / all /;''')
85
86 # [8]
87 gams.gams('solve gen min obj using lp;')
88
89 # [9]
90 print(x.records)
91 print(f'Total cost will be {obj.records["level"][0]}')
92
93 # [10]
94 gams.gams_cleanup(closedown=True)
GamsInteractive A
Definition: millco.py:67
GamsInteractive x
Definition: millco.py:75
GamsInteractive b
Definition: millco.py:73
GamsInteractive supply
Definition: millco.py:34
GamsInteractive ship
Definition: millco.py:41
GamsInteractive dist
Definition: millco.py:31
GamsInteractive demand
Definition: millco.py:35