I’m trying to use Optimization.jl to solve a problem, but a I dont know how to define the problem in a way the package needs it. In my problem I have an Array of a Struct with several parameters and for each element of the Array I need the proper index of a field that is a StepRange type.
I’ll write here a smaller version of my struct and equation. Any help or hint is really appreciated, because I dont know even how to start it. Thanks in advance!!!
#Struct with problem values - needed an Array of it
struct L
a::Float64
b::Float64
s::StepRange{Time,Minute}
t1::Time
t2::Time
t3::Time
L() = new()
function L(a,b,t1,t2,t3)
s = t1:t2:t3
new(a,b,s,t1,t2,t3)
end
end
#GOAL is find all values of x that minimize this function
function Eval_EQ_SUM(H::Array{L,1},x::Array{Int64,1})
if length(H) ≠ length(x)
@error "H and x should have the same length"
return nothing
end
S = Vector{Float64}(undef,length(H))
for i in eachindex(H)
S[i] = H[i].a*EQ(H[i].s[x[i]]), H[i].b)
end
return sum(S)
end
#auxiliar function for better reading
function EQ(t::time, b::Float64)
if t < Time(2)
F = 2*b
elseif t > Time(4) && t < Time(12)
F = sqrt(b)
else
F = b^3
end
return F
end
Yes, values of x[i] are index to a range H[i].s and its values should be restricted to bounds of the vector H[i].s.
I’m not an expert in optimization at all but i think using integer variables should be easy to solve the problem. Past colleagues used Genetic Algorithm and PSO to solve MIN the equation, but the programing interface was Matlab/Python. Thats why Im looking into Optimization.jl
Well, as I said, I’m not an expert in optimization.
I’m just thinking, ‘x’ is integer so it should be an integer programming problem.
If it could help, I can replace H[i].s[x[i]] for a variable t[i], typeof(t) = Vector{Time}
My original problem is a schedule type. I should position the task within an window of time (t1:t3) in a way it minimize a not continuous function based on the schedule point
Here are some link for the papers I’m trying to reproduce the base model.
The image above also have the main equation I’m trying to work with. As you could see its a bit more complicated system than the one I wrote in the initial post. But I’m sure if I could model the function Eval_EQ_SUM, I could model the complete system.
I found a way to eliminate the inner Sum, but to do that I should call a function with some if/else logic. Dont see how could i put it in a single statemente like the one in examples (2x + 3y)
I’ll do some tests now with JuMP.jl. Its a way more clear in its tutorial “who goes where”.
In/with JuMP.jl, can I do something like this ???
function f(a)
if a > 10
return 2*a
elseif a < 4
return 3*a
else
return a^2
end
end
@objective(model, Min, f(x) + 20y)
It depends on the rest of the problem structure. But that objective function probably doesn’t make sense, because it is discontinuous.
If you mean something like your EQ function, is t a decision variable? If it is not, then yes.
I’d start by providing a reproducible example of what you’re trying to do. Use the f(a) syntax for now, even if JuMP complains and gives an error. Once things are started, people can probably point you in the right direction.
The function I must handle its discontinuous. As I said before its something realy like f(a) in the last post. It allow me to cut of about 80% memory allocation and its about 10x faster than the original formulation. So I’ll keep trying on.
Also vector t is my decision variable.
But I’ll try to do like you said, and bring on the real problem.