Sì, ci sono un paio di modi per fare questo. In primo luogo, se il set che si desidera indice è ordinato (che è di default), è possibile utilizzare il first
, last
e prev
metodi sul set in vari modi. (vedi il mio modifiche al codice riportato di seguito)
In secondo luogo, si può sempre costruire il suo sottoinsieme e sia nel modello o meno. Secondo il modello di seguito mostra la costruzione di un arbitrariamente complicato sottoinsieme e la mette nel modello. Questo set può essere utilizzato come base per un obiettivo o di un vincolo.
Questa soluzione è simile a questa risposta
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()
Rendimenti:
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
risolve il mio problema. C'è un posto su internet dove si può imparare questo? Nel pyomo documentazioneprevw
non è nemmeno menzionata (o almeno io non ho visto)