title: Optimisation in Python with time series parameters, Pyomo
body: <p>I am optimising an energy system that supplies energy from two sources of natural gas and hydrogen to end-users with varying demand in a week period (in hourly time steps).</p>
  <p>I used Pyomo and the ipopt solver to model the system which is working properly. I have two lists of inlet parameters (demand and hydrogen) in my CSV file in two separate columns (and 700 rows as I said there is an hourly time step).</p>
  <p>I want to optimise the system for each time step and generate the OF and calculate the variables per time step (per hour for all rows). The error is</p>
  <blockquote>
  <p>Index '(1, 'NG')' is not valid for indexed component 'X'</p>
  </blockquote>
  <p>I was thinking to use the function for each variable and constraint &quot;def&quot; but I couldn't succeed to get the results.</p>
  <p>I would appreciate it if you could take look at the code and let me know how can I repeat the optimisation for each time step.</p>
  <pre><code>    import pyomo.environ as pyo
      from pyomo.opt import SolverFactory
      from pyomo.environ import *
      import pandas as pd
      import matplotlib as mp
      import matplotlib.pyplot as plt 
      from pyomo.dae import *
      import numpy as np
      
      data=pd.read_csv('H2_Demand_Week.csv')
      
       
      date_numbers = data['Time']
      
      y = data['Demand_MWh'] 
      y2 = data['H2_Curtailed_10MWh']
      
      m = pyo.ConcreteModel()
      Nd=len(data)
      
      
       
      cost_NG = 0.066 #the NG price is 0.066 €/kWh in Ireland in March 2022
      cost_CW = 25 #curtailed wind cost is 25 €/MWh
      cost_Storage = 470 #€/MWh
      cost_H2_out = 0.044 #€/nm3
      cost_H2_in = 0.5 #€/nm3
      WI_NG = 48  #the natural gas Wobbe Index MJ/m3
      WI_H2 = 41 #the hydrogen Wobbe Index MJ/m3
      
     
      
     
  H2_perc=0.05
  
  yd = data['Demand_MWh'] 
  yh = data['H2_Curtailed_10MWh']
  
  
  T = range(len(data))
  
  
  data.replace([np.nan], 0, inplace=True)
  
  #sets and paramenters
  #m.setInv = pyo.Set(initialize=['NG','H','S'])
  
  m.NG = pyo.Var(T, bounds=(0,None), initialize=1)
  
  m.H = pyo.Var(T, bounds=(0,None), initialize=1)
  
  m.S = pyo.Var(T, bounds=(0,None), initialize=1)
  
  #constraints
  #m.C1 = pyo.Constraint(expr = pyo.summation(m.X) == Demand)
  for i in range(1,Nd):
      m.C1 = pyo.Constraint(expr = m.NG[i] + m.H[i] ==yd[i])
      
  
  for i in range(1,Nd):    
      m.C2 = pyo.Constraint(expr = m.H[i] + m.S[i] == yh[i])
      
  
  for i in range(1,Nd):    
      m.C3 = pyo.Constraint(expr = m.H[i]/(m.NG[i] + m.H[i])&lt;= H2_perc) #hydrogen concentration
  
  
  #objective function
  #m.obj = pyo.Objective(expr = m.X['NG']*0.066+m.X['H']*0.5+m.X['S']*cost_Storage, sense=pyo.minimize)
  for i in range(1,Nd):
      m.obj = pyo.Objective(expr = m.S[i]*cost_Storage, sense=pyo.minimize)
  
  
  #begin = time.time()
  
  #solve
  opt = SolverFactory('ipopt', executable='C:\\ipopt.exe')
  #opt = SolverFactory('couenne', executable='C:\\My_Files\\_H2_HYDROGEN_THE_GREEN\\ThisPC_MODELLING\\PYTHON-MODEL\\ENERGY SYSTEM OPTIMISATION\RHODE\\Couenne-0.3.2-win32-msvc9\\Couenne-0.3.2-win32-msvc9\\bin\\couenne.exe')
  
  #opt.options['tol']=1e-3
  #opt.solve(m)
  m.results = opt.solve(m)
  
  #detalT = time.time() - begin
  
  #print
  m.pprint()
  
  print('\n\nStorage:',pyo.value(m.S))
  
  
  print('\n\nOF Storage cost €:',pyo.value(m.obj))
  </code></pre>