-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyomo_example.py
52 lines (45 loc) · 1.29 KB
/
pyomo_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 8 23:39:24 2019
@author: changlinli
"""
from pyomo.environ import ConcreteModel,AbstractModel,Param,RangeSet,Set,BuildAction,Var,Objective,Piecewise,minimize,value
from pyomo.environ import NonNegativeReals,Integers,Binary,PositiveIntegers
from pyomo.opt import SolverStatus,TerminationCondition
from pyomo.opt import SolverFactory
v={}
v[1,1] = 9
v[2,2] = 16
v[3,3] = 25
model = ConcreteModel()
model.A = RangeSet(1,3)
model.B = RangeSet(1,3)
model.P = Param(model.A, model.B)
model.S = Param(model.A, model.B, initialize=v, default=0)
def s_validate(model, v, i):
return v > 3.14159
model.S = Param(model.A, validate=s_validate)
def s_init(model, i, j):
if i == j:
return i*i
else:
return 0.0
model.S1 = Param(model.A, model.B, initialize=s_init)
model = ConcreteModel()
model.A = Set(initialize=[1,2,3])
def s_validate(model):
for ele in model:
if ele < 3.14159:
return False
return True
model.s = Param(model.A, validate=False)
model = AbstractModel()
model.A = Set(initialize=[1,2,3])
def s_validate(model):
for ele in model:
if ele < 3.14159:
return False
return True
model.s = Param(model.A, validate=s_validate)
instance = model.create_instance()