Implementing a squarewave: Can’t take remainder of InfiniteOpt.GeneralVariableRef #337
-
I have a squarewave implemented like so:
I am running an InfiniteOpt.jl optimization where I pass in
I get the error
Clearly, the So I have two options:
If #1 is do-able in a InfiniteOpt.jl compatible way, I would love to hear suggestions. Otherwise, how can I get started on #2? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @codercahol, sorry for the late response (I missed the notification for this post). Like JuMP, InfiniteOpt is a symbolic optimization language where expressions like However, since your In your case we can write: function sqw(t; period::Float64=1, duty::Float64=0.5)
return t % period < duty*period ? 1.0 : 0.0
end
model = InfiniteModel()
@infinite_parameter(model, t in [0 , T], num_supports = n)
# create a parameter function called wrapped_sqw that uses sqw with infinite parameter t as an input
@parameter_function(model, wrapped_sqw == sqw(t))
@constraint(model, ∂(x, t) == wrapped_sqw) An alternative, but equivalent syntax is: function sqw(t; period::Float64=1, duty::Float64=0.5)
return t % period < duty*period ? 1.0 : 0.0
end
model = InfiniteModel()
@infinite_parameter(model, t in [0 , T], num_supports = n)
# use parameter_function to embed sqw(t) within the constraint expression
@constraint(model, ∂(x, t) == parameter_function(sqw, t)) |
Beta Was this translation helpful? Give feedback.
Hi @codercahol, sorry for the late response (I missed the notification for this post).
Like JuMP, InfiniteOpt is a symbolic optimization language where expressions like
t % period < duty*period ? 1.0 : 0.0
are not readily supported (see https://jump.dev/JuMP.jl/stable/manual/nonlinear/#Supported-operators).However, since your
sqw
function only depends on an infinite parameter, InfiniteOpt provides a way around this limitation via the use of a modeling object called a parameter function. Parameter functions provide a way to embed an arbitrary Julia function the takes infinite parameter(s) as input and outputs a single number.In your case we can write: