Da, există câteva modalități de a face acest lucru. În primul rând, dacă setul pe care doriți să indice în care este comandat (care este implicit), puteți utiliza first
, last
și prev
metodele pe platou în diverse moduri. (a se vedea meu modificări la codul de mai jos)
Al doilea rând, întotdeauna puteți construi propriile subset și nici loc în model sau nu. Al doilea model de mai jos prezintă construcția unui arbitrar complicat subset și pune-l în model. Acest set poate fi folosit ca baza pentru un obiectiv sau constrângere.
Această soluție este similară cu acest raspuns
import pyomo.environ as pe
solver = pe.SolverFactory('glpk')
model = pe.ConcreteModel('Test')
model.times = pe.Set(initialize=list(range(5)), ordered=True) # ordered is default, this is for clarity...
model.buses = pe.Set(initialize=list(range(2)))
model.SOC = pe.Var(model.times*model.buses, domain=pe.PositiveReals)
def example_rule(model):
return sum(sum(model.SOC[t+1, b] - model.SOC[t, b] for b in model.buses) for t in model.times if t != model.times.last())
model.obj = pe.Objective(rule=example_rule, sense=pe.maximize)
model.pprint()
# making your own subset...
times = 10
model2 = pe.ConcreteModel("other")
model2.times = pe.Set(initialize=range(times))
# make a subset of the even values that are no more than 4 values close to the end....
model2.times_subset = pe.Set(initialize=[t for t in model2.times if t%2==0 and t <= times-4])
model2.pprint()
Randamente:
3 Set Declarations
SOC_index : Size=1, Index=None, Ordered=True
Key : Dimen : Domain : Size : Members
None : 2 : times*buses : 10 : {(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1), (4, 0), (4, 1)}
buses : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 2 : {0, 1}
times : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 5 : {0, 1, 2, 3, 4}
1 Var Declarations
SOC : Size=10, Index=SOC_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
(0, 0) : 0 : None : None : False : True : PositiveReals
(0, 1) : 0 : None : None : False : True : PositiveReals
(1, 0) : 0 : None : None : False : True : PositiveReals
(1, 1) : 0 : None : None : False : True : PositiveReals
(2, 0) : 0 : None : None : False : True : PositiveReals
(2, 1) : 0 : None : None : False : True : PositiveReals
(3, 0) : 0 : None : None : False : True : PositiveReals
(3, 1) : 0 : None : None : False : True : PositiveReals
(4, 0) : 0 : None : None : False : True : PositiveReals
(4, 1) : 0 : None : None : False : True : PositiveReals
1 Objective Declarations
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : SOC[1,0] - SOC[0,0] + SOC[1,1] - SOC[0,1] + SOC[2,0] - SOC[1,0] + SOC[2,1] - SOC[1,1] + SOC[3,0] - SOC[2,0] + SOC[3,1] - SOC[2,1] + SOC[4,0] - SOC[3,0] + SOC[4,1] - SOC[3,1]
5 Declarations: times buses SOC_index SOC obj
2 Set Declarations
times : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 10 : {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
times_subset : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 4 : {0, 2, 4, 6}
2 Declarations: times times_subset
[Finished in 553ms]
prevw
rezolvă problema mea. Există un loc pe internet unde se pot afla acest lucru? În pyomo documentațiaprevw
nu este nici măcar menționat (sau cel puțin eu nu l-am vazut)