Ok so I have the following optimization problem: I need to figure out for every hour of the year if a generation plant should produce or not depending on the cost of producing and the income it receives to produce. So for every hour of the year I have a variable 0 or 1 which is producing or not producing. The problem is that the cost depends on the daily max consumption.
I got until the following code, but I get a nan error.
Can anyone help me figuring out what I am doing wrong?
#Minimize: cost minus income
#cost=5*max(daily_cons)
#daily_cons=sum of hourly consumption per day
#hourly_cons= variable*inst_power
#income=variable*inst_power*prices
#constraint : yearly production has to be equal to 2000
import csv
import numpy as np
from scipy.optimize import minimize
import openpyxl
#define objective function
def objective(x):
# x2=cost, x1=income
return [max(sum((x*inst_power)[i * 24:(i + 1) * 24]) for i in range(int(216/24)))]-sum(x*inst_power*prices)
#define constraint:total number of hours of operation is equal to 10
def constraint1(x):
sum_eq = 2000
for i in range(len(x0)):
sum_eq = sum_eq - x[i]
return sum_eq
f = open('Book2.csv') # use binary mode if on MS windows
d = [i for i in csv.reader(f) ] # use list comprehension to read from file
f.close() # close file
prices=[i[0] for i in d]
prices = np.delete(prices, 0)
prices=[float(i) for i in prices]
inst_power=[i[1] for i in d]
inst_power = np.delete(inst_power, 0)
inst_power=[float(i) for i in inst_power]
eff=[i[2] for i in d]
eff = np.delete(eff, 0)
eff=[float(i) for i in eff]
x0=np.asarray([1.0]*8760)
#bounds of variables
b=(0.0,1.0)
bnds = [(b[0],b[1]) for k in range(len(x0))]
bnds=tuple(bnds)
print(bnds)
#constrain 1 is equality constraint
con1 = {'type': 'eq', 'fun': constraint1}
cons = ([con1])
#solve problem
solution = minimize(objective,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
x = solution.x
# show final objective
print('Final Objective: ' + str(objective(x)))
# print solution
print('Solution')
print(*x, sep = "\n")