Esempio n. 1
0
    def new_constraint_status_var(self, ct):
        # INTERNAL
        model = self._model
        binary_vartype = model.binary_vartype
        status_var_fmt = self.status_var_fmt
        if model.ignore_names or status_var_fmt is None:
            varname = None
        else:
            # use name if any else use truncated ct string representation
            base_varname = self.status_var_fmt.format(
                ct.name or str_holo(ct, maxlen=20))
            # if name is already taken, use unique index at end to disambiguate
            varname = model._get_non_ambiguous_varname(base_varname)

        svar = Var(model,
                   binary_vartype,
                   lb=0,
                   ub=1,
                   _safe_lb=True,
                   _safe_ub=True,
                   name=varname)
        svar.notify_origin(ct)  # generated

        idx = self._engine.create_one_variable(binary_vartype, 0, 1, varname)
        model._register_one_var(svar, idx, varname)
        return svar
Esempio n. 2
0
    def new_multitype_var_list(self,
                               size,
                               vartypes,
                               lbs=None,
                               ubs=None,
                               names=None,
                               key_format=None):
        if not size:
            return []
        mdl = self._model
        assert size == len(vartypes)
        assert size == len(lbs)
        assert size == len(ubs)
        assert size == len(names)

        allvars = [
            Var(mdl,
                vartypes[k],
                names[k] if names[k] else None,
                lbs[k],
                ubs[k],
                _safe_lb=True,
                _safe_ub=True) for k in fast_range(size)
        ]

        indices = self._engine.create_multitype_variables(
            size, vartypes, lbs, ubs, names)
        mdl._register_block_vars(allvars, indices, names)
        return allvars
Esempio n. 3
0
 def new_safe_var(self, vartype, lb=None, ub=None, varname=None):
     # INTERNAL
     self_model = self._model
     used_varname = None if self_model.ignore_names else varname
     rlb = vartype._lb if lb is None else lb
     rub = vartype._ub if ub is None else ub
     var = Var(self_model, vartype, used_varname, rlb, rub, _safe_lb=True, _safe_ub=True)
     idx = self._engine.create_one_variable(vartype, rlb, rub, varname)
     self_model._register_one_var(var, idx, used_varname)
     return var
Esempio n. 4
0
    def new_var(self, vartype, lb=None, ub=None, varname=None):
        self_model = self._model
        self._checker.check_var_domain(lb, ub, varname)
        logger = self_model.logger
        rlb = vartype.resolve_lb(lb, logger)
        rub = vartype.resolve_ub(ub, logger)
        used_varname = None if self_model.ignore_names else varname
        var = Var(self_model, vartype, used_varname, rlb, rub, _safe_lb=True, _safe_ub=True)

        idx = self._engine.create_one_variable(vartype, rlb, rub, varname)
        self_model._register_one_var(var, idx, varname)
        return var
Esempio n. 5
0
    def new_var_list(self,
                     var_container,
                     key_seq,
                     vartype,
                     lb=None,
                     ub=None,
                     name=str,
                     dimension=1,
                     key_format=None):
        number_of_vars = len(key_seq)
        if 0 == number_of_vars:
            return []

        # compute defaults once
        default_lb = vartype.default_lb
        default_ub = vartype.default_ub

        xlbs = self._expand_bounds(key_seq,
                                   lb,
                                   default_lb,
                                   number_of_vars,
                                   true_if_lb=True)
        xubs = self._expand_bounds(key_seq,
                                   ub,
                                   default_ub,
                                   number_of_vars,
                                   true_if_lb=False)
        # at this point both list are either [] or have size numberOfVars

        all_names = self._expand_names(key_seq, name, dimension, key_format)

        safe_lbs = not xlbs
        safe_ubs = not xubs
        if xlbs and xubs:
            self._checker.check_vars_domain(xlbs, xubs, all_names)

        mdl = self._model
        allvars = [
            Var(mdl,
                vartype,
                all_names[k] if all_names else None,
                xlbs[k] if xlbs else default_lb,
                xubs[k] if xubs else default_ub,
                container=var_container,
                _safe_lb=safe_lbs,
                _safe_ub=safe_ubs) for k in fast_range(number_of_vars)
        ]

        # query the engine for a list of indices.
        indices = self._engine.create_variables(len(key_seq), vartype, xlbs,
                                                xubs, all_names)
        mdl._register_block_vars(allvars, indices, all_names)
        return allvars
 def get_noise(variable: Var) -> float:
     stdev = 1 + (4000 if 'x0,' in variable.get_name() else 1000) / (
         (epoch_num + 1) * self.envt.NUM_AGENTS)
     return abs(gauss(0, stdev)) if is_training else 0