samedi 18 juillet 2020

how can I put variables into if statement using python, pyomo and a linear solver(Gurobi)?

I'm implementing an optimization problem in pyomo. However, I need to define a constraint based on temperatures variables by using if statement. The constraint is like: if self.b.temperatures['supply',t]=self.b.temperatures['return',t] then: self.b.temperatures['supply',t]=self.b.temperatures['supply',t-1] else:
self.b.heat_flow[t]=(self.b.temperatures['supply',t]-self.b.temperatures['return',t])*self.cp * b.mass_flow[t]

I know that I cannot use a variable in if statement. So, now I'm looking for some tricks to still implement this constraint. A part of the code is as below:

self.block.heat_flow = Var(self.TIME, within=NonNegativeReals) lines = self.params['lines'].v()

        def _mass_flow(b, t):
            return self.params['mass_flow'].v(t)

        self.block.mass_flow = Param(self.TIME, rule=_mass_flow)

        
        def _decl_init_heat_flow(b):
            return b.heat_flow[0] == (
                    self.params['temperature_supply'].v() -
                    self.params['temperature_return'].v()) * \
                   self.cp * b.mass_flow[0]

        self.block.decl_init_heat_flow = Constraint(
            rule=_decl_init_heat_flow)

        self.block.temperatures = Var(lines, self.TIME)

Aucun commentaire:

Enregistrer un commentaire