agregator.py
def __optimize(self, cost, first_decision=0, correction=None, delta_temperature_outside_correction=0.):
        """
        Optimization process. Can be use during the day-ahead decision period or in real time if errors. Set a
        on/off value for each slot.

        :param cost: Cost using for the minimization function
        :type cost: Vector

        :param first_decision: We can't decide for the current time. We need the first decision of the simulation

        :param correction: Used if errors has been detected in the past
        :type correction: Correction

        :param delta_temperature_outside_correction: Used if difference of temperature has been detected. The difference
                                                     is the delta between the forecase and the real value.
        :type delta_temperature_outside_correction: float

        :return A tuple of two dictionaries: The first dictionary : Key is the time and value is the on/off
                                             value decision. The second dictionary : Key is the time  t and value is the
                                             final temperature at time t.

        """

        ###########################################################
        #
        # Set the data
        #

        starting_time = sorted(cost.keys())[0]
        ending_time = sorted(cost.keys())[-1] + self.delta_time

        # Value importation
        # external_thermal_element = self._outside_process # Get the temperature at time t
        thermal_conductivity = units.value(getattr(self._outside_coupling, "thermal_conductivity"))  # with couple
        thermal_capacity = units.value(self.thermal_process.thermal_volumic_capacity)
        if thermal_capacity == 0:
            raise RuntimeError("Thermal capacity must be greater than zero")

        subject_energy = units.value(self.subject.power)
        subject_efficiency = getattr(self.subject, "_efficiency_factor")

        nb_slots_optimization = len(cost)

        #
        # Set the data
        #
        ###########################################################

        ###########################################################
        #
        # Prepare the problem
        #

        # Linearization of the problem
        problem = pulp.LpProblem("lpOptimization", pulp.LpMinimize)
        # Decisions variables
        initial_temperature = {}
        final_temperature = {}
        _power_on = {}
        exceeding = {}
        difference_mean_temperature = pulp.LpVariable("diff_mean", lowBound=0)

        # Data
        temperature = {}

        # For each step of optimisation
        for t in range(starting_time, ending_time, self.delta_time):
            # Prepare the variables
            initial_temperature[t] = pulp.LpVariable("tinit_%s" % t)
            final_temperature[t] = pulp.LpVariable("tfin_%s" % t)
            _power_on[t] = pulp.LpVariable("p_%s" % t, cat='Binary')
            exceeding[t] = pulp.LpVariable("exc_%s" % t, lowBound=0)
            temperature[t] = \
                units.value(
                    units.convert(self._outside_temperature_forecast[t], units.celsius)
                ) + delta_temperature_outside_correction

        # Objective to minimize the cost
        problem += \
            pulp.lpSum(
                _power_on[t] * cost[t] + 1000 * exceeding[t] for t in cost.keys()
            ) + 10000 * difference_mean_temperature

        #
        # Constraints
        #
        # Temperature mean during the time optimization must be the
        # target temperature (greater than because lost precision float)
        problem += \
            pulp.lpSum(final_temperature[t] for t in final_temperature.keys()) >= \
            units.value(
                units.convert(self.target_temperature, units.celsius)
            ) * nb_slots_optimization - difference_mean_temperature

        if correction is None:
            for t in range(starting_time, ending_time, self.delta_time):

                outside_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=thermal_conductivity,
                    external_temperature=temperature[t],
                    internal_temperature=initial_temperature[t])

                inner_production = self.__internal_production(
                    internal_temperature=initial_temperature[t],
                    power_on=_power_on[t],
                    subject_efficiency=subject_efficiency,
                    subject_energy=subject_energy,
                    thermal_capacity=thermal_capacity)

                problem += final_temperature[t] == outside_leverage + inner_production

        else:
            for t in range(starting_time, ending_time, self.delta_time):
                outside_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=thermal_conductivity,
                    external_temperature=temperature[t],
                    internal_temperature=initial_temperature[t])

                inner_production = self.__internal_production(
                    internal_temperature=initial_temperature[t],
                    power_on=_power_on[t],
                    subject_efficiency=subject_efficiency,
                    subject_energy=subject_energy,
                    thermal_capacity=thermal_capacity)

                correction_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=correction.thermal_conductivity,
                    external_temperature=correction.temperature,
                    internal_temperature=initial_temperature[t])

                problem += final_temperature[t] == outside_leverage + inner_production + correction_leverage

        for t in range(starting_time, ending_time, self.delta_time):
            problem += final_temperature[t] <= self.target_temperature + 0.5 * self.hysteresis + exceeding[t]
            problem += final_temperature[t] >= self.target_temperature - 0.5 * self.hysteresis - exceeding[t]
        problem += \
            initial_temperature[starting_time] == \
            units.value(units.convert(units(self.thermal_process.temperature, units.kelvin), units.celsius))

        problem += _power_on[starting_time] == first_decision

        for t in range(starting_time + self.delta_time, ending_time, self.delta_time):
            problem += initial_temperature[t] == final_temperature[t-self.delta_time]

        #
        # Prepare the problem
        #
        ###########################################################

        ###########################################################
        #
        # Resolution and return
        #

        problem.solve(self.solver)
        self.status = pulp.LpStatus[problem.status]

        if self.status is 'Infeasible':
            raise RuntimeError("The problem isn't feasible")

        return [
            dict([(k, pulp.value(v)) for k, v in _power_on.items()]),
            dict([(k, pulp.value(v)) for k, v in final_temperature.items()])
        ]

        Optimization process. Can be use during the day-ahead decision period or in real time if errors. Set a
        on/off value for each slot.

        :param cost: Cost using for the minimization function
        :type cost: Vector

        :param first_decision: We can't decide for the current time. We need the first decision of the simulation

        :param correction: Used if errors has been detected in the past
        :type correction: Correction

        :param delta_temperature_outside_correction: Used if difference of temperature has been detected. The difference
                                                     is the delta between the forecase and the real value.
        :type delta_temperature_outside_correction: float

        :return A tuple of two dictionaries: The first dictionary : Key is the time and value is the on/off
                                             value decision. The second dictionary : Key is the time  t and value is the
                                             final temperature at time t.

        
Optimization process. Can be use during the day-ahead decision period or in real time if errors. Set a
on/off value for each slot.

:param cost: Cost using for the minimization function
:type cost: Vector

:param first_decision: We can't decide for the current time. We need the first decision of the simulation

:param correction: Used if errors has been detected in the past
:type correction: Correction

:param delta_temperature_outside_correction: Used if difference of temperature has been detected. The difference
is the delta between the forecase and the real value.

:return A tuple of two dictionaries: The first dictionary : Key is the time and value is the on/off
value decision. The second dictionary : Key is the time  t and value is the
final temperature at time t.
Optimization process . Can be use during the day - ahead decision period or in real time if errors . Set a on / off value for each slot . : param cost : Cost using for the minimization function : type cost : Vector : param first_decision : We can ' t decide for the current time . We need the first decision of the simulation : param correction : Used if errors has been detected in the past : type correction : Correction : param delta_temperature_outside_correction : Used if difference of temperature has been detected . The difference is the delta between the forecase and the real value . : return A tuple of two dictionaries : The first dictionary : Key is the time and value is the on / off value decision . The second dictionary : Key is the time t and value is the final temperature at time t .
def __optimize(self, cost, first_decision=0, correction=None, delta_temperature_outside_correction=0.):
         Set the data
        starting_time = sorted(cost.keys())[0]
        ending_time = sorted(cost.keys())[-1] + self.delta_time
         Value importation
         external_thermal_element = self._outside_process  Get the temperature at time t
        thermal_conductivity = units.value(getattr(self._outside_coupling, "thermal_conductivity"))   with couple
        thermal_capacity = units.value(self.thermal_process.thermal_volumic_capacity)
        if thermal_capacity == 0:
            raise RuntimeError("Thermal capacity must be greater than zero")
        subject_energy = units.value(self.subject.power)
        subject_efficiency = getattr(self.subject, "_efficiency_factor")
        nb_slots_optimization = len(cost)
         Set the data
         Prepare the problem
         Linearization of the problem
        problem = pulp.LpProblem("lpOptimization", pulp.LpMinimize)
         Decisions variables
        initial_temperature = {}
        final_temperature = {}
        _power_on = {}
        exceeding = {}
        difference_mean_temperature = pulp.LpVariable("diff_mean", lowBound=0)
         Data
        temperature = {}
         For each step of optimisation
        for t in range(starting_time, ending_time, self.delta_time):
             Prepare the variables
            initial_temperature[t] = pulp.LpVariable("tinit_%s" % t)
            final_temperature[t] = pulp.LpVariable("tfin_%s" % t)
            _power_on[t] = pulp.LpVariable("p_%s" % t, cat='Binary')
            exceeding[t] = pulp.LpVariable("exc_%s" % t, lowBound=0)
            temperature[t] = \
                units.value(
                    units.convert(self._outside_temperature_forecast[t], units.celsius)
                ) + delta_temperature_outside_correction
         Objective to minimize the cost
        problem += \
            pulp.lpSum(
                _power_on[t] * cost[t] + 1000 * exceeding[t] for t in cost.keys()
            ) + 10000 * difference_mean_temperature
         Constraints
         Temperature mean during the time optimization must be the
         target temperature (greater than because lost precision float)
        problem += \
            pulp.lpSum(final_temperature[t] for t in final_temperature.keys()) >= \
            units.value(
                units.convert(self.target_temperature, units.celsius)
            ) * nb_slots_optimization - difference_mean_temperature
        if correction is None:
            for t in range(starting_time, ending_time, self.delta_time):
                outside_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=thermal_conductivity,
                    external_temperature=temperature[t],
                    internal_temperature=initial_temperature[t])
                inner_production = self.__internal_production(
                    internal_temperature=initial_temperature[t],
                    power_on=_power_on[t],
                    subject_efficiency=subject_efficiency,
                    subject_energy=subject_energy,
                    thermal_capacity=thermal_capacity)
                problem += final_temperature[t] == outside_leverage + inner_production
        else:
            for t in range(starting_time, ending_time, self.delta_time):
                outside_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=thermal_conductivity,
                    external_temperature=temperature[t],
                    internal_temperature=initial_temperature[t])
                inner_production = self.__internal_production(
                    internal_temperature=initial_temperature[t],
                    power_on=_power_on[t],
                    subject_efficiency=subject_efficiency,
                    subject_energy=subject_energy,
                    thermal_capacity=thermal_capacity)
                correction_leverage = self.__external_leverage(
                    thermal_capacity=thermal_capacity,
                    thermal_conductivity=correction.thermal_conductivity,
                    external_temperature=correction.temperature,
                    internal_temperature=initial_temperature[t])
                problem += final_temperature[t] == outside_leverage + inner_production + correction_leverage
        for t in range(starting_time, ending_time, self.delta_time):
            problem += final_temperature[t] <= self.target_temperature + 0.5 * self.hysteresis + exceeding[t]
            problem += final_temperature[t] >= self.target_temperature - 0.5 * self.hysteresis - exceeding[t]
        problem += \
            initial_temperature[starting_time] == \
            units.value(units.convert(units(self.thermal_process.temperature, units.kelvin), units.celsius))
        problem += _power_on[starting_time] == first_decision
        for t in range(starting_time + self.delta_time, ending_time, self.delta_time):
            problem += initial_temperature[t] == final_temperature[t-self.delta_time]
         Prepare the problem
         Resolution and return
        problem.solve(self.solver)
        self.status = pulp.LpStatus[problem.status]
        if self.status is 'Infeasible':
            raise RuntimeError("The problem isn't feasible")
        return [
            dict([(k, pulp.value(v)) for k, v in _power_on.items()]),
            dict([(k, pulp.value(v)) for k, v in final_temperature.items()])
        ]
def __optimize ( self , cost , first_decision = 0 , correction = None , delta_temperature_outside_correction = 0. ) : starting_time = sorted ( cost . keys ( ) ) [ 0 ] ending_time = sorted ( cost . keys ( ) ) [ - 1 ] + self . delta_time thermal_conductivity = units . value ( getattr ( self . _outside_coupling , "thermal_conductivity" ) ) thermal_capacity = units . value ( self . thermal_process . thermal_volumic_capacity ) if thermal_capacity == 0 : raise RuntimeError ( "Thermal capacity must be greater than zero" ) subject_energy = units . value ( self . subject . power ) subject_efficiency = getattr ( self . subject , "_efficiency_factor" ) nb_slots_optimization = len ( cost ) problem = pulp . LpProblem ( "lpOptimization" , pulp . LpMinimize ) initial_temperature = { } final_temperature = { } _power_on = { } exceeding = { } difference_mean_temperature = pulp . LpVariable ( "diff_mean" , lowBound = 0 ) temperature = { } for t in range ( starting_time , ending_time , self . delta_time ) : initial_temperature [ t ] = pulp . LpVariable ( "tinit_%s" % t ) final_temperature [ t ] = pulp . LpVariable ( "tfin_%s" % t ) _power_on [ t ] = pulp . LpVariable ( "p_%s" % t , cat = 'Binary' ) exceeding [ t ] = pulp . LpVariable ( "exc_%s" % t , lowBound = 0 ) temperature [ t ] = units . value ( units . convert ( self . _outside_temperature_forecast [ t ] , units . celsius ) ) + delta_temperature_outside_correction problem += pulp . lpSum ( _power_on [ t ] * cost [ t ] + 1000 * exceeding [ t ] for t in cost . keys ( ) ) + 10000 * difference_mean_temperature problem += pulp . lpSum ( final_temperature [ t ] for t in final_temperature . keys ( ) ) >= units . value ( units . convert ( self . target_temperature , units . celsius ) ) * nb_slots_optimization - difference_mean_temperature if correction is None : for t in range ( starting_time , ending_time , self . delta_time ) : outside_leverage = self . __external_leverage ( thermal_capacity = thermal_capacity , thermal_conductivity = thermal_conductivity , external_temperature = temperature [ t ] , internal_temperature = initial_temperature [ t ] ) inner_production = self . __internal_production ( internal_temperature = initial_temperature [ t ] , power_on = _power_on [ t ] , subject_efficiency = subject_efficiency , subject_energy = subject_energy , thermal_capacity = thermal_capacity ) problem += final_temperature [ t ] == outside_leverage + inner_production else : for t in range ( starting_time , ending_time , self . delta_time ) : outside_leverage = self . __external_leverage ( thermal_capacity = thermal_capacity , thermal_conductivity = thermal_conductivity , external_temperature = temperature [ t ] , internal_temperature = initial_temperature [ t ] ) inner_production = self . __internal_production ( internal_temperature = initial_temperature [ t ] , power_on = _power_on [ t ] , subject_efficiency = subject_efficiency , subject_energy = subject_energy , thermal_capacity = thermal_capacity ) correction_leverage = self . __external_leverage ( thermal_capacity = thermal_capacity , thermal_conductivity = correction . thermal_conductivity , external_temperature = correction . temperature , internal_temperature = initial_temperature [ t ] ) problem += final_temperature [ t ] == outside_leverage + inner_production + correction_leverage for t in range ( starting_time , ending_time , self . delta_time ) : problem += final_temperature [ t ] <= self . target_temperature + 0.5 * self . hysteresis + exceeding [ t ] problem += final_temperature [ t ] >= self . target_temperature - 0.5 * self . hysteresis - exceeding [ t ] problem += initial_temperature [ starting_time ] == units . value ( units . convert ( units ( self . thermal_process . temperature , units . kelvin ) , units . celsius ) ) problem += _power_on [ starting_time ] == first_decision for t in range ( starting_time + self . delta_time , ending_time , self . delta_time ) : problem += initial_temperature [ t ] == final_temperature [ t - self . delta_time ] problem . solve ( self . solver ) self . status = pulp . LpStatus [ problem . status ] if self . status is 'Infeasible' : raise RuntimeError ( "The problem isn't feasible" ) return [ dict ( [ ( k , pulp . value ( v ) ) for k , v in _power_on . items ( ) ] ) , dict ( [ ( k , pulp . value ( v ) ) for k , v in final_temperature . items ( ) ] ) ]
Optimization process.
Optimization process .
"""
        Optimization process. Can be use during the day-ahead decision period or in real time if errors. Set a
        on/off value for each slot.

        :param cost: Cost using for the minimization function
        :type cost: Vector

        :param first_decision: We can't decide for the current time. We need the first decision of the simulation

        :param correction: Used if errors has been detected in the past
        :type correction: Correction

        :param delta_temperature_outside_correction: Used if difference of temperature has been detected. The difference
                                                     is the delta between the forecase and the real value.
        :type delta_temperature_outside_correction: float

        :return A tuple of two dictionaries: The first dictionary : Key is the time and value is the on/off
                                             value decision. The second dictionary : Key is the time  t and value is the
                                             final temperature at time t.

        """ ########################################################### # # Set the data # # Value importation # external_thermal_element = self._outside_process # Get the temperature at time t # with couple # # Set the data # ########################################################### ########################################################### # # Prepare the problem # # Linearization of the problem # Decisions variables # Data # For each step of optimisation # Prepare the variables # Objective to minimize the cost # # Constraints # # Temperature mean during the time optimization must be the # target temperature (greater than because lost precision float) # # Prepare the problem # ########################################################### ########################################################### # # Resolution and return #
self
cost
first_decision
correction
delta_temperature_outside_correction