I want to solve a quadratic programming problem with JuMP.
I don’t know if Ipopt
can solve it or not. I’m using HiGHS
(Although I prefer Ipopt if it’s possible). Here is the model:
\eqalign{ & \max z = \prod\limits_{i = 1}^m {b \cdot {x_i}} \cr & s.t. \cr & \sum\limits_{j = 1}^n {{b_j} = 1} \cr & 0 \le {b_j} \le 1,{\rm{ }}j \in \{ 1,2, \ldots n\} \cr}
where x_i is a vector of size n and b is a vector of size n. Here is the code:
using HiGHS
using JuMP
function example()
n = m = 3
# A matrix to store the output of each round
weights = Matrix{Float64}(undef, n, m)
# Synthetic data
data = rand(n, 10);
# Arbitrary indices
rnd_idxs = rand(1:3, 3)
# run the procedure for 3 times
for iter ∈ 1:3
# size(x) is (3, 3)
x = data[:, rnd_idxs]
# Since HiGHS supports quadratic programming, I chose it.
model = Model(HiGHS.Optimizer)
@variables(model, begin
0<=b[j=1:n]<=1
end)
@constraint(model, sum(b[j] for j = 1:n) == 1)
# Part of the objective function. x[:, i] represents the xᵢ
h = [sum(b.*x[:, i]) for i ∈ 1:m]
@NLobjective(model, Max, reduce(*, h))
optimize!(model)
# Store the output
weights[:, iter] = value.(b)
end
weights
end;
example()
But it throws the following error:
┌ Warning: Function reduce automatically registered with 2 arguments.
│
│ Calling the function with a different number of arguments will result in an
│ error.
│
│ While you can safely ignore this warning, we recommend that you manually
│ register the function as follows:
│ ```Julia
│ model = Model()
│ register(model, :reduce, 2, reduce; autodiff = true)
│ ```
└ @ MathOptInterface.Nonlinear C:\Users\Shayan\.julia\packages\MathOptInterface\4g9vU\src\Nonlinear\operators.jl:370
ERROR: Unexpected object * of type typeof(*) in nonlinear expression.
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:35
[2] parse_expression(#unused#::MathOptInterface.Nonlinear.Model, #unused#::MathOptInterface.Nonlinear.Expression, x::Function, #unused#::Int64)
@ MathOptInterface.Nonlinear C:\Users\Shayan\.julia\packages\MathOptInterface\4g9vU\src\Nonlinear\parse.jl:34
[3] parse_expression(data::MathOptInterface.Nonlinear.Model, expr::MathOptInterface.Nonlinear.Expression, x::Expr, parent_index::Int64)
@ MathOptInterface.Nonlinear C:\Users\Shayan\.julia\packages\MathOptInterface\4g9vU\src\Nonlinear\parse.jl:52
[4] parse_expression
@ C:\Users\Shayan\.julia\packages\MathOptInterface\4g9vU\src\Nonlinear\parse.jl:14 [inlined]
[5] set_objective
@ C:\Users\Shayan\.julia\packages\MathOptInterface\4g9vU\src\Nonlinear\model.jl:43 [inlined]
[6] set_nonlinear_objective(model::Model, sense::MathOptInterface.OptimizationSense, x::Expr)
@ JuMP C:\Users\Shayan\.julia\packages\JuMP\pQApG\src\nlp.jl:167
[7] macro expansion
@ C:\Users\Shayan\.julia\packages\JuMP\pQApG\src\macros.jl:2688 [inlined]
[8] example()
@ Main e:\CORN.jl:139
[9] top-level scope
@ e:\CORN.jl:150
The problem is with this line:
@NLobjective(model, Max, reduce(*, h))
How can I solve this problem?
Note that For the example above, for each iteration (iter
), the objective function can be written as:
\max z = \left( {{b_1} \times {x_{11}} + {b_2} \times {x_{12}} + {b_3} \times {x_{13}}} \right) \times \cdots \times \left( {{b_1} \times {x_{31}} + {b_2} \times {x_{32}} + {b_3} \times {x_{33}}} \right)