hdavid16 / DisjunctiveProgramming.jl

A JuMP extension for Generalized Disjunctive Programming
MIT License
27 stars 3 forks source link

Is it possible to use this package to optimize the selection of indices for a vector? #115

Closed Datseris closed 3 months ago

Datseris commented 3 months ago

Hi there,

I just discovered this package and it re-ignited my hopes of solving https://discourse.julialang.org/t/using-a-jump-bin-vector-to-index-an-array-checkbounds-fails-with-type-variableref/102674 via JuMP.

I have the following MWE:

using DisjunctiveProgramming
import HiGHS

# example input to test it with
input = rand(100, 6) .* collect(1:6)'
# objective function
function objective(chosen::AbstractVector{Int})
    # given a vector of integers (columns to select)
    # return a real number. Not possible to write down analytic expression,
    # this is just an example
    sums = [sum(input[:, i]) for i in chosen]
    return mean(sums)
end
# start the model
model = GDPModel(HiGHS.Optimizer)
D = size(input, 2)

# Make as many logical variables as columns
@variable(model, I[1:D], Logical)
# we add a constrain that at most Fmax indicators are true
Fmax = 3
@constraint(model, I in AtMost(Fmax))

# goal: find indices, at most Fmax, where `objective`
# of these indices is maximized
# however, I have the same problem, I don't know how to extract
# an integer from `LogicalVariableRef{Model}` inside the
# `objective` function
@objective(model, Max, objective(I))

# I would hope that this works
optimize!(model, gdp_method = BigM(100, false))
print(model)

my code errors in @objective(model, Max, objective(I)) with

ERROR: MethodError: no method matching objective(::Vector{LogicalVariableRef{Model}})

Which is to be expected. I am facing the same issue as the one I posted in the aforementioned discourse thread.

The question therefore is, is it possible via DisjunctiveProgramming.jl and the usage of some disjunctions, to achieve this: optimize the selection of which columns of a matrix to choose (i.e., a vector of integers), so that when using these specific columns to compute a real number, this real number is maximized?

hdavid16 commented 3 months ago

Unfortunately no. Your objective function must be an algebraic expression of your decision variables. It has the same limitations as standard mathematical optimization.

Datseris commented 3 months ago

I suspected so, thank you very much for taking the time to reply!