def test_param_from_pandas(self): # Test issue #68 model = ConcreteModel() model.I = Set(initialize=range(6)) model.P0 = Param(model.I, initialize={ 0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0 }) model.P1 = Param(model.I, initialize=pd.Series({ 0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0 }).to_dict()) model.P2 = Param(model.I, initialize=pd.Series({ 0: 400.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 240.0 })) #model.pprint() self.assertEqual(list(model.P0.values()), list(model.P1.values())) self.assertEqual(list(model.P0.values()), list(model.P2.values())) model.V = Var(model.I, initialize=0) def rule(m, l): return -m.P0[l] <= m.V[l] model.Constraint0 = Constraint(model.I, rule=rule) def rule(m, l): return -m.P1[l] <= m.V[l] model.Constraint1 = Constraint(model.I, rule=rule) def rule(m, l): return -m.P2[l] <= m.V[l] model.Constraint2 = Constraint(model.I, rule=rule)
def __init__(self, data, weights=None, nvar=None, objective=True): if nvar is None: nvar = len(data) if weights is None: weights = np.ones(len(data)) / nvar if len(data) != len(weights): print("Data and weights don't match in length") return model = ConcreteModel() # Number of original variables model.nvar = Param(within=NonNegativeIntegers, initialize=nvar) # Number of lines in data model.n = Param(within=NonNegativeIntegers, initialize=np.shape(data)[0]) # Number of columns in data model.m = Param(within=NonNegativeIntegers, initialize=np.shape(data)[1]) model.I = RangeSet(0, model.n - 1) model.J = RangeSet(0, model.m - 1) # Initialize all x_ij = 0.0, when j != 0, and all x_i0 = 1.0 model.x = Var(model.I, model.J, domain=Binary, initialize=0) for i in model.I: model.x[i, 0].value = 1.0 # Initialize c_ij from given data def c_init(model, i, j): return data[i, j] model.c = Param(model.I, model.J, initialize=c_init) # Initialize w_i from given parameter) def w_init(model, i): return weights[i] model.w = Param(model.I, initialize=w_init) if objective: model.OBJ = Objective(rule=self.obj_fun, sense=maximize) def const(model, i): ''' Constraint: Given line i has only one 1 \sum_{i=1}^{n}x_{ij} = 1''' return sum(model.x[i, j] for j in model.J) == 1 model.Constraint1 = Constraint(model.I, rule=const) self.model = model self._modelled = True
def __init__(self, data): self._solved = False self.res = None model = ConcreteModel() # Number of lines in data model.n = Param(within=NonNegativeIntegers, initialize=len(data)) # Number of columns in data model.m = Param(within=NonNegativeIntegers, initialize=len(list(data))) model.I = RangeSet(0, model.n - 1) model.J = RangeSet(0, model.m - 1) # Initialize all x_ij = 0.0, when j != 0, and all x_i0 = 1.0 model.x = Var(model.I, model.J, domain=Binary, initialize=0.0) for i in model.I: model.x[i, 0].value = 1.0 # Initialize c_ij from given data def c_init(model, i, j): return data.values[i, j] model.c = Param(model.I, model.J, initialize=c_init) '''Objective function: Formulate problem as binary problem. \sum_{i=1}^{n} \sum_{j=1}^{m} c_{ij}*x_{ij}''' def obj_fun(model): return sum( sum(model.x[i, j] * model.c[i, j] for i in model.I) for j in model.J) model.OBJ = Objective(rule=obj_fun, sense=maximize) ''' Constraint: Given line i has only one 1 \sum_{i=1}^{n}x_{ij} = 1''' def const(model, i): return sum(model.x[i, j] for j in model.J) == 1 model.Constraint1 = Constraint(model.I, rule=const) self.model = model self._modelled = True
model.OBJ = Objective(expr=obj_expression(model)) def _e(model): return np.dot(values, list(model.ct)) model.e = Expression([0], rule=_e) def constr(model): return model.e == 88 model.Constraint1 = Constraint(expr=constr(model)) def ObjRule(model): return 2 * model.x[1] + 3 * model.x[2] model.g = Objective(rule=ObjRule) prob = model.create() optim = SolverFactory('glpk') result = optim.solve(prob, tee=True) prob.load(result) # all variables prob.display()