OR-Tools (Google Optimization Tools)
2020. 10. 24. 17:03ㆍ분석 Python/Packages
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
developers.google.com/optimization/examples
developers.google.com/optimization/reference/python/sat/python/cp_model
728x90
'분석 Python > Packages' 카테고리의 다른 글
google에서 만든 NAS 패키지 Model Search (0) | 2021.02.24 |
---|---|
[PyGAD] Python 에서 Genetic Algorithm 을 사용해보기 (0) | 2021.01.30 |
[Python] package 설치 및 인터넷 없이 설치하는 방법 (0) | 2020.09.16 |
[Python] XAI 패키지 조사 해보기 (eXplainable AI) (0) | 2020.09.14 |
[Python] Imbalanced를 고려한 ML 오픈 소스 (0) | 2020.09.02 |