# Method Error using round on JuMP Variable

MethodError: no method matching round(::Type{Int64}, ::GenericAffExpr{Float64,VariableRef})

I’m using Julia 1.3.1 on Jupyter Notebooks and have encountered a method error using an Ipop solver.

Within dynamic constraints I call a function with an input of one of the JuMP variables. My function uses round and as a result I believe that the error is implying I cannot round my JuMP variable to integer. Is this correct thinking? and if so are their any solutions. I have including my solver function and the function I call on below. Thank you so much:

function vectorfield(x)
Integer1=round(Int,(51-50*(x[2,1])))
Integer2=round(Int,(51+50*(x[1,1])))
force_1_s=U[Integer1,Integer2]
force_2_s=V[Integer1,Integer2]
return [force_1_s; force_2_s]
end

solver = IpoptSolver(print_level=0)
t_true=0.1
i=2
j=1
num_time_steps = 20

# and acceleration.

function run_mpc(initial_position, initial_velocity,initial_Force_A)

``````model = Model((Ipopt.Optimizer))

t_true=0.1
num_time_steps = 10
max_acceleration = 5
x= [0.3;0.3]

@variables model begin
position[1:2, 1:num_time_steps]
velocity[1:2, 1:num_time_steps]
Force_A[1:2, 1:num_time_steps]
-max_acceleration <= acceleration[1:2, 1:num_time_steps] <= max_acceleration
end

# Dynamics constraints

@constraint(model, [i=2:num_time_steps, j=1:2],
Force_A[j, i] == vectorfield(position[:,i-1])[j,1] * t_true)
@NLconstraint(model, [i=2:num_time_steps, j=1:2],
velocity[j, i] == velocity[j, i - 1] + (acceleration[j, i - 1]) * t_true + Force_A[j, i-1])
@constraint(model, [i=2:num_time_steps, j=1:2],
position[j, i] == position[j, i - 1] + velocity[j, i - 1] * t_true)

# Cost function: minimize final position and final velocity
@objective(model, Min,
100 * sum(position[:, end].^2) + sum(velocity[:, end].^2))

# Initial conditions:
@constraint(model, position[:, 1] .== initial_position)
@constraint(model, velocity[:, 1] .== initial_velocity)
@constraint(model, Force_A[:, 1] .== initial_Force_A)
optimize!(model)
return JuMP.value.(position), JuMP.value.(velocity), JuMP.value.(acceleration)
``````

end

# The robot’s starting position and velocity

q = [0.3, 0.3]
v = [0.0, -1.0]
f = vectorfield(q)
b=zeros(10)
b_plan=zeros(10)
a=zeros(10)
a_plan=zeros(10)

for i in 1:80
# Plot the current position
#plot!([q[1,1]], [q[2,1]], marker=(:hex, 10), xlim=(-1.1, 1.1), ylim=(-1.1, 1.1))

``````# Run the MPC control optimization
q_plan, v_plan, u_plan = run_mpc(q, v,f)

# Draw the planned future states from the MPC optimization
plot!(q_plan[1, :], q_plan[2, :], linewidth=5)
a_plan[i]=q_plan[1,i]
a[i]=q[1,1]
b_plan[i]=q_plan[2,i]
b[i]=q[2,1]
# Apply the planned acceleration and simulate one step in time
u = u_plan[:, 1]
v += u * t_true
q += v * t_true
``````

end

Please see the first post of PSA: make it easier to help you. In particular, provide a minimal working example and use the tools to format your code.

You should also read the docs on NLP: https://www.juliaopt.org/JuMP.jl/stable/nlp/

For your specific question, there is a chain of issues: i) don’t use nonlinear functions in `@constraint` and ii) no, you can’t use `round`. You will need to think of a way to reformulate your problem to make it amenable to solvers.

You may be interested in https://github.com/RoboticExplorationLab/TrajectoryOptimization.jl

1 Like