GEKKO optimization for each datapoint in the csv

22 Views Asked by At

So I have an objective function in the format

3*[var_1] + [ROW_MEAN_hpbh]*[var_2] + [MAX_feia] * [var_3]

where

[ROW_MEAN_hbph] is the row mean of 2 columns
[MAX_feia] is the max value of another column of the dataset


[var_1] [var_2] [var_3] are varibales with upper bound and lower bound defined 

I need an expression parser where it can read the variables

1

There are 1 best solutions below

0
John Hedengren On

The question needs some additional clarification, but here is an example script that possibly meets the objectives of the question. Please clarify, if needed.

import numpy as np
import pandas as pd
from gekko import GEKKO

# Example dataset
data = {
    'col1': [1, 2, 3],
    'col2': [4, 5, 6],
    'col_feia': [7, 8, 9]
}
df = pd.DataFrame(data)

# Calculate ROW_MEAN_hbph and MAX_feia
ROW_MEAN_hbph = df[['col1', 'col2']].mean(axis=1).mean()
MAX_feia = df['col_feia'].max()

# Gekko model
m = GEKKO()
var_1,var_2,var_3 = m.Array(m.Var,3,lb=1,ub=5)
m.Minimize(3*var_1 + ROW_MEAN_hbph*var_2 + MAX_feia*var_3)
m.options.SOLVER = 1
m.solve(disp=True)
print('var_1:', var_1.value[0])
print('var_2:', var_2.value[0])
print('var_3:', var_3.value[0])

The solution is the lower bound for all variables, but this may change if you update with your own data.

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
    1  1.55000E+01  0.00000E+00
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.018999999999999996 sec
 Objective      :  15.5
 Successful solution
 ---------------------------------------------------

var_1: 1.0
var_2: 1.0
var_3: 1.0

If you need to have the equations applied to each row of the data, then switch to parameter regression mode with m.options.IMODE=2.