Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Project: MAT 3770
Path: linProg.sage
Views: 341##########################################1# Linear Programming Scripts2##########################################3#4# Johann Thiel5# ver 11.17.246# Functions to solve linear7# programming problems.8#9##########################################1011##########################################12# Necessary modules13##########################################14from sage.numerical.backends.generic_backend import get_solver15import sage.numerical.backends.glpk_backend as backend16from tabulate import tabulate17##########################################1819##########################################20# Generic linear programming solver that21# produces a sensitivity report22##########################################23# var = list of variable names24# con = list of constraint names25# ob = list of coefficients of objective26# functions27# M = matrix of constraint coefficients28# inq = list of inequality direction29# 1 for '<=', -1 for '>=' and30# 0 for '='.31# bnd = list of constraint bounds32# mx = Boolean to determine if the33# problem is a maximization (True)34# or minimization (False) problem.35# Default is set to maximization.36##########################################37def lp(var, con, ob, M, inq, bnd, mx=True):38# loads the solver39q = get_solver(solver = 'GLPK')40# sets solver to min, max otherwise41if not mx:42q.set_sense(-1)43# adds all variables44for v in var:45q.add_variable(name=v)46# adds all constraints47for i in range(len(M)):48if inq[i] == 1:49q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), None, bnd[i], con[i])50elif inq[i] == -1:51q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], None, con[i])52else:53q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], bnd[i], con[i])54# sets objective55q.set_objective(ob)56q.solver_parameter(backend.glp_simplex_or_intopt, backend.glp_simplex_only)57# solves the problem58q.solve()59# produces sensitivity report60q.print_ranges()61##########################################6263##########################################64# Generic linear programming solver for65# integer programming problems (produces66# no sensitivity report)67##########################################68# var = list of variable names69# con = list of constraint names70# ob = list of coefficients of objective71# functions72# M = matrix of constraint coefficients73# inq = list of inequality direction74# 1 for '<=', -1 for '>=' and75# 0 for '='.76# bnd = list of constraint bounds77# mx = Boolean to determine if the78# problem is a maximization (True)79# or minimization (False) problem.80# Default is set to maximization.81##########################################82def lpInt(var, con, ob, M, inq, bnd, mx=True):83# loads the solver84q = get_solver(solver = 'GLPK')85# sets solver to min, max otherwise86if not mx:87q.set_sense(-1)88# adds all variables89for v in var:90q.add_variable(integer=True, name=v)91# adds all constraints92for i in range(len(M)):93if inq[i] == 1:94q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), None, bnd[i], con[i])95elif inq[i] == -1:96q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], None, con[i])97else:98q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], bnd[i], con[i])99# sets objective100q.set_objective(ob)101q.solver_parameter(backend.glp_simplex_then_intopt)102# solves the problem103q.solve()104# The following lines all produce an output report105if mx:106st = ' (max) '107else:108st = ' (min) '109sol = [q.get_variable_value(i) for i in range(q.ncols())]110print('Optimal'+st+'value: ', q.get_objective_value())111print('')112results = [[a,b] for a,b in zip(var,sol)]113print(tabulate(results, headers=['Variable', 'Value']))114print('')115slack = [[q.row_name(j), round(max(0,abs(bnd[j]-sum([a*b for a,b in zip(sol,M[j])]))),3)] for j in range(q.nrows())]116print(tabulate(slack, headers=['Constraint', 'Slack']))117##########################################118119##########################################120# Generic linear programming solver for121# binary integer programming problems122# (produces no sensitivity report)123##########################################124#125# var = list of variable names126# con = list of constraint names127# ob = list of coefficients of objective128# functions129# M = matrix of constraint coefficients130# inq = list of inequality direction131# 1 for '<=', -1 for '>=' and132# 0 for '='.133# bnd = list of constraint bounds134# mx = Boolean to determine if the135# problem is a maximization (True)136# or minimization (False) problem.137# Default is set to maximization.138##########################################139def lpBin(var, con, ob, M, inq, bnd, mx=True):140# loads the solver141q = get_solver(solver = 'GLPK')142# sets solver to min, max otherwise143if not mx:144q.set_sense(-1)145# adds all variables146for v in var:147q.add_variable(binary=True, name=v)148# adds all constraints149for i in range(len(M)):150if inq[i] == 1:151q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), None, bnd[i], con[i])152elif inq[i] == -1:153q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], None, con[i])154else:155q.add_linear_constraint(list(zip(range(len(M[0])), M[i])), bnd[i], bnd[i], con[i])156# sets objective157q.set_objective(ob)158q.solver_parameter(backend.glp_simplex_then_intopt)159# solves the problem160q.solve()161# The following lines all produce an output report162if mx:163st = ' (max) '164else:165st = ' (min) '166sol = [q.get_variable_value(i) for i in range(q.ncols())]167print('Optimal'+st+'value: ', q.get_objective_value())168print('')169results = [[a,b] for a,b in zip(va,sol)]170print(tabulate(results, headers=['Variable', 'Value']))171print('')172slack = [[q.row_name(j), round(max(0,abs(bnd[j]-sum([a*b for a,b in zip(sol,M[j])]))),3)] for j in range(q.nrows())]173print(tabulate(slack, headers=['Constraint', 'Slack']))174##########################################175176