Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def RunAllIntegerExampleCppStyleAPI():
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'C++ style API')
RunIntegerExampleCppStyleAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
def RunAllIntegerExampleNaturalLanguageAPI():
if hasattr(pywraplp.Solver, 'GLPK_MIXED_INTEGER_PROGRAMMING'):
Announce('GLPK', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CBC_MIXED_INTEGER_PROGRAMMING'):
Announce('CBC', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'SCIP_MIXED_INTEGER_PROGRAMMING'):
Announce('SCIP', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.SCIP_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'GUROBI_MIXED_INTEGER_PROGRAMMING'):
Announce('GUROBI', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.GUROBI_MIXED_INTEGER_PROGRAMMING)
if hasattr(pywraplp.Solver, 'CPLEX_MIXED_INTEGER_PROGRAMMING'):
Announce('CPLEX', 'natural language API')
RunIntegerExampleNaturalLanguageAPI(
pywraplp.Solver.CPLEX_MIXED_INTEGER_PROGRAMMING)
38.4, 24.6, 217, 0
], ['Coffee', '1 lb.', 22.4, 0, 0, 0, 0, 0, 4, 5.1, 50,
0], ['Tea', '1/4 lb.', 17.4, 0, 0, 0, 0, 0, 0, 2.3, 42, 0],
['Cocoa', '8 oz.', 8.6, 8.7, 237, 3, 72, 0, 2, 11.9, 40, 0], [
'Chocolate', '8 oz.', 16.2, 8.0, 77, 1.3, 39, 0, 0.9, 3.4, 14, 0
], ['Sugar', '10 lb.', 51.7, 34.9, 0, 0, 0, 0, 0, 0, 0, 0],
['Corn Syrup', '24 oz.', 13.7, 14.7, 0, 0.5, 74, 0, 0, 0, 5, 0], [
'Molasses', '18 oz.', 13.6, 9.0, 0, 10.3, 244, 0, 1.9, 7.5, 146,
0
], [
'Strawberry Preserves', '1 lb.', 20.5, 6.4, 11, 0.4, 7, 0.2,
0.2, 0.4, 3, 0
]]
# Instantiate a Glop solver, naming it LinearExample.
solver = pywraplp.Solver('StiglerDietExample',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# Declare an array to hold our variables.
foods = [solver.NumVar(0.0, solver.infinity(), item[0]) for item in data]
# Objective function: Minimize the sum of (price-normalized) foods.
objective = solver.Objective()
for food in foods:
objective.SetCoefficient(food, 1)
objective.SetMinimization()
# Create the constraints, one per nutrient.
constraints = []
for i, nutrient in enumerate(nutrients):
constraints.append(solver.Constraint(nutrient[1], solver.infinity()))
for j, item in enumerate(data):
def main(sol='CBC'):
# Create the solver.
print('Solver: ', sol)
if sol == 'GLPK':
# using GLPK
solver = pywraplp.Solver('CoinsGridGLPK',
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
else:
# Using CBC
solver = pywraplp.Solver('CoinsGridCLP',
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
#
# data
#
n = 4
price = [50, 20, 30, 80] # in cents
limits = [500, 6, 10, 8] # requirements for each nutrition type
# nutritions for each product
calories = [400, 200, 150, 500]
chocolate = [3, 2, 0, 0]
def main():
"""Entry point of the program"""
# Instantiate a Glop solver, naming it LinearExample.
solver = pywraplp.Solver('LinearExample',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# Create the two variables and let them take on any value.
x = solver.NumVar(-solver.infinity(), solver.infinity(), 'x')
y = solver.NumVar(-solver.infinity(), solver.infinity(), 'y')
# Objective function: Maximize 3x + 4y.
objective = solver.Objective()
objective.SetCoefficient(x, 3)
objective.SetCoefficient(y, 4)
objective.SetMaximization()
# Constraint 0: x + 2y <= 14.
constraint0 = solver.Constraint(-solver.infinity(), 14)
constraint0.SetCoefficient(x, 1)
constraint0.SetCoefficient(y, 2)
def RunMCLPexampleCppStyleAPI(optimization_problem_type, p, SD):
""" Example of simple MCLP program with the C++ style API."""
solver = pywraplp.Solver('RunIntegerExampleCppStyleAPI', optimization_problem_type)
# Create a global version of:
# Facility Site Variable X
X = [None] * numSites
# Coverage Variable Y - NOTE!!: Matt used the Minimization form where:
# Y = 1 if it is NOT COVERED by a facility located within SD of demand Yi
Y = [None] * numDemands
computeCoverageMatrix(SD)
BuildModel(solver, X, Y, p)
SolveModel(solver)
demandCovered = demandTotal - solver.Objective().Value()
def main(sol='CBC'):
# Create the solver.
# using GLPK
if sol == 'GLPK':
solver = pywraplp.Solver('CoinsGridGLPK',
pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)
else:
# Using CLP
solver = pywraplp.Solver('CoinsGridCLP',
pywraplp.Solver.CLP_LINEAR_PROGRAMMING)
# data
rows = 3
cols = 3
game = [[3.0, -1.0, -3.0], [-2.0, 4.0, -1.0], [-5.0, -6.0, 2.0]]
#
# declare variables
#
#
# row player
#
x1 = [solver.NumVar(0, 1, 'x1[%i]' % i) for i in range(rows)]
def linprog(f, A, b):
'''
Solve the following linear programming problem
maximize_x (f.T).dot(x)
subject to A.dot(x) <= b
where A is a sparse matrix (coo_matrix)
f is column vector of cost function associated with variable
b is column vector
'''
# flatten the variable
f = f.ravel()
b = b.ravel()
solver = pywraplp.Solver('SolveReviewerAssignment',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
infinity = solver.Infinity()
n, m = A.shape
x = [[]] * m
c = [0] * n
for j in range(m):
x[j] = solver.NumVar(-infinity, infinity, 'x_%u' % j)
# state objective function
objective = solver.Objective()
for j in range(m):
objective.SetCoefficient(x[j], f[j])
objective.SetMaximization()