Exemplo n.º 1
0
 def get_energy_estimate(self, world):
   '''
   Estimates how much energy will be used in the next timestep, given the current world state, by 
   running a GridLAB-D simulation for the timestep. The world's .glm file includes the thermostat 
   heating and cooling setpoint, which remain constant over the course of the simulated timestep.
   '''
   createGLM.write_GLM_file(world, self, "pred")
   util.run_gld_reg(world.glmfile)
   energy_used = util.get_energy_used(world.energy_use_file, world.sim_start_time, world.sim_end_time)
   if (round(world.outdoor_temp, 0), round(world.indoor_temp, 0), round(energy_used, 2)) not in self.energy_estimate.keys():
     self.energy_estimate[(round(world.outdoor_temp, 0), round(world.indoor_temp, 0), round(energy_used, 2))] = (world.heating_setpoint, world.cooling_setpoint)
   return energy_used
Exemplo n.º 2
0
  def explore(self, world, run_params):
    # TODO (maybe): Get timestamps for start and end of the year prior, to make it more "realistic" 
    # - test if this makes any difference on results (ie does indicating a different year actually result in different weather for TMY2)
    
    print "starting to explore at: ", datetime.now()
    # Write needed files:
    for f in ["explore_cool_setpoints", "explore_heat_setpoints", "explore_results", "explore_temps"]:
      file_name = run_params.run_name + '/' + run_params.run_name + '_' + f + '.csv'
      setattr(self, f+"_file", file_name)
    random_cooling_setpoints = {}
    random_heating_setpoints = {}
    for (player, valid_setpoints, random_setpoints) in [(self.explore_cool_setpoints_file, self.valid_cooling_setpoints, random_cooling_setpoints), (self.explore_heat_setpoints_file, self.valid_heating_setpoints, random_heating_setpoints)]:
      with open(player, 'wb') as f:
        fwriter = csv.writer(f)
        timestamp = world.start_control
        while (timestamp <= world.end_control):
          timestamp_string = util.datetimeTOstring(timestamp, world.timezone_short)
          random_setpoint = random.choice(valid_setpoints)
          fwriter.writerow([timestamp_string, random_setpoint])
          random_setpoints[timestamp] = random_setpoint
          timestamp += timedelta(minutes = self.timestep)
    print "finished writing player files at ", datetime.now()

    # Write GLM file
    self.explore_glmfile = run_params.run_name + '/' + run_params.run_name + '_explore_GLM.glm'
    createGLM.write_GLM_file(world, self, "explore")

    # Run GridLAB-D offline
    util.run_gld_reg(self.explore_glmfile)
    print "finished running GLD offline at ", datetime.now()

    # Get indoor and outdoor temps at each timestep:
    indoor_temps = {}
    outdoor_temps = {}
    with open(self.explore_temps_file) as f:
      r = csv.reader(f)
      for row_header in r:
        match = re.search(r'\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d', row_header[0])
        if match:
          ts = parser.parse(row_header[0])
          if ts >= world.start_control:
            indoor_temps[ts] = float(row_header[1])
            outdoor_temps[ts] = float(row_header[2])
            break
      for row in r:
        ts = parser.parse(row[0])
        indoor_temps[ts] = float(row[1])
        outdoor_temps[ts] = float(row[2])
        if ts >= world.end_control:
          break
    print "finished reading indoor and outdoor temps at ", datetime.now()

    # Update q-values sequentially:
    util.energy_row_tracker = 0
    current_month_index = 0
    n_timesteps_passed = 0.0
    budget_month_used = 0.0
    n_timesteps_in_month = self.n_timesteps_in_day * world.n_days_in_months[current_month_index]
    start_timestamp = world.start_control
    start_budget = self.budgets[self.current_month_index] / (n_timesteps_in_month + 0.0)
    s = (round(indoor_temps[start_timestamp], 0), round(outdoor_temps[start_timestamp], 0), round(start_budget, 2))
    while (start_timestamp < world.end_control-timedelta(minutes = self.timestep)):
      end_timestamp = start_timestamp + timedelta(minutes = self.timestep)
      energy_used = util.get_energy_used(self.explore_results_file, start_timestamp, end_timestamp, True)
      elec_price = self.elec_prices[current_month_index]
      # If end_timestamp starts a new month, reset the month's used budget to 0 and increment the current month index:
      # THIS IS WHERE THE EPISODE WOULD END!!!!
      if end_timestamp.hour == 0 and end_timestamp.minute == 0 and end_timestamp.day == 1:
        n_timesteps_passed = 0.0
        budget_month_used = 0.0
        current_month_index += 1
        n_timesteps_in_month = self.n_timesteps_in_day * world.n_days_in_months[current_month_index]
      else:
        n_timesteps_passed += 1
        budget_month_used += energy_used * self.elec_prices[current_month_index]

      end_budget = (self.budgets[current_month_index] - budget_month_used) / (n_timesteps_in_month - n_timesteps_passed)
      cooling_setpoint = random_cooling_setpoints[start_timestamp]
      heating_setpoint = random_heating_setpoints[start_timestamp]
      reward = self.get_reward(start_budget, energy_used, elec_price, cooling_setpoint, heating_setpoint)
      s_prime = (round(indoor_temps[end_timestamp], 0), round(outdoor_temps[end_timestamp], 0), round(end_budget, 2))
      new_sample = reward + self.gamma * self.get_max_qValue(s_prime, self.valid_setpoints)
      self.qValues[(s, (heating_setpoint, cooling_setpoint))] = (1-self.alpha)*self.get_qValue(s, (heating_setpoint, cooling_setpoint)) + self.alpha*new_sample

      start_timestamp = end_timestamp
      start_budget = end_budget
      s = s_prime
    print "done exploring at ", datetime.now()