def formulate(cp): prob = dippy.DipProblem( "Coke", display_mode='xdot', # layout = 'bak', display_interval=None, ) # create variables LOC_SIZES = [(l, s) for l in cp.LOCATIONS for s in cp.SIZES] buildVars = LpVariable.dicts("Build", LOC_SIZES, cat=LpBinary) # create arcs flowVars = LpVariable.dicts("Arcs", cp.ARCS) BIG_M = max(sum(cp.supply.values()), sum(cp.demand.values())) for a in cp.ARCS: flowVars[a].bounds(0, BIG_M) # objective prob += 1e6 * lpSum(buildVars[(l, s)] * cp.build_costs[s] \ for (l, s) in LOC_SIZES) + \ lpSum(flowVars[(s, d)] * cp.transport_costs[(s, d)] \ for (s, d) in cp.ARCS), "min" # plant availability - assumes that SIZES are numeric, # which they should be for loc in cp.LOCATIONS: prob += lpSum(flowVars[(loc, i)] for i in cp.CUSTOMERS) \ <= lpSum(buildVars[(loc, s)] * s for s in cp.SIZES) # one size for loc in cp.LOCATIONS: prob += lpSum(buildVars[(loc, s)] for s in cp.SIZES) == 1 # conserve flow (mines) # flows are in terms of tonnes of coke for m in cp.MINES: prob += lpSum(flowVars[(m, j)] for j in cp.LOCATIONS) \ <= cp.supply[m] # conserve flow (locations) # convert from coal to coke for loc in cp.LOCATIONS: prob += lpSum(flowVars[(m, loc)] for m in cp.MINES) - \ cp.conversion_factor * \ lpSum(flowVars[(loc, c)] for c in cp.CUSTOMERS) \ >= 0 for c in cp.CUSTOMERS: prob += lpSum(flowVars[(loc, c)] for loc in cp.LOCATIONS) \ >= cp.demand[c] prob.cp = cp prob.buildVars = buildVars prob.flowVars = flowVars return prob
def formulate(bpp): prob = dippy.DipProblem( "Bin Packing", display_mode='xdot', # layout = 'bak', display_interval=None, ) assign_vars = LpVariable.dicts("x", [(i, j) for i in bpp.ITEMS for j in bpp.BINS], cat=LpBinary) use_vars = LpVariable.dicts("y", bpp.BINS, cat=LpBinary) waste_vars = LpVariable.dicts("w", [(j, k) for j in bpp.BINS for k in bpp.LIMITS], 0, None) prob += lpSum(use_vars[j] for j in bpp.BINS), "min_bins" for j in bpp.BINS: for k in bpp.LIMITS: prob += lpSum(bpp.volume[i, k] * assign_vars[i, j] for i in bpp.ITEMS) \ + waste_vars[j, k] == bpp.capacity[k] * use_vars[j] for i in bpp.ITEMS: prob += lpSum(assign_vars[i, j] for j in bpp.BINS) == 1 for i in bpp.ITEMS: for j in bpp.BINS: prob += assign_vars[i, j] <= use_vars[j] for n in range(0, len(bpp.BINS) - 1): prob += use_vars[bpp.BINS[n]] >= use_vars[bpp.BINS[n + 1]] # Attach the problem data and variable dictionaries to the DipProblem prob.bpp = bpp prob.assign_vars = assign_vars prob.use_vars = use_vars prob.waste_vars = waste_vars return prob
import sys #sys.path.append("C:\\COIN\\GIMPy\\GrUMPy\\trunk") #sys.path.append("C:\\COIN\\GIMPy\\trunk") import coinor.pulp as pulp from pulp import * import dippy try: import path except ImportError: pass prob = dippy.DipProblem("Dippy Example", display_mode='pygame', display_interval=1) x1 = LpVariable("x_1", 0, None, LpInteger) x2 = LpVariable("x_2", 0, cat=LpInteger) prob += -x1 - x2, "min" prob += -2 * x1 + 2 * x2 >= 1 prob += -8 * x1 + 10 * x2 <= 13 dippy.Solve(prob, { 'CutCGL': 0, }) for var in prob.variables():