0

I have a simple math prog that I am trying to solve:

m = Model(solver=MosekSolver())
@variable(m, x[1:8] >= 0)
@objective(m,Min,sum(x))
@constraint(m,A*x .== given)
@constraint(m, x, sum(x)==1)

status = solve(m)

println("x = ", getvalue(x))

A is some matrix with type Array{Float64,2

The line:

@constraint(m, x, sum(x)==1))

Changes the type of x from Array{JuMP.Variable,1} to JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}}}.

  1. Since x has been previously declared as a variable shouldn't the type remain the same? (Furthermore if the above line is executed, everything still works, but, getvalue will not due to the change in type.)

  2. Is there a way to add the summation constraint without changing the type of x

puhniste
  • 1
  • 1
  • 2

2 Answers2

1

refer to JuMP documentation:

Constraint References

In order to manipulate constraints after creation, it is necessary to maintain a reference. The simplest way to do this is to use the special three-argument named constraint syntax for @constraint, which additionally allows you to create groups of constraints indexed by sets analogously to @variable

So the way JuMP works is as expected, why not @constraint(m, anothersymbol, sum(x)==1)?

Reza Afzalan
  • 5,646
  • 3
  • 26
  • 44
0

make it like so

@constraint(m, constr, A*x .== given)

@constraint(m, constr2, sum(x) == 1)
isebarn
  • 3,812
  • 5
  • 22
  • 38