Example #1
0
    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')
Example #2
0
    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")
Example #3
0
class Forecast(Thread):
    """ Setup a Forecast Object. A new |env| and new Devices will be created.
    Forecasting can either be ran synchronous or asynchronous (threaded)::

        foocast = Forecast(time.time(), forward=10*24*3600)
        barcast = Forecast(time.time(), forward=2*24*3600)

        #run threaded
        barcast.start() 
        
        #wait until foocast is finished, then get result
        resultfoo = foocast.run().get() 
        
        # wait until barcast is finished
        while resultbar == None:
            resultbar = barcast.get()


    :param int initial_time: timestamp of the time, at which the forecast starts
    :param configurations: cached configurations, if ``None``, retrieve from database
    :param code: code to be executed
    :param int forward: Time to forecast. Uses `DEFAULT_FORECAST_INTERVAL` if ``None``
    :param boolean forecast: Passed to |env| forecast.
    """

    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 step(self):
        """ execute one step of the simulation.
        This steps all devices, auto-optimizes if needed and store the values
        """
        execute_user_function(self.env, self.env.forecast, self.devices, self.user_function)

        if self.use_optimization and self.next_optimization <= 0.0:
            auto_optimize(self)
            self.next_optimization = 3600.0

        # call step function for all devices
        for device in self.devices:
            device.step()

        self.store_values()

        self.env.now += self.env.step_size
        self.next_optimization -= self.env.step_size

    def run(self):
        """ run the main loop. Returns self after finishing.
        Results are obtained with :meth:`get`"""
        time_remaining = self.forward
        while time_remaining > 0:
            self.step()

            self.progress = (1.0 - time_remaining / float(self.forward)) * 100
            time_remaining -= self.env.step_size

        self.result = {
            "start": datetime.fromtimestamp(self.env.initial_date).isoformat(),
            "step": DEFAULT_FORECAST_STEP_SIZE,
            "end": datetime.fromtimestamp(self.env.now).isoformat(),
            "sensors": self.measurements.get_cached(),
        }

        return self

    def store_values(self):
        """ sample device values"""
        self.measurements.take_and_cache()

    def get(self):
        """ return the result of the forecast. 
        If the mainloop is still forecasting, ``None`` is returned.

        outputs a dict with::

            result = {start: datetime, 
                       step: stepsize, 
                        end: datetime, 
                    sensors: list with values per sensor (see MeasurementStorage)}

        """
        return self.result
Example #4
0
class Forecast(Thread):
    """ Setup a Forecast Object. A new |env| and new Devices will be created.
    Forecasting can either be ran synchronous or asynchronous (threaded)::

        foocast = Forecast(time.time(), forward=10*24*3600)
        barcast = Forecast(time.time(), forward=2*24*3600)

        #run threaded
        barcast.start() 
        
        #wait until foocast is finished, then get result
        resultfoo = foocast.run().get() 
        
        # wait until barcast is finished
        while resultbar == None:
            resultbar = barcast.get()


    :param int initial_time: timestamp of the time, at which the forecast starts
    :param configurations: cached configurations, if ``None``, retrieve from database
    :param code: code to be executed
    :param int forward: Time to forecast. Uses `DEFAULT_FORECAST_INTERVAL` if ``None``
    :param boolean forecast: Passed to |env| forecast.
    """
    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 step(self):
        """ execute one step of the simulation.
        This steps all devices, auto-optimizes if needed and store the values
        """
        execute_user_function(self.env, self.env.forecast, self.devices,
                              self.user_function)

        if self.use_optimization and self.next_optimization <= 0.0:
            auto_optimize(self)
            self.next_optimization = 3600.0

        # call step function for all devices
        for device in self.devices:
            device.step()

        self.store_values()

        self.env.now += self.env.step_size
        self.next_optimization -= self.env.step_size

    def run(self):
        """ run the main loop. Returns self after finishing.
        Results are obtained with :meth:`get`"""
        time_remaining = self.forward
        while time_remaining > 0:
            self.step()

            self.progress = (1.0 - time_remaining / float(self.forward)) * 100
            time_remaining -= self.env.step_size

        self.result = {
            'start': datetime.fromtimestamp(self.env.initial_date).isoformat(),
            'step': DEFAULT_FORECAST_STEP_SIZE,
            'end': datetime.fromtimestamp(self.env.now).isoformat(),
            'sensors': self.measurements.get_cached()
        }

        return self

    def store_values(self):
        """ sample device values"""
        self.measurements.take_and_cache()

    def get(self):
        """ return the result of the forecast. 
        If the mainloop is still forecasting, ``None`` is returned.

        outputs a dict with::

            result = {start: datetime, 
                       step: stepsize, 
                        end: datetime, 
                    sensors: list with values per sensor (see MeasurementStorage)}

        """
        return self.result