Hi,
I am planning to extend JuMP @variable macro by defining a new struct to replace the VariableInfo struct defined in JuMP, the code below is following the extension page:
struct My_var <: JuMP.AbstractVariable
bound::Vector{Real}
end
function JuMP.build_variable(
_err::Function,
tb::Vector{Real},
::Type{My_var};
kwargs...
)
println("Can also use $kwargs here.")
return My_var(tb)
end
function JuMP.add_variable(
model::JuMP.Model,
duplicate::My_var,
name::String,
)
a = JuMP.add_variable(
model,
duplicate,
"$(name)_a",
)
b = JuMP.add_variable(
model,
duplicate,
"$(name)_b",
)
return (a, b)
end
m = JuMP.Model()
JuMP.@variable(m, x[i=1:2], variable_type = Dy_var)
In the example, the user-defined struct only has a field with the type of VariableInfo, but I want to use my own type such as the struct My_var in the above code, and in the add_variable() function I am directly putting the My_var type as the argument, since I also do not want to use JuMP.ScalarVariable shown in the example, but this error always appears:
ERROR: LoadError: `@variable(m, x[i = 1:2], variable_type = Dy_var)`: Unrecognized positional arguments: (Main.My_var,). (You may have passed it as a positional argument, or as a keyword value to `variable_type`.)
If you're trying to create a JuMP extension, you need to implement `build_variable`. Read the docstring for more details.
May I ask if I can only use fields with VariableInfo and not my own types? If not then what have I done wrong? Thank you.