lanl-ansi / MathOptAI.jl

Embed trained machine learning predictors in JuMP
https://lanl-ansi.github.io/MathOptAI.jl/
Other
29 stars 1 forks source link

GLM.jl and DataFrames.jl #27

Closed odow closed 4 months ago

odow commented 4 months ago

Our vector-in/vector-out syntax keeps things simple. But it doesn't really help real-world use-cases.

It'd be nice to have a good solution for the interaction of GLM.jl and DataFrames.

This example is cribbed from https://gurobi-machinelearning.readthedocs.io/en/stable/auto_examples/example2_student_admission.html

using JuMP
import CSV
import DataFrames
import Downloads
import GLM
import Ipopt
import MathOptAI
function read_df(filename)
    url = "https://raw.githubusercontent.com/INFORMSJoC/2020.1023/master/data/"
    data = Downloads.download(url * filename)
    return CSV.read(data, DataFrames.DataFrame)
end
historical_df = read_df("college_student_enroll-s1-1.csv")
model_glm = GLM.glm(
    GLM.@formula(enroll ~ 0 + merit + SAT + GPA),
    historical_df,
    GLM.Bernoulli(),
)
application_df = read_df("college_applications6000.csv")
n_students = size(application_df, 1)
model = Model(Ipopt.Optimizer)
application_df.merit = @variable(model, 0 <= x_merit[1:n_students] <= 2.5)
y_enroll = MathOptAI.add_predictor(model, model_glm, application_df)
@objective(model, Max, sum(y_enroll))
@constraint(model, sum(x_merit) <= 0.2 * n_students)
optimize!(model)
@assert is_solved_and_feasible(model)
value.(x_merit)