def _generate_model(self): self.model = pmo.block() model = self.model model._name = self.description model.f = pmo.variable() model.x = pmo.variable(lb=1,ub=3) model.fi = pmo.parameter_dict() model.fi[1] = pmo.parameter(value=1.0) model.fi[2] = pmo.parameter(value=2.0) model.fi[3] = pmo.parameter(value=0.0) model.xi = pmo.parameter_dict() model.xi[1] = pmo.parameter(value=1.0) model.xi[2] = pmo.parameter(value=2.0) model.xi[3] = pmo.parameter(value=3.0) model.p = pmo.variable(domain=NonNegativeReals) model.n = pmo.variable(domain=NonNegativeReals) model.lmbda = pmo.variable_dict( (i, pmo.variable()) for i in range(1,4)) model.obj = pmo.objective(model.p+model.n) model.c1 = pmo.constraint_dict() model.c1[1] = pmo.constraint((0.0, model.lmbda[1], 1.0)) model.c1[2] = pmo.constraint((0.0, model.lmbda[2], 1.0)) model.c1[3] = pmo.constraint(0.0 <= model.lmbda[3]) model.c2 = pmo.sos2(model.lmbda.values()) model.c3 = pmo.constraint(sum(model.lmbda.values()) == 1) model.c4 = pmo.constraint(model.f==sum(model.fi[i]*model.lmbda[i] for i in model.lmbda)) model.c5 = pmo.constraint(model.x==sum(model.xi[i]*model.lmbda[i] for i in model.lmbda)) model.x.fix(2.75) # Make an empty SOS constraint model.c6 = pmo.sos2([])
def _generate_model(self): self.model = pmo.block() model = self.model model._name = self.description model.a1 = pmo.parameter(value=1.0) model.a2 = pmo.parameter_dict({1: pmo.parameter(value=1.0)}) model.a3 = pmo.parameter(value=1.0) model.a4 = pmo.parameter_dict({1: pmo.parameter(value=1.0)}) model.x = pmo.variable(domain=NonNegativeReals) model.y = pmo.variable(domain=NonNegativeReals) model.z1 = pmo.variable() model.z2 = pmo.variable() model.dummy_expr1 = pmo.expression(model.a1 * model.a2[1]) model.dummy_expr2 = pmo.expression(model.y / model.a3 * model.a4[1]) model.inactive_obj = pmo.objective(model.x + 3.0 * model.y + 1.0 + model.z1 - model.z2) model.inactive_obj.deactivate() model.p = pmo.parameter(value=0.0) model.obj = pmo.objective(model.p + model.inactive_obj) model.c1 = pmo.constraint( model.dummy_expr1 <= pmo.noclone(model.dummy_expr2)) model.c2 = pmo.constraint((2.0, model.x / model.a3 - model.y, 10)) model.c3 = pmo.constraint((0, model.z1 + 1, 10)) model.c4 = pmo.constraint((-10, model.z2 + 1, 0))
def _generate_model(self): self.model = pmo.block() model = self.model model._name = self.description model.a1 = pmo.parameter(value=1.0) model.a2 = pmo.parameter_dict( {1: pmo.parameter(value=1.0)}) model.a3 = pmo.parameter(value=1.0) model.a4 = pmo.parameter_dict( {1: pmo.parameter(value=1.0)}) model.x = pmo.variable(domain=NonNegativeReals) model.y = pmo.variable(domain=NonNegativeReals) model.z1 = pmo.variable() model.z2 = pmo.variable() model.dummy_expr1 = pmo.expression(model.a1*model.a2[1]) model.dummy_expr2 = pmo.expression(model.y/model.a3*model.a4[1]) model.inactive_obj = pmo.objective( model.x + 3.0*model.y + 1.0 + model.z1 - model.z2) model.inactive_obj.deactivate() model.p = pmo.parameter(value=0.0) model.obj = pmo.objective(model.p + model.inactive_obj) model.c1 = pmo.constraint(model.dummy_expr1 <= pmo.noclone(model.dummy_expr2)) model.c2 = pmo.constraint((2.0, model.x/model.a3 - model.y, 10)) model.c3 = pmo.constraint((0, model.z1 + 1, 10)) model.c4 = pmo.constraint((-10, model.z2 + 1, 0))
[125, 95, 90, 95], [45, 110, 95, 115], [50, 100, 90, 100], ] NUM_WORKERS = len(costs) NUM_TASKS = len(costs[0]) # Define the model model = pmo.block() # Create the sets model.set_workers = range(NUM_WORKERS) model.set_tasks = range(NUM_TASKS) # Create the parameters model.param_cost = pmo.parameter_dict() for _worker in model.set_workers: for _task in model.set_tasks: model.param_cost[(_worker, _task)] = pmo.parameter(costs[_worker][_task]) # Create the variables model.var_x = pmo.variable_dict() for _worker in model.set_workers: for _task in model.set_tasks: model.var_x[(_worker, _task)] = pmo.variable(domain=pmo.Binary) # Create the constraints ## Each worker is assigned to at most 1 task model.con_worker = pmo.constraint_list() for _worker in model.set_workers:
312] weights = [ 7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13] capacities = 850 model = pmo.block() # Create the sets model.set_labels = range(len(values)) # Create the parameters model.param_label_value = pmo.parameter_dict() model.param_label_weight = pmo.parameter_dict() for i in model.set_labels: model.param_label_value[i] = pmo.parameter(values[i]) model.param_label_weight[i] = pmo.parameter(weights[i]) # Create the variables model.var_pack = pmo.variable_dict() for i in model.set_labels: model.var_pack[i] = pmo.variable(domain=pmo.Binary) # Create the constraints model.con_capacity = pmo.constraint(sum(model.var_pack[i] * weights[i] for i in model.set_labels) <= capacities) # Create the objective model.obj = pmo.objective(sum(model.var_pack[i] * values[i] for i in model.set_labels), sense=pmo.maximize)
# @Sets_1 m.s = [1, 2] # @Sets_1 # @Sets_2 # [0,1,2] m.q = range(3) # @Sets_2 # @Parameters_single m.p = pmo.parameter(0) # @Parameters_single # @Parameters_dict # pd[1] = 0, pd[2] = 1 m.pd = pmo.parameter_dict() for k, i in enumerate(m.s): m.pd[i] = pmo.parameter(k) # @Parameters_dict # @Parameters_list # uses 0-based indexing # pl[0] = 0, pl[0] = 1, ... m.pl = pmo.parameter_list() for j in m.q: m.pl.append(pmo.parameter(j)) # @Parameters_list # @Variables_single m.v = pmo.variable(value=1, lb=1, ub=4) # @Variables_single
# # Blocks # # define a simple optimization model b = pmo.block() b.x = pmo.variable() b.c = pmo.constraint(expr=b.x >= 1) b.o = pmo.objective(expr=b.x) # define an optimization model with indexed containers b = pmo.block() b.p = pmo.parameter() b.plist = pmo.parameter_list(pmo.parameter() for i in range(10)) b.pdict = pmo.parameter_dict( ((i, j), pmo.parameter()) for i in range(10) for j in range(10)) b.x = pmo.variable() b.xlist = pmo.variable_list(pmo.variable() for i in range(10)) b.xdict = pmo.variable_dict( ((i, j), pmo.variable()) for i in range(10) for j in range(10)) b.c = pmo.constraint(b.x >= 1) b.clist = pmo.constraint_list( pmo.constraint(b.xlist[i] >= i) for i in range(10)) b.cdict = pmo.constraint_dict(((i, j), pmo.constraint(b.xdict[i, j] >= i * j)) for i in range(10) for j in range(10)) b.o = pmo.objective(b.x + sum(b.xlist) + sum(b.xdict.values())) #
# @Sets_1 # @Sets_2 # [0,1,2] m.q = range(3) # @Sets_2 # @Parameters_single m.p = pmo.parameter(0) # @Parameters_single # @Parameters_dict # pd[1] = 0, pd[2] = 1 m.pd = pmo.parameter_dict() for k,i in enumerate(m.s): m.pd[i] = pmo.parameter(k) # @Parameters_dict # @Parameters_list # uses 0-based indexing # pl[0] = 0, pl[0] = 1, ... m.pl = pmo.parameter_list() for j in m.q: m.pl.append( pmo.parameter(j)) # @Parameters_list