Esempio n. 1
0
    def addConstraints(self, model):
        """
      Add specific constraints for MultipleKnapsack problems
      @ In, model, pyomo model instance, pyomo abstract model
      @ Out, model, pyomo model instance, pyomo abstract model
    """
        model = KnapsackBase.addConstraints(self, model)

        def constraintX(model, i):
            """ sum of investments over knapsacks should less or equal than bounds """
            return (self.lowerBounds[i],
                    sum(model.x[i, m]
                        for m in model.capitals), self.upperBounds[i])

        model.constraintX = pyomo.Constraint(model.investments,
                                             rule=constraintX)

        def constraintCapacity(model, m, t):
            """Knapsacks capacity constraints"""
            return sum(
                model.costs[i, t] * model.x[i, m]
                for i in model.investments) <= model.available_capitals[m, t]

        model.constraintCapacity = pyomo.Constraint(model.capitals,
                                                    model.time_periods,
                                                    rule=constraintCapacity)

        if self.mandatory is not None:

            def constraintRegulatory(model, i):
                """Regulatory constraints, always required projects/investments"""
                return sum(model.x[i, m] for m in model.capitals) == 1

            model.constraintRegulatory = pyomo.Constraint(
                model.mandatory, rule=constraintRegulatory)

        if self.uncertainties is not None:

            def consistentConstraintI(model, i, j):
                """Constraint for variable y if priority project selection is required"""
                if i == j:
                    return model.y[i, j] == model.y[j, i]
                else:
                    return sum(
                        model.x[j, m]
                        for m in model.capitals) + model.y[i, j] - 1 <= sum(
                            model.x[i, m] for m in model.capitals)

            model.consistentConstraintI = pyomo.Constraint(
                model.investments,
                model.investments,
                rule=consistentConstraintI)

        return model
Esempio n. 2
0
File: MCKP.py Progetto: milljm/LOGOS
 def addConstraints(self, model):
     """
   Add specific constraints for MCKP problems
   @ In, model, pyomo model instance, pyomo abstract model
   @ Out, model, pyomo model instance, pyomo abstract model
 """
     model = KnapsackBase.addConstraints(self, model)
     # constraint (1d)
     model.constraintCapacity = pyomo.Constraint(
         model.resources, model.time_periods, rule=self.constraintCapacity)
     # constraint (1e) and (1f)
     # last option of any project will be denoted as "non-selection" option
     if self.mandatory is not None:
         model.constraintRegulatory = pyomo.Constraint(
             model.mandatory, rule=self.constraintRegulatory)
     # Special handles for required and DoNothing options
     # The options in input file can include DoNothing, but not required to be selected by optimization
     # Only the investment added to mandatory will be selected.
     # constraint to handle 'DoNothing' options --> (1f)
     if self.nonSelection:
         model.constraintX = pyomo.Constraint(
             model.investments, rule=self.constraintXNonSelection)
     else:
         model.constraintX = pyomo.Constraint(model.investments,
                                              rule=self.constraintX)
     # constraint for scenario analysis
     if self.uncertainties is not None:
         if self.nonSelection:
             # constraint (1c) --> optional
             model.consistentConstraintI = pyomo.Constraint(
                 model.investments,
                 model.investments,
                 rule=self.consistentConstraintINonSelection)
             # constraint (1j) including both non-selection and regulatory mandated options
             model.consistentConstraintII = pyomo.Constraint(
                 model.investments,
                 model.investmentOption,
                 rule=self.consistentConstraintIINoSelection)
         else:
             # constraint (1c) --> optional
             model.consistentConstraintI = pyomo.Constraint(
                 model.investments,
                 model.investments,
                 rule=self.consistentConstraintI)
             # constraint (1j)
             model.consistentConstraintII = pyomo.Constraint(
                 model.investments,
                 model.investmentOption,
                 rule=self.consistentConstraintII)
     return model
Esempio n. 3
0
    def addConstraints(self, model):
        """
      Add specific constraints for SingleKnapsack problems
      @ In, model, pyomo model instance, pyomo abstract model
      @ Out, model, pyomo model instance, pyomo abstract model
    """
        model = KnapsackBase.addConstraints(self, model)

        def constraintCapacity(model, t):
            """Knapsacks capacity constraints"""
            return sum(
                model.costs[i, t] * model.x[i]
                for i in model.investments) <= model.available_capitals[t]

        model.constraintCapacity = pyomo.Constraint(model.time_periods,
                                                    rule=constraintCapacity)

        if self.mandatory is not None:

            def constraintRegulatory(model, i):
                """Regulatory constraints, always required projects/investments"""
                return model.x[i] == 1

            model.constraintRegulatory = pyomo.Constraint(
                model.mandatory, rule=constraintRegulatory)

        if self.uncertainties is not None:

            def consistentConstraintI(model, i, j):
                """Constraint for variable y if priority project selection is required"""
                if i == j:
                    return model.y[i, j] == model.y[j, i]
                else:
                    return model.x[j] + model.y[i, j] - 1 <= model.x[i]

            model.consistentConstraintI = pyomo.Constraint(
                model.investments,
                model.investments,
                rule=consistentConstraintI)

        return model