OR-Tools Day2

개발공부를해보자·2025년 6월 12일

실습 코드

from ortools.sat.python import cp_model
# ------------------ Day2(25.06.12) ------------------

# 모델 생성
model = cp_model.CpModel()

# 변수 선언: x, y 는 0~2 사이 정수
x = model.NewIntVar(0, 2, 'x')
y = model.NewIntVar(0, 2, 'y')

# 제약 조건
model.Add(x + y <= 2)

# 솔버 생성
solver = cp_model.CpSolver()
status = solver.Solve(model)

# 결과 출력: x = 0, y = 0
if status == cp_model.FEASIBLE or status == cp_model.OPTIMAL:
    print(f'x = {solver.Value(x)}, y = {solver.Value(y)}')
else:
    print('해를 찾을 수 없습니다.')
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글