OR-Tools (Google Optimization Tools)

2020. 10. 24. 17:03분석 Python/Packages

728x90

python에서 constraint 툴을 찾다가 발견함.

 

 

combinatorial optimization에 사용할 수 있는 open source tool 

 

여러 언어에서 제공하고 있는 것 같음!

 

examples Root directory for all examples.

  • contrib Examples from the community.
  • cpp C++ examples.
  • dotnet .Net examples.
  • java Java examples.
  • python Python examples.
  • notebook Jupyter/IPython notebooks.
  • flatzinc FlatZinc examples.
  • data Data files for examples.
  • tests Unit tests and bug reports.

 

support 되는 것은 ubuntu 18.04라고 하는데, ubuntu 16.04에 해보니 되긴 된다;;

from ortools.linear_solver import pywraplp
solver = pywraplp.Solver.CreateSolver('GLOP')
x = solver.NumVar(0, 1, 'x')
y = solver.NumVar(0, 2, 'y')

print('Number of variables =', solver.NumVariables())

# Create a linear constraint, 0 <= x + y <= 2.
ct = solver.Constraint(0, 2, 'ct')
ct.SetCoefficient(x, 1)
ct.SetCoefficient(y, 1)

print('Number of constraints =', solver.NumConstraints())
# Number of constraints = 1

# Create the objective function, 3 * x + y.
objective = solver.Objective()
objective.SetCoefficient(x, 3)
objective.SetCoefficient(y, 1)
objective.SetMaximization()

solver.Solve()
print('Solution:')
print('Objective value =', objective.Value())
print('x =', x.solution_value())
print('y =', y.solution_value())

#Solution:
#Objective value = 4.0
#x = 1.0
#y = 1.0

 

from ortools.sat.python import cp_model

model = cp_model.CpModel()
num_vals = 10
x = model.NewIntVar(3, num_vals - 1, 'x')
y = model.NewIntVar(0, num_vals - 1, 'y')
z = model.NewIntVar(0, num_vals - 1, 'z')
model.Add(x != y)
model.Add(x < y)
model.Add(x+z > y)
model.Add(x+y+z > 20)
solver = cp_model.CpSolver()
status = solver.Solve(model)

x.GetVarValueMap()
# (defaultdict(int, {x(3..9): 1}), 0)

if status == cp_model.OPTIMAL:
    print('x = %i' % solver.Value(x))
    print('y = %i' % solver.Value(y))
    print('z = %i' % solver.Value(z))
    
    
x = 4
y = 8
z = 9

solver.NumConflicts()
# 2

class VarArraySolutionPrinter(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):
        self.__solution_count += 1
        for v in self.__variables:
            print('%s=%i' % (v, self.Value(v)), end=' ')
        print()

    def solution_count(self):
        return self.__solution_count
        
solution_printer = VarArraySolutionPrinter([x, y, z])    
solver.SearchForAllSolutions(model, solution_printer)

 

오호...

model = cp_model.CpModel()
x_range = cp_model.Domain.FromIntervals([[0],[1],[2],[3],[4],[5]])
x = model.NewIntVarFromDomain(x_range, 'x')
y_range = cp_model.Domain.FromIntervals([[5],[6],[7],[8],[9],[10]])
y = model.NewIntVarFromDomain(y_range, 'y')
model.Add(x != y)
model.Add( 5*x > y)
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
    print('x = %i' % solver.Value(x))
    print('y = %i' % solver.Value(y))

 

간단히 사용해봤을 때는 굉장히 편리함을 느꼈다...

잘 사용하면 좋을 것 같기도 하고...

 

 

 

github.com/google/or-tools#quick-start

 

google/or-tools

Google's Operations Research tools:. Contribute to google/or-tools development by creating an account on GitHub.

github.com

developers.google.com/optimization/examples

 

OR-Tools Examples  |  Google Developers

 

developers.google.com

developers.google.com/optimization/reference/python/sat/python/cp_model

 

Python Reference: CP-SAT  |  OR-Tools  |  Google Developers

Module cp_model Methods for building and solving CP-SAT models. The following two sections describe the main methods for building and solving CP-SAT models. CpModel: Methods for creating models, including variables and constraints. CPSolver: Methods for so

developers.google.com

 

728x90