
from __future__ import print_functionimport sysfrom ortools.linear_solver import pywraplp
def main():
solver = pywraplp.Solver('LinearExample', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) # data capacity = [150, 150, 200, 400] value = [96, 76, 56, 11, 86, 10, 66, 86, 83, 12, 9, 81] nb_items = len(value) nb_resources = len(capacity) items = list(range(nb_items)) resources = list(range(nb_resources)) # Assign variable: assign={} for r in resources: assign[r] = [solver.IntVar(0, 1, 'Assign[%r][%d]' % (r, j)) for j in items] # objective: minimize number of trucks: z = solver.Sum((value[i] * assign[r][i]) for i in items for r in resources) ### constraints ### # Assign: for j in items: solver.Add(solver.Sum(assign[r][j] for r in resources) == 1) for r in resources: solver.Add(solver.Sum(value[j] * assign[r][j] for j in items) <= capacity[r])
## Objective: objective = solver.Maximize(z)
# solution and search solver.Solve() print('z: ', int(solver.Objective().Value())) print('walltime :', solver.WallTime(), 'ms') print(assign)
if __name__ == '__main__': main()constraints = [0] * len(nutrients) for i in range(0, len(nutrients)): constraints[i] = solver.Constraint(nutrients[i][1], solver.infinity()) for j in range(0, len(data)): constraints[i].SetCoefficient(food[j], data[j][i+3])--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ortools.sat.python import cp_model
# You need to subclass the cp_model.CpSolverSolutionCallback class.
class VarArrayAndObjectiveSolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
def __init__(self, variables):
cp_model.CpSolverSolutionCallback.__init__(self)
self.__variables = variables
self.__solution_count = 0
def on_solution_callback(self):
print('Solution %i' % self.__solution_count)
print(' objective value = %i' % self.ObjectiveValue())
for v in self.__variables:
print(' %s = %i' % (v, self.Value(v)), end=' ')
print()
self.__solution_count += 1
def solution_count(self):
return self.__solution_count
def SolveAndPrintIntermediateSolutionsSampleSat():
"""Showcases printing intermediate solutions found during search."""
# Creates the model.
model = cp_model.CpModel()
model.AddNoOverlap2D()
# Creates the variables.
num_vals = 3
x = model.NewIntVar(0, num_vals - 1, 'x')
y = model.NewIntVar(0, num_vals - 1, 'y')
z = model.NewIntVar(0, num_vals - 1, 'z')
# Creates the constraints.
model.Add(x != y)
model.Minimize(x + 2 * y + 3 * z)
# Creates a solver and solves.
solver = cp_model.CpSolver()
solution_printer = VarArrayAndObjectiveSolutionPrinter([x, y, z])
status = solver.SolveWithSolutionCallback(model, solution_printer)
print('Status = %s' % solver.StatusName(status))
print('Number of solutions found: %i' % solution_printer.solution_count())
SolveAndPrintIntermediateSolutionsSampleSat()Please, I would gladly accept any example on how to insert sum in a constraint or objective function.
--