Advertisement
Guest User

Untitled

a guest
May 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. from science_optimization.builder import BuilderOptimizationProblem
  2. from science_optimization.builder import Objective
  3. from science_optimization.builder import Variable
  4. from science_optimization.builder import Constraint
  5. from science_optimization.function import FunctionsComposite
  6.  
  7.  
  8. class GenericProblem(BuilderOptimizationProblem):
  9.     """Concrete builder implementation.
  10.  
  11.    This class builds a generic optimization problem.
  12.  
  13.    """
  14.  
  15.     # objective function(s)
  16.     _f = None
  17.  
  18.     # equality constraint function(s)
  19.     _eq_cons = None
  20.  
  21.     # inequality constraint function(s)
  22.     _ineq_cons = None
  23.  
  24.     # the variables' bounds
  25.     _x_bounds = None
  26.  
  27.     def __init__(self, f, eq_cons, ineq_cons, x_bounds):
  28.         """Constructor of a generic optimization problem.
  29.  
  30.        Args:
  31.            f        : Objective functions.
  32.            eq_cons  : Equality constraint functions.
  33.            ineq_cons: Inequality constraint functions.
  34.            x_bounds : Lower bound and upper bounds.
  35.        """
  36.  
  37.         self.f = f
  38.         self.eq_cons = eq_cons
  39.         self.ineq_cons = ineq_cons
  40.         self.x_bounds = x_bounds
  41.  
  42.     @property
  43.     def f(self):
  44.         return self._f
  45.  
  46.     @property
  47.     def eq_cons(self):
  48.         return self._eq_cons
  49.  
  50.     @property
  51.     def ineq_cons(self):
  52.         return self._ineq_cons
  53.  
  54.     @property
  55.     def x_bounds(self):
  56.         return self._x_bounds
  57.  
  58.     @f.setter
  59.     def f(self, value):
  60.         self._f = value
  61.  
  62.     @eq_cons.setter
  63.     def eq_cons(self, value):
  64.         self._eq_cons = value
  65.  
  66.     @ineq_cons.setter
  67.     def ineq_cons(self, value):
  68.         self._ineq_cons = value
  69.  
  70.     @x_bounds.setter
  71.     def x_bounds(self, value):
  72.         self._x_bounds = value
  73.  
  74.     def build_objectives(self):
  75.  
  76.         obj_fun = FunctionsComposite()
  77.  
  78.         for f in self.f:
  79.             obj_fun.add(f)
  80.  
  81.         objective = Objective(objective=obj_fun)
  82.  
  83.         return objective
  84.  
  85.     def build_constraints(self):
  86.  
  87.         eq_cons = FunctionsComposite()
  88.         ineq_cons = FunctionsComposite()
  89.  
  90.         for eq_g in self.eq_cons:
  91.             eq_cons.add(eq_g)
  92.  
  93.         for ineq_g in self.ineq_cons:
  94.             ineq_cons.add(ineq_g)
  95.  
  96.         constraints = Constraint(eq_cons=eq_cons, ineq_cons=ineq_cons)
  97.  
  98.         return constraints
  99.  
  100.     def build_variables(self):
  101.  
  102.         variables = Variable(x_min=self.x_bounds[:, 0].reshape(-1, 1),
  103.                              x_max=self.x_bounds[:, 1].reshape(-1, 1))
  104.  
  105.         return variables
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement