I am trying to run a 'simple' optimation programm without any constraints to get the programming running. After getting the program running I am planning on adding constraints and other input variables. However, I cannot seem to get the model to work. I have filterd the used dateframe down to just 2 days for computational feasability. The dataframe named df contains dates on a 15 minute interval (datetime) and two column with building load and PV generation, both floats(See attached figure).

I used the following code just to see if the optimzation problem works. Note that no constraints are added (Pgrid_max is commented out). Eventually I will add a data set with data on EV charging to optimize bi-directional EV charging.
start_date = '2023-06-01 00:00:00'
end_date = '2023-06-02 23:59:59'
# Pgrid_max = (df['Building load [kWh]'].max() + 24*18*0.25) * 1.5
model = ConcreteModel()
# Time intervals
T = df[start_date:end_date].index.tolist() # Create a list of the timesteps
# Sets
model.T = Set(initialize=T) # Time interval
# Parameters
model.Pbuilding = Param(model.T, initialize=df[start_date:end_date]['Building load [kWh]'].to_dict())
model.Ppv = Param(model.T, initialize=df[start_date:end_date]['PV load [kWh]'].to_dict())
# Variables
model.Pgrid = Var(model.T, domain=Reals)
# model.Pev = Var(model.T, domain=PositiveReals, bounds=(0, 24*0.25))
# Energy balance equation
def energy_balance(model, t):
return model.Pgrid[t] == model.Pbuilding[t] - model.Ppv[t]
# Objective function
def objective_rule(model):
return sum(model.Pgrid[t] for t in model.T)
model.obj = Objective(rule=objective_rule, sense=minimize)
# Solve the model (assuming you have a solver like glpk installed)
solver = SolverFactory('glpk')
solver.solve(model, tee=True)
# Extract results directly into a pandas DataFrame
results = pd.DataFrame(index=T)
results['Pgrid'] = [model.Pgrid[t].value for t in T]
for t in model.T:
results['Pgrid'][t] = model.Pgrid[t].value
print(results)
When running this code I keep getting the message that the problem has no solution. How can this be the case sinde Pgrid can take any real value. Printing the results gives only "None" values. Anyone that knows what I am doing wrong?
You have a couple issues in your code.
The most significant (and why you are probably getting zeros) is that you didn't construct the constraint. You need to call the function as a
ruleas shown in the code below.A couple other tips/ideas:
pprintthe model to QA it. If you did, you would have noticed the missing constraintpandasunless you are SUPER comfortable with it. Dictionaries as shown are easy to troubleshoot with small dataOn your model:
Pgrida non-negative real and make the balance constraint greater than or equal (GTE) as shown to cover the cases where the pv > demand, unless it is the case that you can push power back to the grid.Code:
Output: