# Method Error using round on JuMP Variable

**URL:** <https://discourse.julialang.org/t/method-error-using-round-on-jump-variable/37136>\
**Category:** Optimization (Mathematical)\
**Created:** [April 7, 2020, 12:33am UTC](https://discourse.julialang.org/t/method-error-using-round-on-jump-variable/37136 "2020-04-07T00:33:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrus\_the\_man](https://avatars.discourse-cdn.com/v4/letter/c/ccd318/32.png) [@Cyrus\_the\_man](https://discourse.julialang.org/u/Cyrus_the_man)\
**Post date:** [April 7, 2020, 12:33am UTC](https://discourse.julialang.org/t/method-error-using-round-on-jump-variable/37136/1 "2020-04-07T00:33:41Z")

</div>

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

# run\_mpc() takes the robot’s current position and velocity

# and returns an optimized trajectory of position, velocity,

# 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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 7, 2020, 1:15am UTC](https://discourse.julialang.org/t/method-error-using-round-on-jump-variable/37136/2 "2020-04-07T01:15:37Z")

</div>

Please see the first post of [Please read: make it easier to help you - #8](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/8). In particular, provide a minimal working example and use the tools to format your code.

You should also read the docs on NLP: [Nonlinear Modeling · JuMP](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 [GitHub - RoboticExplorationLab/TrajectoryOptimization.jl: A fast trajectory optimization library written in Julia](https://github.com/RoboticExplorationLab/TrajectoryOptimization.jl)
