Sí, hay un par de maneras de hacer esto. En primer lugar, si el conjunto que desea índice a es la ordenada (que es el predeterminado), puede utilizar la first
, last
y prev
métodos en el conjunto de diversas maneras. (ver mis ediciones a su código de abajo)
En segundo lugar, usted siempre puede construir su propio subconjunto y poner en el modelo o no. El segundo modelo que a continuación se muestra la construcción de una forma arbitraria complicado subconjunto y la pone en el modelo. Este conjunto puede ser utilizado como base para un objetivo o restricción.
Esta solución es similar a esta respuesta
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()
Rendimientos:
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
soluciona mi problema. Hay un lugar en internet donde uno puede aprender de esto? En el pyomo documentaciónprevw
ni siquiera se menciona (o al menos yo no lo he visto)