1
$\begingroup$

I am learning how to do optimization with a variable number of variables in Mathematica. I tried to write one experiment that I need to do but I have failed to get even a stripped-down version working. The code is the following

y = {1, 2, 3, 4, 5}
Maximize[{Sum[Indexed[y, i]/Indexed[x, i], {i, 1, 5}], 
    ForAll[i,Indexed[y, i]^(1/4) <= Indexed[x, i] <= Indexed[y, i]^(1/2)]}, 
    x ∈ Vectors[5, Reals]]

But it says

Maximize::ivar: x ∈ Vectors[5, Reals] is not a valid variable.

I have tried to re-write the optimization problems in different ways but nothing helped. Any advice on what I am doing wrong would be greatly appreciated.

$\endgroup$
4
$\begingroup$

I think there are two issues. One, the constraint using ForAll doesn't work, and two, Vectors[5, Reals] is not a region:

RegionQ[Vectors[5, Reals]]

False

So, one idea is to use VectorLessEqual instead of ForAll, and to use FullRegion[5] as the region:

Maximize[
    {y . (1/x), VectorLessEqual[{y^(1/4), x}] && VectorLessEqual[{x, y^(1/2)}]}, 
    x ∈ FullRegion[5]
]

{1 + 2 Sqrt[2] + 2^(3/4) + 3^(3/4) + 5^( 3/4), {x -> {1, 2^(1/4), 3^(1/4), Sqrt[2], 5^(1/4)}}}

$\endgroup$
3
$\begingroup$

Here's one way to write this:

y = {1, 2, 3, 4, 5};
xvec = Array[x, 5];
NMaximize[{Total[y/xvec], Thread[y^(1/4) <= xvec <= y^(1/2)]}, xvec]

{11.1334, {x[1] -> 1., x[2] -> 1.18921, x[3] -> 1.31607, x[4] -> 1.41421, x[5] -> 1.49535}}

The elements of xvec are the variables x[1], x[2], etc. The Threading is needed so that all the constraints are stated properly. It also works fine with Maximize instead of NMaximize:

Maximize[{Total[y/xvec], Thread[y^(1/4) <= xvec <= y^(1/2)]}, xvec]

{1 + 2 Sqrt[2] + 2^(3/4) + 3^(3/4) + 5^(3/4), 
 {x[1] -> 1, x[2] -> 2^(1/4), x[3] -> 3^(1/4), x[4] -> Sqrt[2], x[5] -> 5^(1/4)}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.