def __init__(self, initial_time, configurations=None, code=None, forward=None, forecast=True): Thread.__init__(self) self.daemon = True demomode = Configuration.objects.get(key="system_mode").value == "demo" self.env = BaseEnvironment( initial_time=initial_time, forecast=forecast, step_size=DEFAULT_FORECAST_STEP_SIZE, demomode=demomode ) # get_forecast if configurations is None: configurations = DeviceConfiguration.objects.all() self.devices = get_initialized_scenario(self.env, configurations) self.measurements = MeasurementStorage(self.env, self.devices) self.user_function = get_user_function(self.devices, code) self.progress = 0.0 self.result = None self.forward = forward if forward == None: self.forward = DEFAULT_FORECAST_INTERVAL self.next_optimization = 0.0 self.use_optimization = get_configuration("auto_optimization")
def start_or_get(cls, print_visible=False): """ This method starts a new demo simulation if necessary and it makes sure that only one demo simulation can run at once. This is the preferred way to start the demo simulation. :returns: :class:`DemoSimulation` or ``None`` if system not in demo mode. """ # Start demo simulation if in demo mode system_mode = Configuration.objects.get(key="system_mode") if system_mode.value != "demo": return None if cls.stored_simulation == None: if print_visible: print "Starting demo simulation..." else: logger.debug("Starting demo simulation...") simulation = DemoSimulation(get_initial_time()) simulation.use_optimization = get_configuration("auto_optimization") simulation.start() cls.stored_simulation = simulation return cls.stored_simulation
def start_or_get(cls, print_visible=False): """ This method starts a new demo simulation if necessary and it makes sure that only one demo simulation can run at once. This is the preferred way to start the demo simulation. :returns: :class:`DemoSimulation` or ``None`` if system not in demo mode. """ # Start demo simulation if in demo mode system_mode = Configuration.objects.get(key='system_mode') if system_mode.value != 'demo': return None if cls.stored_simulation == None: if print_visible: print "Starting demo simulation..." else: logger.debug("Starting demo simulation...") simulation = DemoSimulation(get_initial_time()) simulation.use_optimization = get_configuration( 'auto_optimization') simulation.start() cls.stored_simulation = simulation return cls.stored_simulation
def __init__(self, initial_time, configurations=None, code=None, forward=None, forecast=True): Thread.__init__(self) self.daemon = True demomode = Configuration.objects.get(key='system_mode').value == "demo" self.env = BaseEnvironment(initial_time=initial_time, forecast=forecast, step_size=DEFAULT_FORECAST_STEP_SIZE, demomode=demomode) #get_forecast if configurations is None: configurations = DeviceConfiguration.objects.all() self.devices = get_initialized_scenario(self.env, configurations) self.measurements = MeasurementStorage(self.env, self.devices) self.user_function = get_user_function(self.devices, code) self.progress = 0.0 self.result = None self.forward = forward if forward == None: self.forward = DEFAULT_FORECAST_INTERVAL self.next_optimization = 0.0 self.use_optimization = get_configuration('auto_optimization')
def get_operating_costs(device, start): workload = Sensor.objects.get(device=device, key='workload') max_gas_input = get_device_configuration(device, 'max_gas_input') total_gas_consumption = 0 for value in SensorValueDaily.objects.filter(sensor=workload, timestamp__gte=start): step = (value.value / 100.0) * (120 / 3600.0) total_gas_consumption += max_gas_input * step return '%s Euro' % round(total_gas_consumption * get_configuration('gas_costs'), 2)
def get_operating_costs(device, start): workload = Sensor.objects.get(device=device, key='workload') max_gas_input = get_device_configuration(device, 'max_gas_input') total_gas_consumption = 0 for value in SensorValueDaily.objects.filter(sensor=workload, timestamp__gte=start): step = (value.value / 100.0) * (120 / 3600.0) total_gas_consumption += max_gas_input * step return '%s Euro' % round( total_gas_consumption * get_configuration('gas_costs'), 2)
def find_optimal_config(initial_time, forecast): """ ``Internal Method`` Main method, which optimizes the costs by running a global approximation for the best configuration and then running a local minimization method on this approximation""" prices = {} prices["gas_costs"] = get_configuration('gas_costs') prices["electrical_costs"] = get_configuration('electrical_costs') rewards = {} rewards["thermal_revenues"] = get_configuration('thermal_revenues') rewards["warmwater_revenues"] = get_configuration('warmwater_revenues') rewards["electrical_revenues"] = get_configuration('electrical_revenues') rewards["feed_in_reward"] = get_configuration('feed_in_reward') arguments = (initial_time, forecast, prices, rewards) #find initial approximation for parameters results = [] for cu_load in range(0,100,10): config = [cu_load,] cost = estimate_cost(config, *arguments) results.append(BilanceResult(cost, config)) boundaries = [(0.0,100.0)] #take parameters with lowest cost initial_parameters = min(results,key=lambda result: result.cost).params parameters = fmin_l_bfgs_b(estimate_cost, x0 = array(initial_parameters), args = arguments, bounds = boundaries, approx_grad = True, factr=10**4, iprint=0, epsilon=1, maxfun =50) cu_workload, = parameters[0] return {"cu_overwrite_workload":cu_workload}
def get_total_balance_by_date(month, year): start = datetime(year, month, 1).replace(tzinfo=utc) end = datetime(year, month, calendar.mdays[month]).replace(tzinfo=utc) # calculate costs sensor_ids = Sensor.objects.filter( device__device_type__in=[Device.CU, Device.PLB]).values_list('id', flat=True) sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter( sensor__key='current_gas_consumption') total_gas_consumption = 0 for sensor_value in sensor_values: total_gas_consumption += sensor_value.sum gas_costs = get_configuration('gas_costs') costs = total_gas_consumption * gas_costs # Calculate electrical purchase sensor_ids = Sensor.objects.filter( device__device_type=Device.PM).values_list('id', flat=True) sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter(sensor__key='purchased') total_electrical_purchase = 0 for sensor_value in sensor_values: total_electrical_purchase += sensor_value.sum electrical_costs = get_configuration('electrical_costs') costs += total_electrical_purchase * electrical_costs # calculate rewards # thermal consumption sensor_ids = Sensor.objects.filter( device__device_type=Device.TC).values_list('id', flat=True) sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter( sensor__key='get_consumption_power') total_thermal_consumption = 0 for sensor_value in sensor_values: total_thermal_consumption += sensor_value.sum thermal_revenues = get_configuration('thermal_revenues') rewards = total_thermal_consumption * thermal_revenues # warmwater consumption sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter( sensor__key='get_warmwater_consumption_power') total_warmwater_consumption = 0 for sensor_value in sensor_values: total_warmwater_consumption += sensor_value.sum warmwater_revenues = get_configuration('warmwater_revenues') rewards += total_warmwater_consumption * \ get_configuration('warmwater_revenues') # electrical consumption sensor_ids = Sensor.objects.filter( device__device_type=Device.EC).values_list('id', flat=True) sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter( sensor__key='get_consumption_power') total_electrical_consumption = 0 for sensor_value in sensor_values: total_electrical_consumption += sensor_value.sum electrical_revenues = get_configuration('electrical_revenues') rewards += total_electrical_consumption * electrical_revenues # electrical infeed sensor_ids = Sensor.objects.filter( device__device_type=Device.PM).values_list('id', flat=True) sensor_values_sum = SensorValueMonthlySum.objects.filter( timestamp__gte=start, timestamp__lte=end, sensor_id__in=sensor_ids) sensor_values = sensor_values_sum.filter(sensor__key='fed_in_electricity') total_electrical_infeed = 0 for sensor_value in sensor_values: total_electrical_infeed += sensor_value.sum feed_in_reward = get_configuration('feed_in_reward') rewards += total_electrical_infeed * feed_in_reward return { 'costs': round(-costs, 2), 'rewards': round(rewards, 2), 'balance': round(rewards - costs, 2), 'prices': { 'gas_costs': -gas_costs, 'electrical_costs': -electrical_costs, 'thermal_revenues': thermal_revenues, 'warmwater_revenues': warmwater_revenues, 'electrical_revenues': electrical_revenues, 'feed_in_reward': feed_in_reward }, 'kwh': { 'gas_consumption': round(total_gas_consumption, 2), 'electrical_purchase': round(total_electrical_purchase, 2), 'thermal_consumption': round(total_thermal_consumption, 2), 'warmwater_consumption': round(total_warmwater_consumption, 2), 'electrical_consumption': round(total_electrical_consumption, 2), 'electrical_infeed': round(total_electrical_infeed, 2) } }
def get_statistics_for_cogeneration_unit(start=None, end=None): output = [] try: for device in Device.objects.filter(device_type=Device.CU): device_output = [] device_output.append(('type', Device.CU)) device_output.append(('device_id', device.id)) device_output.append(('device_name', device.name)) sensor_workload = Sensor.objects.get(device=device, key='workload') sensor_consumption = Sensor.objects.get( device=device, key='current_gas_consumption') workloads = SensorValueDaily.objects.filter(sensor=sensor_workload) workloads_monthly_avg = SensorValueMonthlyAvg.objects.filter( sensor=sensor_workload) consumptions_monthly_sum = SensorValueMonthlySum.objects.filter( sensor=sensor_consumption) if start is not None: workloads = workloads.filter(timestamp__gte=start) workloads_monthly_avg = workloads_monthly_avg.filter( timestamp__gte=start) consumptions_monthly_sum = consumptions_monthly_sum.filter( timestamp__gte=start) if end is not None: workloads = workloads.filter(timestamp__lte=end) workloads_monthly_avg = workloads_monthly_avg.filter( timestamp__lte=end) consumptions_monthly_sum = consumptions_monthly_sum.filter( timestamp__lte=end) hours_of_operation = workloads.filter(value__gt=0).count() * 24 device_output.append( ('hours_of_operation', round(hours_of_operation, 2))) device_output.append( ('average_workload', round(workloads_monthly_avg.latest('timestamp').avg, 2))) thermal_efficiency = get_device_configuration( device, 'thermal_efficiency') electrical_efficiency = get_device_configuration( device, 'electrical_efficiency') max_gas_input = get_device_configuration(device, 'max_gas_input') total_gas_consumption = consumptions_monthly_sum.latest( 'timestamp').sum total_electrical_production = total_gas_consumption * \ electrical_efficiency total_thermal_production = total_gas_consumption * \ thermal_efficiency device_output.append( ('total_thermal_production', round(total_thermal_production, 2))) device_output.append(('total_electrical_production', round(total_electrical_production, 2))) device_output.append( ('total_gas_consumption', round(total_gas_consumption, 2))) gas_costs = get_configuration('gas_costs') operating_costs = total_gas_consumption * gas_costs device_output.append(('operating_costs', round(operating_costs, 2))) values = list(workloads) device_output.append(('values_count', len(values))) power_ons = 0 last_time_on = None for value in values: if last_time_on is None: last_time_on = value.value > 0 if (last_time_on and value.value == 0) or (not last_time_on and value.value > 0): power_ons += 1 last_time_on = not last_time_on device_output.append(('power_ons', power_ons)) output.append(dict(device_output)) except (Device.DoesNotExist, DeviceConfiguration.DoesNotExist, Configuration.DoesNotExist, Sensor.DoesNotExist, SensorValue.DoesNotExist, SensorValueMonthlyAvg.DoesNotExist) as e: logger.warning("DoesNotExist error: %s" % e) return output
def get_statistics_for_cogeneration_unit(start=None, end=None): output = [] try: for device in Device.objects.filter(device_type=Device.CU): device_output = [] device_output.append(('type', Device.CU)) device_output.append(('device_id', device.id)) device_output.append(('device_name', device.name)) sensor_workload = Sensor.objects.get(device=device, key='workload') sensor_consumption = Sensor.objects.get( device=device, key='current_gas_consumption') workloads = SensorValueDaily.objects.filter(sensor=sensor_workload) workloads_monthly_avg = SensorValueMonthlyAvg.objects.filter( sensor=sensor_workload) consumptions_monthly_sum = SensorValueMonthlySum.objects.filter( sensor=sensor_consumption) if start is not None: workloads = workloads.filter(timestamp__gte=start) workloads_monthly_avg = workloads_monthly_avg.filter( timestamp__gte=start) consumptions_monthly_sum = consumptions_monthly_sum.filter( timestamp__gte=start) if end is not None: workloads = workloads.filter(timestamp__lte=end) workloads_monthly_avg = workloads_monthly_avg.filter( timestamp__lte=end) consumptions_monthly_sum = consumptions_monthly_sum.filter( timestamp__lte=end) hours_of_operation = workloads.filter( value__gt=0).count() * 24 device_output.append( ('hours_of_operation', round(hours_of_operation, 2))) device_output.append( ('average_workload', round(workloads_monthly_avg.latest('timestamp').avg, 2))) thermal_efficiency = get_device_configuration( device, 'thermal_efficiency') electrical_efficiency = get_device_configuration( device, 'electrical_efficiency') max_gas_input = get_device_configuration(device, 'max_gas_input') total_gas_consumption = consumptions_monthly_sum.latest('timestamp').sum total_electrical_production = total_gas_consumption * \ electrical_efficiency total_thermal_production = total_gas_consumption * \ thermal_efficiency device_output.append( ('total_thermal_production', round(total_thermal_production, 2))) device_output.append( ('total_electrical_production', round(total_electrical_production, 2))) device_output.append( ('total_gas_consumption', round(total_gas_consumption, 2))) gas_costs = get_configuration('gas_costs') operating_costs = total_gas_consumption * gas_costs device_output.append( ('operating_costs', round(operating_costs, 2))) values = list(workloads) device_output.append(('values_count', len(values))) power_ons = 0 last_time_on = None for value in values: if last_time_on is None: last_time_on = value.value > 0 if (last_time_on and value.value == 0) or (not last_time_on and value.value > 0): power_ons += 1 last_time_on = not last_time_on device_output.append(('power_ons', power_ons)) output.append(dict(device_output)) except (Device.DoesNotExist, DeviceConfiguration.DoesNotExist, Configuration.DoesNotExist, Sensor.DoesNotExist, SensorValue.DoesNotExist, SensorValueMonthlyAvg.DoesNotExist) as e: logger.warning("DoesNotExist error: %s" % e) return output