```#导入谷歌或工具，并声明模型模板。
from ortools.sat.python import cp_model
model = cp_model.CpModel()
#声明空列表，用于存储工人-班-日组合的指数。
shiftoptions = {}

＃以及每个工人的最大班次差额
workers = 5
shifts = 3
days = 7
maxshiftsperday = 1
maxdifference = 1

# 为每个工人、班次和日期的组合创建一个元组作为班次选项列表索引。
# 使用谷歌或工具创建一个布尔变量，表明如果给定的工人在当天工作，在该班次
for x in range(workers):
for y in range(days):
for z in range(shifts):
shiftoptions[(x,y,z)] = model.NewBoolVar("shift with id" + str(x) + " " + str(y) + " " + str(z))

# 现在，我们增加了一个约束条件，即只能分配给一个工人轮班。
for y in range(days):
for z in range(shifts):
model.Add(sum(shiftoptions[(x, y, z)] for x in range(workers)) == 1)
# 现在，我们增加了一个工人每天只上一个班的约束条件。
for x in range(workers):
for y in range(days):
model.Add(sum(shiftoptions[(x,y,z)] for z in range(shifts)) <= 1)
# 现在，我们增加了一个约束条件，即所有工人都有相同的班次，允许有一些偏差，允许有最大的差别
minshiftsperworker = (shifts * days) // workers
print(minshiftsperworker)
maxshiftsperworker = minshiftsperworker + maxdifference
for x in range(workers):
shiftsassigned = 0
for y in range(days):
for z in range(shifts):
shiftsassigned += shiftoptions[(x,y,z)]

class SolutionPrinterClass(cp_model.CpSolverSolutionCallback):
def __init__(self, shiftoptions, workers, days, shifts, sols):
val = cp_model.CpSolverSolutionCallback.__init__(self)
self._shiftoptions = shiftoptions
self._workers = workers
self._days = days
self._shifts = shifts
self._solutions = set(sols)
self._solution_count = 0
def on_solution_callback(self):
if self._solution_count in self._solutions:
print("solution " + str(self._solution_count))
for y in range(self._days):
print("day " + str(y))
for x in range(self._workers):
is_working = False
for z in range(self._shifts):
if self.Value(self._shiftoptions[(x,y,z)]):
is_working = True
print("worker " +str(x) +" works day " + str(y) +" shift " + str(z))
if not is_working:
print('  Worker {} does not work'.format(x))
print()
self._solution_count += 1
def solution_count(self):
return self._solution_count

# 解决模型
solver = cp_model.CpSolver()
solver.parameters.linearization_level = 0
# 解决它并检查解决方案是否可行。
solutionrange = range(1) #我们要显示1个可行的
results (the first one in the feasible set)
solution_printer = SolutionPrinterClass(shiftoptions, workers,
days, shifts, solutionrange)
solver.SearchForAllSolutions(model, solution_printer)
```
```4
solution 0
day 0
Worker 0 does not work
worker 1 works day 0 shift 0
Worker 2 does not work
worker 3 works day 0 shift 2
worker 4 works day 0 shift 1
day 1
Worker 0 does not work
Worker 1 does not work
worker 2 works day 1 shift 0
worker 3 works day 1 shift 2
worker 4 works day 1 shift 1
day 2
Worker 0 does not work
Worker 1 does not work
worker 2 works day 2 shift 0
worker 3 works day 2 shift 1
worker 4 works day 2 shift 2
day 3
worker 0 works day 3 shift 2
Worker 1 does not work
Worker 2 does not work
worker 3 works day 3 shift 1
worker 4 works day 3 shift 0
day 4
worker 0 works day 4 shift 0
worker 1 works day 4 shift 2
worker 2 works day 4 shift 1
Worker 3 does not work
Worker 4 does not work
day 5
worker 0 works day 5 shift 0
worker 1 works day 5 shift 1
worker 2 works day 5 shift 2
Worker 3 does not work
Worker 4 does not work
day 6
worker 0 works day 6 shift 1
worker 1 works day 6 shift 2
worker 2 works day 6 shift 0
Worker 3 does not work
Worker 4 does not work```

``````在R中使用lpSolve进行简单的线性优化