0

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")
David
  • 129
  • 1
  • 11
  • this is a lot of code with no comments and vague variable names. you should provide part of the code you have problem with or at least mention some of your expected results and what you get in return. – prhmma Nov 25 '19 at 16:18
  • ok, I have added comments to explain what the code is doing in every step – David Nov 25 '19 at 16:48
  • well I tested it and it doesn't return NAN – prhmma Nov 25 '19 at 17:00
  • for me it does :(. I think the problem is in the objective function. – David Nov 25 '19 at 17:04
  • 1
    use google colab and see if it still have problem – prhmma Nov 25 '19 at 17:05
  • 1
    try it on smaller size than 8760, maybe 100 – prhmma Nov 25 '19 at 17:07
  • you are right that the exact same code works in google colab but not in my pycharm IDE. What could be causing that issue? – David Nov 25 '19 at 17:22
  • maybe the size is bigger than your system can handle, because what see is pretty large – prhmma Nov 25 '19 at 17:31
  • Ok, I changed the code to reflect the problem I have with the maximum of the daily consumption. Thank you – David Nov 25 '19 at 17:42
  • still works fine, all of your hourly values are same, so all of daily values will be the same and max will not have any meaning here – prhmma Nov 25 '19 at 17:50
  • ok, yes the problem was that my pc could not handle it. I hope google colab can handle the 8760 hours. – David Nov 25 '19 at 18:11
  • 1
    in your pc you can use your hard disk to save partially array. google for working with big data on low ram – prhmma Nov 25 '19 at 18:14
  • 1
    Ok, so if you respond as an answer that the problem is that my pc can not handle it and that using google colab could solve the problem I will accept your response as the solution. Thank you very much! – David Nov 25 '19 at 18:43

1 Answers1

0

everything looks just fine, the problem is your ram capacity on the pc is not enough to handle such big arrays. google colab will help or you can try ways to work on your data partially and save the rest on hard disk, since your calculation seems linear.

prhmma
  • 843
  • 10
  • 18