def _to_expr(self, qcc, lcc=None, constant=0): # no need to sort here, sort is done by str() on the fly. if qcc: linear_expr = LinearExpr(self._model, e=lcc, constant=constant, safe=True) quad = self._quad_factory.new_quad(quads=qcc, linexpr=linear_expr, safe=True) quad._transient = self._generate_transients return quad elif lcc or constant: linear_expr = LinearExpr(self._model, e=lcc, constant=constant, safe=True) linear_expr._transient = self._generate_transients return linear_expr else: return self.new_zero_expr()
def constant_expr(self, cst, safe_number=False, force_clone=False): if 0 == cst: return self.new_zero_expr() elif 1 == cst: if force_clone: return LinearExpr(self._model, e=None, constant=1, safe=True) else: return self._get_cached_one_expr() else: if safe_number: k = cst else: self_number_validation_fn = self.number_validation_fn k = self_number_validation_fn(cst) if self_number_validation_fn else cst return LinearExpr(self._model, e=None, constant=k, safe=True)
def _get_cached_one_expr(self): if self.one_expr is None: self.one_expr = LinearExpr(self._model, e=None, constant=1, safe=True) return self.one_expr
def constant_expr(self, cst, safe_number=False): if 0 == cst: return self.new_zero_expr() elif self.use_constant_expr: return self._new_constant_expr(cst, safe_number=safe_number) else: # pragma: no cover if safe_number: k = cst else: self_number_validation_fn = self.number_validation_fn k = self_number_validation_fn(cst) if self_number_validation_fn else cst return LinearExpr(self._model, e=None, constant=k, safe=True)
def linear_expr(self, arg=None, constant=0, name=None, safe=False, transient=False): return LinearExpr(self._model, arg, constant, name, safe=safe, transient=transient)
def new_one_expr(self): return LinearExpr(self._model, e=None, constant=1, safe=True)
# Decision variable number_of_units = m.integer_var_dict([u.name for u in units], name='number_of_units') number_of_units # Decision expressions m.barracks_build_time = m.sum([ number_of_units[u.name] * u.recruit_time_in_seconds for u in barracks_units ]) m.hoo_build_time = m.sum([ number_of_units[u.name] * u.recruit_time_in_seconds for u in hoo_units ]) m.totalNegativeAttack = LinearExpr.negate( m.sum([number_of_units[u.name] * u.att_strength for u in units])) m.totalFood = m.sum([number_of_units[u.name] * u.food for u in units]) # Constraints ct_food_no_church = m.add_constraints( [m.totalFood <= 20596, m.totalFood >= 20580]) ct_must_have_rams = m.add_constraint(number_of_units['ram'] >= 250) ct_build_time_less_than_4_weeks_barracks = m.add_constraints([ 1 <= m.barracks_build_time, (m.barracks_build_time <= build_time_limit_seconds) ]) ct_build_time_less_than_4_weeks_hoo = m.add_constraints([ 1 <= m.hoo_build_time, (m.hoo_build_time <= build_time_limit_seconds) ])