Getindex in Model Constraints assign

Hello

Is there a way to use getIndex or findfirst function in Constraints in Model?
Usage: Get maximum value from “1 vector(column)”-but remember just its index, for example 8th.
Then get value from “another vector(column)” at specific index (8th)?

pseudocode
vector2[ getindex(max(vector1), vector1) ]

in model:

data = rand(14,10)
model = Model() #model = Model(Ipopt.Optimizer)

@variable(model, x1)
@variable(model, maxi)
@variable(model, inde) #@variable(model, inde,Int)

@constraints(model, begin
        [i=1:14], data[i,1] * x1 <=maxi
end)

@constraints(model, begin
#        inde==findfirst(maxi, data[1:14,1])
        inde==getindex(maxi, data[1:14,1])
end)

@objective(model, Max, data[inde,2] ) #goal

or custom function for model is needed to create?

In general you can’t index with a variable as the variable is continuous.

2 Likes

i tried
(code is working without errors, but optimum wasnt found :grin:)

using Random
using JuMP
import Ipopt
Random.seed!(1200)		


data = rand(6)	
data2 = rand(6)	

data[2]=40.1
data2[2]=123.12

model = Model(Ipopt.Optimizer)

@variable(model, x1)

f(x...) = data2[findfirst(x .== max(x...) )	]
register(model, :f, 6, f; autodiff = true)

@NLexpression(model, y[i = 1:6],data[i]*x1	)

@NLobjective(model, Max, f(y...) )


optimize!(model)
value(x1)
data2
data

solution is optimal…x1=0

but, for x=1, objective value is 123.12 (higher)
(2nd element in data is max…also 2nd element in data2 (objective) is max

6-element Array{Float64,1}:
0.6090858757801794
40.1
0.7418153587583025
0.1461819584127848
0.22809478234081726
0.7570257298492764

julia> data2
6-element Array{Float64,1}:
0.8393052531712282
123.12
0.05918180470419587
0.05660165930690897
0.3355748112808865
0.8392086371374328

You need to use a mixed-integer linear program:

using JuMP, Cbc
data = rand(14, 2)
model = Model(Cbc.Optimizer)
@variables(model, begin
    maxi
    z[1:14], Bin
end)
@constraints(model, begin
    [i=1:14], data[i, 1] <= maxi
    maxi == sum(data[i, 1] * z[i] for i = 1:14)
    sum(z) == 1
end)
@objective(model, Max, sum(data[i, 2] * z[i] for i = 1:14)

Here are some tricks: 9 Mixed integer optimization — MOSEK Modeling Cookbook 3.3.0

1 Like