Unable to minimize output of sum of several machine learning models

62 Views Asked by At

I have trained several machine learning models and wrapped them in functions. The functions are e1,e2 and e3. The functions take in an input of demand and output energy. I tried using the scipy.optimize minimize library to minimize the output of the sum of the machine learning models as such:

output = e1(demand1) + e2(demand2) + e3(demand3)

where demand1 + demand2 + demand3 = 4567

and 1000<= demand1,demand2,demand3<=2000

These are the high level functions I have as well as my following steps to minimize the output.

def e1(demand1):
    #reshaping of demand1 to a 2d array for my model to predict below
    y =model1.predict(demand1)
    return y

def e2(demand2):
    #reshaping of demand1 to a 2d array for my model to predict below
    y =model2.predict(demand2)
    return y

def e1(demand3):
    #reshaping of demand1 to a 2d array for my model to predict below
    y =model3.predict(demand3)
    return y

import numpy as np
from scipy.optimize import minimize
def objective(x):
    demand1 = x[0]
    demand2 = x[1]
    demand3 = x[2]
    return e1(demand1) + e2(demand2) + e3(demand3)

def constraint(x):
    sumofdemands = 4567
    for i in range(3):
        sumofdemands = sumofdemands - x[i]
    return sumofdemands 


x0 = [2000,2000,2000]
b = (1000.0,2000.0)
bnds = (b,b,b)
con = {'type':'eq','fun':constraint}
cons = [con]

 

sol = minimize(objective,x0,method='SLSQP',bounds=bnds,constraints=cons,options={'maxiter':100})

The output of sol is:

 message: Optimization terminated successfully
 success: True
  status: 0
     fun: 2618.1348
       x: [ 1.522e+03  1.522e+03  1.522e+03]
     nit: 2
     jac: [ 0.000e+00  0.000e+00  0.000e+00]
    nfev: 8
    njev: 2

Hence, these are the demand1, demand2 and demand3 values i get from sol:

[1522.333333333,1522.3333333333,1522.333333333333]

It seems to just have divided the sum of demands by 3 and used that as the individual inputs for the functions.

However the minimized output is attained when demand1, demand2 and demand3 respectively are:

[[1999.9999999998308, 1566.9999999999272, 1000.0000000002418]

0

There are 0 best solutions below