Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
bounds = []
s = pywraplp.Solver('',pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
a = [[2],[-2]]
b = [3,-12]
x = s.NumVar(2,5,'x')
z,l = maximax(s,a,[x],b)
rc = s.Solve()
print('x = ',SolVal(x))
print('z = ',SolVal(z))
print('delta = ', SolVal(l))
main()
def main(sol="CBC"):
# Create the solver.
print("Solver: ", sol)
# using GLPK
if sol == "GLPK":
solver = pywraplp.Solver("CoinsGridGLPK",
pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)
else:
# Using CLP
solver = pywraplp.Solver("CoinsGridCLP",
pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
#
# data
#
# commodities
num_commodities = 77
C = list(range(num_commodities))
# days in a year
days = 365.25
# nutrients
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
#
def LinearProgrammingExample():
"""Linear programming sample."""
# Instantiate a Glop solver, naming it LinearExample.
# [START solver]
solver = pywraplp.Solver('LinearProgrammingExample',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
# [END solver]
# Create the two variables and let them take on any non-negative value.
# [START variables]
x = solver.NumVar(0, solver.infinity(), 'x')
y = solver.NumVar(0, solver.infinity(), 'y')
# [END variables]
# [START constraints]
# Constraint 0: x + 2y <= 14.
constraint0 = solver.Constraint(-solver.infinity(), 14)
constraint0.SetCoefficient(x, 1)
constraint0.SetCoefficient(y, 2)
# Constraint 1: 3x - y >= 0.
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()
try:
entry[key] = float(entry[key])
except ValueError:
pass
self.constraints_table = from_csv(nutrient_constraints_filename)
# clean up constraints table
for entry in self.constraints_table:
for key in entry:
try:
entry[key] = float(entry[key])
except ValueError:
pass
self.solver = pywraplp.Solver('diet_optimizer', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
self.create_variable_dict()
# treat these nutrient constraints as a percentage of the total calories
self.percent_constraints = {
'total fat (g)': {'calories_per_gram': 9},
}
self.create_constraints()
self.objective = self.solver.Objective()
for row in self.food_table:
name = row['description']
var = self.variable_dict[name]
calories_in_food = row[calories_name]
self.objective.SetCoefficient(var, calories_in_food)
self.objective.SetMinimization()
:param language: Implementation language of components
:return: A tuple: (updated isolates, new isolates)
"""
# Normalize entries (components and isolates)
components_names = sorted(component.name for component in components)
nb_components = len(components_names)
isolates_names = sorted(map_isolates.keys())
# Compute boundaries
max_isolates = max(len(components_names), len(isolates_names)) + 1
# Prepare the incompatibility matrix
incompat_matrix = self.__make_incompatibility_matrix(components_names)
# Prepare the problem solver
solver = ortools.Solver("Components distribution",
ortools.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
# Declare variables
# ... component on isolate (Iso_i <=> Iso_i_j = 1)
iso = {}
for i, name in enumerate(components_names):
for j in range(max_isolates):
iso[i, j] = solver.IntVar(0, 1, "{0} on {1}".format(name, j))
# ... assigned isolates (for the objective)
assigned_isolates = [solver.IntVar(0, 1, "Isolate {0}".format(i))
for i in range(max_isolates)]
# ... number of isolates for a component (must be 1)
nb_component_isolate = [solver.Sum(iso[i, j]
for j in range(max_isolates))
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
#
# max number of colors
# [we know that 4 suffices for normal maps]
nc = 5
# number of nodes
n = 11
# set of nodes
V = list(range(n))
num_edges = 20
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)
def SolveModel(solver, X, Y, p):
"""Solve the problem"""
result_status = solver.Solve()
# The problem has an optimal solution.
assert result_status == pywraplp.Solver.OPTIMAL, "The problem does not have an optimal solution!"
# The solution looks legit (when using solvers others than
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
assert solver.VerifySolution(1e-7, True)
return 1