1
- from swat import CAS
2
1
import sasoptpy as so
3
2
import pandas as pd
4
3
@@ -38,22 +37,24 @@ def test(cas_conn):
38
37
INDUSTRIES = industry_data .index .tolist ()
39
38
[init_stocks , init_productive_capacity , demand ] = so .read_frame (
40
39
industry_data )
41
- INPUTS = production_data .index .tolist ()
40
+ # INPUTS = production_data.index.tolist()
42
41
production_coeff = so .flatten_frame (production_data )
43
42
productive_capacity_coeff = so .flatten_frame (productive_capacity_data )
44
43
45
44
static_production = m .add_variables (INDUSTRIES , lb = 0 ,
46
45
name = 'static_production' )
47
46
m .set_objective (0 , sense = so .MIN , name = 'Zero' )
48
47
m .add_constraints ((static_production [i ] == demand [i ] +
49
- sum (production_coeff [i , j ] * static_production [j ]
48
+ so .quick_sum (
49
+ production_coeff [i , j ] * static_production [j ]
50
50
for j in INDUSTRIES ) for i in INDUSTRIES ),
51
51
name = 'static_con' )
52
52
53
53
m .solve ()
54
54
print (so .get_solution_table (static_production ))
55
55
56
- final_demand = so .get_solution_table (static_production )['static_production' ]
56
+ final_demand = so .get_solution_table (
57
+ static_production )['static_production' ]
57
58
# Alternative way
58
59
# final_demand = {}
59
60
# for i in INDUSTRIES:
@@ -69,35 +70,35 @@ def test(cas_conn):
69
70
for i in INDUSTRIES :
70
71
for year in range (1 , num_years + 2 ):
71
72
productive_capacity [i , year ] = init_productive_capacity [i ] + \
72
- sum (extra_capacity [i , y ] for y in range (2 , year + 1 ))
73
+ so . quick_sum (extra_capacity [i , y ] for y in range (2 , year + 1 ))
73
74
for i in INDUSTRIES :
74
75
production [i , 0 ].set_bounds (ub = 0 )
75
76
stock [i , 0 ].set_bounds (lb = init_stocks [i ], ub = init_stocks [i ])
76
77
77
78
total_productive_capacity = sum (productive_capacity [i , num_years ]
78
79
for i in INDUSTRIES )
79
- total_production = sum (production [i , year ] for i in INDUSTRIES
80
- for year in [4 , 5 ])
81
- total_manpower = sum (production_coeff ['manpower' , i ] *
82
- production [i , year + 1 ] +
83
- productive_capacity_coeff ['manpower' , i ] *
84
- extra_capacity [i , year + 2 ]
85
- for i in INDUSTRIES for year in YEARS )
80
+ total_production = so . quick_sum (production [i , year ] for i in INDUSTRIES
81
+ for year in [4 , 5 ])
82
+ total_manpower = so . quick_sum (production_coeff ['manpower' , i ] *
83
+ production [i , year + 1 ] +
84
+ productive_capacity_coeff ['manpower' , i ] *
85
+ extra_capacity [i , year + 2 ]
86
+ for i in INDUSTRIES for year in YEARS )
86
87
87
88
continuity_con = m .add_constraints ((
88
89
stock [i , year ] + production [i , year ] ==
89
90
(demand [i ] if year in YEARS else 0 ) +
90
- sum (production_coeff [i , j ] * production [j , year + 1 ] +
91
- productive_capacity_coeff [i , j ] * extra_capacity [ j , year + 2 ]
92
- for j in INDUSTRIES ) +
91
+ so . quick_sum (production_coeff [i , j ] * production [j , year + 1 ] +
92
+ productive_capacity_coeff [i , j ] *
93
+ extra_capacity [ j , year + 2 ] for j in INDUSTRIES ) +
93
94
stock [i , year + 1 ]
94
95
for i in INDUSTRIES for year in YEARS0 ), name = 'continuity_con' )
95
96
96
97
manpower_con = m .add_constraints ((
97
- sum (production_coeff ['manpower' , j ] * production [j , year ] +
98
- productive_capacity_coeff ['manpower' , j ] *
99
- extra_capacity [j , year + 1 ]
100
- for j in INDUSTRIES )
98
+ so . quick_sum (production_coeff ['manpower' , j ] * production [j , year ] +
99
+ productive_capacity_coeff ['manpower' , j ] *
100
+ extra_capacity [j , year + 1 ]
101
+ for j in INDUSTRIES )
101
102
<= manpower_capacity for year in range (1 , num_years + 2 )),
102
103
name = 'manpower_con' )
103
104
@@ -137,7 +138,7 @@ def test(cas_conn):
137
138
continuity_con [i , year ].set_rhs (0 )
138
139
problem2 .solve ()
139
140
print (so .get_solution_table (production , stock , extra_capacity ,
140
- productive_capacity ))
141
+ productive_capacity ))
141
142
print (so .get_solution_table (manpower_con .get_expressions ()))
142
143
143
144
# Problem 3
@@ -151,7 +152,7 @@ def test(cas_conn):
151
152
continuity_con [i , year ].set_rhs (demand [i ])
152
153
problem3 .solve ()
153
154
print (so .get_solution_table (production , stock , extra_capacity ,
154
- productive_capacity ))
155
+ productive_capacity ))
155
156
print (so .get_solution_table (manpower_con .get_expressions ()))
156
157
157
- return problem3 .get_objective_value ()
158
+ return problem3 .get_objective_value ()
0 commit comments