def _to_sas_string(obj): if hasattr(obj, '_expr'): return obj._expr() elif isinstance(obj, str): return "'{}'".format(obj) elif isinstance(obj, tuple): return ', '.join(_to_sas_string(i) for i in obj) elif isinstance(obj, list): return '{{{}}}'.format(','.join([_to_sas_string(i) for i in obj])) elif isinstance(obj, range): if obj.step == 1: return '{}..{}'.format(_to_sas_string(obj.start), _to_sas_string(obj.stop - 1)) else: return '{}..{} by {}'.format(_to_sas_string(obj.start), _to_sas_string(obj.stop - 1), _to_sas_string(obj.step)) elif np.isinstance(type(obj), np.number): return str(obj) elif isinstance(obj, sasoptpy.abstract.Conditional): parent = obj._parent iters = get_iterators(parent) conds = get_conditions(parent) return get_iterators_optmodel_format(iters, conds) elif obj is None: return '.' else: raise TypeError('Cannot convert type {} to SAS string'.format( type(obj)))
def extract_list_value(tuplist, listname): """ Extracts values inside various object types Parameters ---------- tuplist : tuple Key combination to be extracted listname : dict or list or int or float or DataFrame or Series object List where the value will be extracted """ if listname is None: v = None elif isinstance(listname, dict): v = listname.get(get_first_member(tuplist), None) elif np.isinstance(type(listname), np.number): v = listname elif isinstance(listname, pd.DataFrame): if isinstance(listname.index, pd.MultiIndex): v = listname.loc[tuplist[:-1]][tuplist[-1]] else: v = listname.loc[tuplist] elif isinstance(listname, pd.Series): v = listname.loc[tuplist] else: raise ValueError('Parameter type {} is not valid for the operation'.\ format(type(listname))) return v
def expression_to_constraint(left, relation, right): if isinstance(right, list) and relation == 'E': e = left.copy() e.add_to_member_value('CONST', -1 * min(right[0], right[1])) ranged_constraint = sasoptpy.core.Constraint(exp=e, direction='E', crange=abs(right[1] - right[0])) return ranged_constraint elif not is_variable(left): if left._operator is None: r = left.copy() else: r = sasoptpy.core.Expression(0) r += left if np.isinstance(type(right), np.number): r._linCoef['CONST']['val'] -= right right = 0 elif is_expression(right): r -= right elif right is None: r.set_member_value('CONST', np.nan) generated_constraint = sasoptpy.core.Constraint(exp=r, direction=relation, crange=0, internal=True) return generated_constraint else: r = sasoptpy.core.Expression() for v in left._linCoef: r._add_coef_value(left._linCoef[v]['ref'], v, left._linCoef[v]['val']) if np.isinstance(type(right), np.number): r._linCoef['CONST']['val'] -= right else: for v in right._linCoef: if r.get_member(v): r.add_to_member_value(v, -1 * right.get_member_value(v)) else: r.copy_member(v, right) r.mult_member_value(v, -1) generated_constraint = sasoptpy.core.Constraint(exp=r, direction=relation, crange=0) return generated_constraint
def to_expression(cls, obj): if sasoptpy.core.util.is_expression(obj): return obj else: if np.isinstance(type(obj), np.number): r = Expression(name=str(obj)) r.add_to_member_value('CONST', obj) return r else: raise RuntimeError( 'Cannot convert type {} to Expression'.format(type(obj)))
def is_valid_ub(ub, variable_type): if ub is None: return False if ub == inf or ub == np.inf: return False if ub == 1 and variable_type == sasoptpy.BIN: return False if np.isinstance(type(ub), np.number): return True else: return True
def is_valid_lb(lb, variable_type): if lb is None: return False if lb == -inf or lb == -np.inf: return False if lb == 0 and variable_type == sasoptpy.BIN: return False if np.isinstance(type(lb), np.number): return True else: return False
def wrap_expression(e, abstract=False): """ Wraps expression inside another expression """ wrapper = sasoptpy.Expression() if hasattr(e, 'get_name') and e.get_name() is not None: name = e.get_name() else: name = get_next_name() if sasoptpy.core.util.is_expression(e): wrapper.set_member(key=name, ref=e, val=1) wrapper._abstract = e._abstract or abstract elif isinstance(e, dict): wrapper._linCoef[name] = {**e} elif isinstance(e, str): wrapper = sasoptpy.Auxiliary(base=e) elif np.isinstance(type(e), np.number): wrapper += e return wrapper
def is_valid_init(init, varible_type): if init is None: return False if np.isinstance(type(init), np.number): return True return False