I currently try to use this unit commitment example to build my own model with pyomo. After defining switch-on and switch-off variables I struggle to implement the following equation:Equation
The yalmip example is pretty straight forward:
for k = 2:Horizon
for unit = 1:Nunits
% indicator will be 1 only when switched on
indicator = onoff(unit,k)-onoff(unit,k-1);
range = k:min(Horizon,k+minup(unit)-1);
% Constraints will be redundant unless indicator = 1
Constraints = [Constraints, onoff(unit,range) >= indicator];
end
end
Right now I am only looking into one unit, which gives me this model.
model = ConcreteModel()
p = prices
ts = timesteps
ut = min_uptime1
model.x = Var(ts, within = Binary) #onoff
model.v = Var(ts, within = Binary) #switch_on
model.w = Var(ts, within = Binary) #switch_off
def obj_rule(model):
return sum(p[t] * model.x[t] - 0.001 * (model.v[t] + model.w[t]) for t in ts)
model.revenue = Objective(rule = obj_rule, sense = maximize)
#start-up, shut-down costs will be added
def daily_uptime_rule (model):
return sum(model.x[t] for t in ts) == 12
model.daily_uptime_rule = \
Constraint(rule = daily_uptime_rule)
def switch_on(model, t):
if t == ts[0]:
return model.v[t] >= 1 - (1 - model.x[t])
else:
return model.v[t] >= 1 - model.x[t-1] - (1 - model.x[t])
model.switch_on = \
Constraint(ts, rule = switch_on)
def switch_off(model, t):
if t == ts[23]:
return model.w[t] >= model.x[t]
else:
return model.w[t] >= 1 - model.x[t+1] + (model.x[t] - 1)
model.switch_off = \
Constraint(ts, rule = switch_off)
def min_ut(model, t):
a = list(range(t, (min(ts[23], t+ut-1)+1)))
for i in a:
return model.x[i] >= model.v[t]
model.min_ut = \
Constraint(ts, rule = min_ut)
My problem here is, that i can't access the variable x the same way in pyomo. For every timestep t we need constraints for t+1, t+2, .. t+min_up -1, but I can't use ranges with variables (model.x). Can I use the yalmip example in pyomo or do i need a new formulation?
Ok, so it seems the fundamental issue here is that the index of summation that you would like to do is dependent on the RHS of the inequality. You can construct the indices of the summation in a couple ways. You just need to be careful that the values you construct are valid. Here is an idea that might help you. This toy model tries to maximize the sum of x[t], but limits x[t] <= x[t-1] + x[t-2] just for giggles. Note the construction of the summation range "on the fly" from the passed value of t:
from pyomo.environ import *
m = ConcreteModel()
m.t = Set(initialize=range(5))
m.x = Var(m.t)
# constrain x_t to be less than the sum of x_(t-1), x_(t-2)
def x_limiter(m, t):
if t==0:
return m.x[t] <= 1 # limit the first value
# upperlimit on remainder is sum of previous 2
return sum(m.x[i] for i in range(t-2, t) if i in m.t) >= m.x[t]
m.c1 = Constraint(m.t, rule=x_limiter)
# try to maximize x
m.OBJ = Objective(expr=sum(m.x[t] for t in m.t), sense=maximize)
solver = SolverFactory('glpk')
m.pprint()
solver.solve(m)
m.display()
It gets the job done:
Variables:
x : Size=5, Index=t
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : None : 1.0 : None : False : False : Reals
1 : None : 1.0 : None : False : False : Reals
2 : None : 2.0 : None : False : False : Reals
3 : None : 3.0 : None : False : False : Reals
4 : None : 5.0 : None : False : False : Reals
Objectives:
OBJ : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 12.0
This recent post also has a similar idea:
Pyomo creating a variable time index
Related
I want to adjust the lower and upper bound of three pyo.Var() depending on the outcome of another variable.
the variable model.inverter_power should be in the i-th iteration bigger than model.inverter_power[i].lb + model.fcr_power[i] but also smaller than model.inverter_power[i].ub - model.fcr_power[i])
How can i implement this? Unfortuntately my idea is not working....
def fcr_inverter_reduction(model, i):
return (model.inverter_power[i] >= model.inverter_power[i].lb + model.fcr_power[i],
model.inverter_power[i] <= model.inverter_power[i].ub - model.fcr_power[i])
model.fcr_inverter_rule = pyo.Constraint(model.i, rule = fcr_inverter_reduction)
I tried various versions of this code, not only is this code not linear anymore, so I used ipopt as a solver but no solution can be found, i got this error message:
File "D:\.conda\envs\PythonEnviromentV2\lib\site-packages\pyomo\opt\base\solvers.py", line 596, in solve
raise ApplicationError(
pyomo.common.errors.ApplicationError: Solver (ipopt) did not exit normally
This is very doable if you reformulate just a bit. Also, I don't think it is possible to return a tuple of 2 constraints like you are doing with that function, so you should break it up... it is clearer as well.
You probably could access the upper/lower bound and use that within the constraint because they are fixed/constant with respect to the solver, but I think it is probably clearer to break out your min/max values as parameters. Some variation of this works.
(also, in the future, you are more likely to get better help/results if you post a fully minimal-reproducible example instead of just 1-line.)
Code:
import pyomo.environ as pyo
model = pyo.ConcreteModel()
model.I = pyo.Set(initialize=[1,2,3])
# power parameters...
model.min_inverter = pyo.Param(model.I, initialize={1:10, 2:15, 3:22})
model.max_inverter = pyo.Param(model.I, initialize={1:55, 2:45, 3:80})
# vars...
model.inverter_power = pyo.Var(model.I)
model.fcr_power = pyo.Var(model.I)
def fcr_inverter_min(model, i):
return model.inverter_power[i] >= model.min_inverter[i] + model.fcr_power[i]
model.fcr_inverter_rule_min = pyo.Constraint(model.I, rule=fcr_inverter_min)
def fcr_inverter_max(model, i):
return model.inverter_power[i] <= model.max_inverter[i] - model.fcr_power[i]
model.fcr_inverter_rule_max = pyo.Constraint(model.I, rule=fcr_inverter_max)
model.pprint()
Output:
1 Set Declarations
I : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 3 : {1, 2, 3}
2 Param Declarations
max_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 55
2 : 45
3 : 80
min_inverter : Size=3, Index=I, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 10
2 : 15
3 : 22
2 Var Declarations
fcr_power : Size=3, Index=I
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : None : None : None : False : True : Reals
2 : None : None : None : False : True : Reals
3 : None : None : None : False : True : Reals
inverter_power : Size=3, Index=I
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : None : None : None : False : True : Reals
2 : None : None : None : False : True : Reals
3 : None : None : None : False : True : Reals
2 Constraint Declarations
fcr_inverter_rule_max : Size=3, Index=I, Active=True
Key : Lower : Body : Upper : Active
1 : -Inf : inverter_power[1] - (55 - fcr_power[1]) : 0.0 : True
2 : -Inf : inverter_power[2] - (45 - fcr_power[2]) : 0.0 : True
3 : -Inf : inverter_power[3] - (80 - fcr_power[3]) : 0.0 : True
fcr_inverter_rule_min : Size=3, Index=I, Active=True
Key : Lower : Body : Upper : Active
1 : -Inf : 10 + fcr_power[1] - inverter_power[1] : 0.0 : True
2 : -Inf : 15 + fcr_power[2] - inverter_power[2] : 0.0 : True
3 : -Inf : 22 + fcr_power[3] - inverter_power[3] : 0.0 : True
I am new to Julia and trying to understand how things work.
Below is the sample code I just wrote.
(This is the baseline code and I am planning to add other lines one by one.)
I expected to see something like 1 2 3 4 5 6 7... from test = check(m)
However, I don't see any result.
Any help will be very much appreciated.
using Pkg
using Optim
using Printf
using LinearAlgebra, Statistics
using BenchmarkTools, Optim, Parameters, QuantEcon, Random
using Optim: converged, maximum, maximizer, minimizer, iterations
using Interpolations
using Distributions
using SparseArrays
using Roots
# ================ 1. Parameters and Constants ============================
mutable struct Model
# Model Parameters and utility function
δ::Float64
function Model(;
δ = 0.018,
)
new(
δ
)
end
end
function check(m)
it = 0
tol=1e-8
itmax = 1000
dif = 0
# Iteration
while it < itmax && dif >=tol
it = it + 1;
V = Vnew;
println(it)
end
return itmax
end
m=Model()
test = check(m)
dif = 0
tol = 1e-8
while it < itmax && dif >= tol
Now explain to me how
dif >= tol
Is there a way to set multiple indexes to a set value within one constraint without having to type out the same variable for each indexed time. I have provided an example, imagine that you want to optimize when to charge your electric vehicle but you don't want it to charge at certain hours in a day. The example below works to avoid charging at the 4th and 5th hour. However, what if I want it to not charge for 15 hours of the day but don't feel like writing m.EVcharge[0]+m.EVcharge[1]+... putting m.EVcharge[:15] == 0 will not work because the constraints don't handle slices that well in pyomo.
def time_rule(m):
return m.EVcharge[4]+m.EVcharge[5] == 0
m.time_rule = Constraint(time, rule=time_rule)
Yes. There are a variety of ways to do this. You could make a subset of m.time and only pass that subset in to the constraint rule, which would constrain them to zero, or sum across the subset and make constrain it to zero (both assume negative charging is not possible.)
Or, you could do it more cleanly with data or a parameter that holds the limit for any arbitrary time block and use that, which keeps the data separate from the model, which is generally a good idea...
import pyomo.environ as pyo
# some background data on limits...
use_limit = { 0:3, # limited juice avial
1:3,
2:0, # no juice avail. :)
3:0}
m = pyo.ConcreteModel('EV Charge')
m.T = pyo.Set(initialize=range(6))
m.EV_charge = pyo.Var(m.T, domain=pyo.NonNegativeReals)
# Constraints
def charge_limit(m, time):
return m.EV_charge[time] <= use_limit[time]
m.C1 = pyo.Constraint(use_limit.keys(), rule=charge_limit)
m.pprint()
Yields:
2 Set Declarations
C1_index : Size=1, Index=None, Ordered=False
Key : Dimen : Domain : Size : Members
None : 1 : Any : 4 : {0, 1, 2, 3}
T : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 6 : {0, 1, 2, 3, 4, 5}
1 Var Declarations
EV_charge : Size=6, Index=T
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : None : None : False : True : NonNegativeReals
1 : 0 : None : None : False : True : NonNegativeReals
2 : 0 : None : None : False : True : NonNegativeReals
3 : 0 : None : None : False : True : NonNegativeReals
4 : 0 : None : None : False : True : NonNegativeReals
5 : 0 : None : None : False : True : NonNegativeReals
1 Constraint Declarations
C1 : Size=4, Index=C1_index, Active=True
Key : Lower : Body : Upper : Active
0 : -Inf : EV_charge[0] : 3.0 : True
1 : -Inf : EV_charge[1] : 3.0 : True
2 : -Inf : EV_charge[2] : 0.0 : True
3 : -Inf : EV_charge[3] : 0.0 : True
Suppose x,y,z are int variables and A is a matrix, I want to express a constraint like:
z == A[x][y]
However this leads to an error:
TypeError: object cannot be interpreted as an index
What would be the correct way to do this?
=======================
A specific example:
I want to select 2 items with the best combination score,
where the score is given by the value of each item and a bonus on the selection pair.
For example,
for 3 items: a, b, c with related value [1,2,1], and the bonus on pairs (a,b) = 2, (a,c)=5, (b,c) = 3, the best selection is (a,c), because it has the highest score: 1 + 1 + 5 = 7.
My question is how to represent the constraint of selection bonus.
Suppose CHOICE[0] and CHOICE[1] are the selection variables and B is the bonus variable.
The ideal constraint should be:
B = bonus[CHOICE[0]][CHOICE[1]]
but it results in TypeError: object cannot be interpreted as an index
I know another way is to use a nested for to instantiate first the CHOICE, then represent B, but this is really inefficient for large quantity of data.
Could any expert suggest me a better solution please?
If someone wants to play a toy example, here's the code:
from z3 import *
items = [0,1,2]
value = [1,2,1]
bonus = [[1,2,5],
[2,1,3],
[5,3,1]]
choices = [0,1]
# selection score
SCORE = [ Int('SCORE_%s' % i) for i in choices ]
# bonus
B = Int('B')
# final score
metric = Int('metric')
# selection variable
CHOICE = [ Int('CHOICE_%s' % i) for i in choices ]
# variable domain
domain_choice = [ And(0 <= CHOICE[i], CHOICE[i] < len(items)) for i in choices ]
# selection implication
constraint_sel = []
for c in choices:
for i in items:
constraint_sel += [Implies(CHOICE[c] == i, SCORE[c] == value[i])]
# choice not the same
constraint_neq = [CHOICE[0] != CHOICE[1]]
# bonus constraint. uncomment it to see the issue
# constraint_b = [B == bonus[val(CHOICE[0])][val(CHOICE[1])]]
# metric definition
constraint_sumscore = [metric == sum([SCORE[i] for i in choices ]) + B]
constraints = constraint_sumscore + constraint_sel + domain_choice + constraint_neq + constraint_b
opt = Optimize()
opt.add(constraints)
opt.maximize(metric)
s = []
if opt.check() == sat:
m = opt.model()
print [ m.evaluate(CHOICE[i]) for i in choices ]
print m.evaluate(metric)
else:
print "failed to solve"
Turns out the best way to deal with this problem is to actually not use arrays at all, but simply create integer variables. With this method, the 317x317 item problem originally posted actually gets solved in about 40 seconds on my relatively old computer:
[ 0.01s] Data loaded
[ 2.06s] Variables defined
[37.90s] Constraints added
[38.95s] Solved:
c0 = 19
c1 = 99
maxVal = 27
Note that the actual "solution" is found in about a second! But adding all the required constraints takes the bulk of the 40 seconds spent. Here's the encoding:
from z3 import *
import sys
import json
import sys
import time
start = time.time()
def tprint(s):
global start
now = time.time()
etime = now - start
print "[%ss] %s" % ('{0:5.2f}'.format(etime), s)
# load data
with open('data.json') as data_file:
dic = json.load(data_file)
tprint("Data loaded")
items = dic['items']
valueVals = dic['value']
bonusVals = dic['bonusVals']
vals = [[Int("val_%d_%d" % (i, j)) for j in items if j > i] for i in items]
tprint("Variables defined")
opt = Optimize()
for i in items:
for j in items:
if j > i:
opt.add(vals[i][j-i-1] == valueVals[i] + valueVals[j] + bonusVals[i][j])
c0, c1 = Ints('c0 c1')
maxVal = Int('maxVal')
opt.add(Or([Or([And(c0 == i, c1 == j, maxVal == vals[i][j-i-1]) for j in items if j > i]) for i in items]))
tprint("Constraints added")
opt.maximize(maxVal)
r = opt.check ()
if r == unsat or r == unknown:
raise Z3Exception("Failed")
tprint("Solved:")
m = opt.model()
print " c0 = %s" % m[c0]
print " c1 = %s" % m[c1]
print " maxVal = %s" % m[maxVal]
I think this is as fast as it'll get with Z3 for this problem. Of course, if you want to maximize multiple metrics, then you can probably structure the code so that you can reuse most of the constraints, thus amortizing the cost of constructing the model just once, and incrementally optimizing afterwards for optimal performance.
Say I have a List of records in elm:
[ { id = 1, magnitude = 100 }
, { id = 3, magnitude = 300 }
, { id = 2, magnitude = 200 } ]
and I want to get the record with the greatest magnitude value (300). What is a good way of doing this?
The docs gives an example of using the "maximum" -method, but it uses a simple list of integers. How is it done with records?
Update based on recommendation from #robertjlooby
There is a function called maximumBy which does exactly this in elm-community/list-extra. Example:
List.Extra.maximumBy .magnitude list
Original Answer
There are a few ways to achieve this.
This first way is more concise but it involves sorting the whole list, reversing it, then taking the head.
maxOfField : (a -> comparable) -> List a -> Maybe a
maxOfField field =
List.head << List.reverse << List.sortBy field
If you want something that's more efficient and only traverses the list once, here's a more efficient version:
maxOfField : (a -> comparable) -> List a -> Maybe a
maxOfField field =
let f x acc =
case acc of
Nothing -> Just x
Just y -> if field x > field y then Just x else Just y
in List.foldr f Nothing
An example of it in use:
list =
[ { id = 1, magnitude = 100 }
, { id = 3, magnitude = 300 }
, { id = 2, magnitude = 200 } ]
main =
text <| toString <| maxOfField .magnitude list
Here is a version that uses foldl and a default record:
bigger =
let
choose x y =
if x.magnitude > y.magnitude then
x
else
y
in
List.foldl choose {id = 0, magnitude = 0} items
Sebastian's answer add an arbitrary start value which could cause a problem if all your magnitudes were negative. I would adjust to
bigger items =
case items of
[] -> []
(h :: []) -> h
(h :: tail) ->
let
choose x y =
if x.magnitude > y.magnitude then
x
else
y
in
List.foldl choose h tail