optimize
cybersyn.optimize
Optimize an Economy dataclass using linear programming.
TODO
Input targets as an object in optimizer? - Target domestic - Target export Add more ecological constraints
ErrorPeriods()
Bases: Exception
Error in the number of revise periods given.
Source code in cybersyn/optimize.py
57 58 59 60 61 62 |
|
ErrorRevisePeriods()
Bases: Exception
Error in the number of periods given.
Source code in cybersyn/optimize.py
47 48 49 50 51 |
|
InfeasibleProblem(iter_)
Bases: Exception
Exception raised for infeasible LP problems in the input salary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iter_
|
int
|
Current iteration of the linear programming algorithm. |
required |
Source code in cybersyn/optimize.py
35 36 37 38 39 40 41 |
|
OptimizePlan(periods, horizon_periods, revise_periods, economy, ecology=None)
Given the data for an economy, create the desired constraints and calculate the desired production for the upcoming years using linear programming and receding horizon control.
To plan an economy, we need to solve the following linear programming problem
subject to different constraints:
-
The activity of the production units must be positive at each period,
-
More is produced than it is consumed,
-
Trade balance is positive after a certain number of periods,
Parameters:
Name | Type | Description | Default |
---|---|---|---|
periods
|
int
|
The number of periods to actually plan (discarding the horizon). |
required |
horizon_periods
|
int
|
The number of periods to plan in each iteration. |
required |
revise_periods
|
int
|
The number of periods after which to revise a plan. |
required |
economy
|
EconomicPlan
|
The economy, which contains supply-use tables, import prices... |
required |
Attributes:
Name | Type | Description |
---|---|---|
periods |
int
|
The number of periods to actually plan (discarding the horizon). For example, we may want to plan the production for the next 4 years. |
horizon_periods |
int
|
The number of periods to plan in each iteration. For example, we may want to use a horizon of 6 years. |
revise_periods |
int
|
The number of periods after which to revise a plan. For example, if we planned a horizon of 6 years and we choose to revise the plan after 2 years, we discard the resting 4 years and plan again. |
economy |
EconomicPlan
|
The economy, which contains supply-use tables, import prices... |
worked_hours |
list[NDArray]
|
Total worked hours in each period. |
planned_activity |
list[NDArray]
|
The planned activity for the production units in each period. |
planned_production |
list[NDArray]
|
The planned production for each product in each period. |
planned_surplus |
list[NDArray]
|
The surplus production at the end of each period. |
export_deficit |
list[NDArray]
|
The export deficit at the end of each period. |
activity |
list[Variable]
|
The activity variables of our LP problem, which correspond to the level of activation of each production unit. |
total_import |
list[Variable]
|
The final imported products (variables) of our LP problem, which correspond to the level of activation of each production unit. |
Source code in cybersyn/optimize.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
__call__(target_economy, target_ecology=None, init_surplus=None, init_export_deficit=0)
Optimize the plan over the specified periods and horizon.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
init_surplus
|
NDArray
|
The surplus production at the initial time period. Defaults to None. |
None
|
init_export_deficit
|
float
|
The export deficit at the initial time period. Defaults to None. |
0
|
Source code in cybersyn/optimize.py
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 |
|
cost(period)
Create the cost function to optimize and save the total worked hours in each period. Args: period (int): current period of the optimization. Returns: Variable: Cost function to optimize.
Source code in cybersyn/optimize.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
export_constraints(period, target_economy, export_deficit)
We must export more than we import at the end of the horizon.
Note
If we don't force a positive deficit at the end of the revise period, we will have an ever increasing export deficit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period
|
int
|
current period of the optimization. |
required |
export_deficit
|
float
|
The export deficit at the end of each period. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[Constraint]
|
Export constraints. |
Source code in cybersyn/optimize.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
labor_realloc_constraint(period)
This constraint limits the reallocation of labor from one period to the next. For example, one cannot turn all farmers into train manufacturers in one year.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period
|
int
|
current period of the optimization. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[Constraint]
|
Labor reallocation constraints. |
Source code in cybersyn/optimize.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
optimize_period(period, target_economy, target_ecology, surplus, export_deficit)
Optimize one period of the plan.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period
|
int
|
current period of the optimization. |
required |
surplus
|
NDArray
|
The surplus production at the end of each period. |
required |
export_deficit
|
float
|
The export deficit at the end of each period. |
required |
Raises:
Type | Description |
---|---|
InfeasibleProblem
|
Exception raised for infeasible LP problems in the input salary. |
Source code in cybersyn/optimize.py
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 |
|
pollutants_constraint(period, target_ecology)
Maximum pollution allowed.
Source code in cybersyn/optimize.py
338 339 340 341 342 343 344 345 346 347 |
|
production_constraints(period, target_economy, surplus)
We must produce more than the target output,
Parameters:
Name | Type | Description | Default |
---|---|---|---|
period
|
int
|
current period of the optimization. |
required |
surplus
|
NDArray
|
the surplus production at the end of each period. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list[Constraint]
|
Production meets target constraints. |
Source code in cybersyn/optimize.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|