Using H2O Automl model in a constraint for optimization

priya Member Posts: 3

Hello! I am working on an optimization problem using NeverGrad with a H2O AutoMl model. The objective function is a linear function of decision variables which is an array of 6 scalars. The constraints for the objective function are upper and lower bounds on the value of output variables. The output variables are obtained from a machine learning model stored in variable ‘estimator’ which takes decision variables along with a list of user inputs as an input to a trained machine learning model.

The machine learning model was trained using H2O Automl on the training dataset.
If I call the ‘estimator.predict’ command with any test sample, it gives the output prediction dataset successfully. But if I use this inside lambda function for creating a constraint, it throws an error.


File ~\anaconda3\envs\blend_env\lib\site-packages\h2o\, in H2OFrame.dtype(self)
368 """
369 Returns the numpy.dtype of the first column of this data frame.
370 Works only for single-column data frames.
373 :returns: Numpy dtype of the first column
374 """
375 if not len(self.columns) == 1:
--> 376 raise H2OValueError("dtype is only supported for one column frames")
377 if not can_use_numpy():
378 raise ImportError("H2OFrame.dtype function requires numpy to be installed")

H2OValueError: dtype is only supported for one column frames

import nevergrad as ng
import joblib
import h2o
import pandas as pd
import numpy

def nopti(x_inp, inp_range, out_range, p, inp_cols, out_cols, model_path):

    x=ng.p.Array(init=list(inp_range.iloc[2,:]) , lower= list(inp_range.iloc[0,:]), upper= list(inp_range.iloc[1,:]))

    optimizer = ng.optimizers.NGOpt(parametrization=x, budget=100)

    def obj(var):
        return sum(m * n for m, n in zip(var, p))    

    def constraint(x):        
        estimator = h2o.import_mojo(model_path)
        a = lambda x: estimator.predict(h2o.H2OFrame(pd.DataFrame(data = [x_inp+list(x)],
                                            columns = inp_cols))).as_data_frame().iat[0,0] >= out_range.iat[0,j]
        return a

    recommendation = optimizer.minimize(obj,constraint_violation=constraint(x))

    val = recommendation.value

    return val