What does this error message mean? From Convex.jl: Cannot `convert` an object of type EqConstraint

Hi, I ran into the following error and could not find help from reading the documentation.

The error is

MethodError: Cannot `convert` an object of type EqConstraint to an object of type Array{Constraint,N} where N

My code is:

h = 1.
g = 0.1
m = 10.
Fmax = 10.
p0 = [50, 50, 100]
v0 = [-10, 0, -10]
alpha = 0.5
gamma = 1.
K = 35;
using Convex, Mosek
solver = MosekSolver()
p = Variable(3, K+1)
v = Variable(3, K+1)
f = Variable(3, K)
problem = minimize(sum(gamma .* [Convex.norm(f[:, i]) for i in 1:K])) 
constraint1 = p[:, 1] == p0
constraint2 = p[:, K+1] == zeros(3)
constraint3 = v[:, 1] == v0
constraint4 = v[:, K+1] == zeros(3)
constraint5 = p[:, 2:(K+1)] == p[:, 1:K] + (h / 2) * (v[:, 1:K] + v[:, 2:(K+1)])
constraint6 = v[:, 2:(K+1)] == v[:, 1:K] + (h / m) .* f[:, 1:K] - (h * g) .* transpose(repeat([0 0 1], K))
constraint7 = [Convex.norm(f[:, i]) for i in 1:K] .≤ Fmax .* ones(K)
constraint8 = [p[3, i] ≥ alpha *  Convex.norm(p[1:2, i]) for i in 1:(K+1)]
problem.constraints = constraint1
problem.constraints += [
    constraint2, constraint3, constraint4, 
    constraint5, constraint6, constraint7, constraint8
]
solve!(problem, solver)

I think the issue is that some of those are individual constraints, and others are vectors of constraints. I usually do something like

constrs = Constraint[] # initialize a vector of constraints
push!(constrs, x >= 0) # add a single constraint
append!(constrs, vector_of_constraints) # add a vector of constraints
...
problem = minimize(objective, constrs)

Of course, you can do append!(problem.constraints, vector_of_constraints) too, or populate them in a loop, etc.

By the way, I am thinking we should remove the overload for problem.constraints += constraint because I think it’s nicer if move towards treating Convex.jl objects more like regular Julia objects, and stick to the usual ways of populating vectors etc.

Thank you very much! This solves my problem.

1 Like