Python 或工具-通过CP-SAT最小化进行分类
我使用谷歌或工具(CP-SAT解算器)来解决约束问题 我需要对人员进行分类,因此,我创建了以下模型:Python 或工具-通过CP-SAT最小化进行分类,python,constraint-programming,or-tools,sat,Python,Constraint Programming,Or Tools,Sat,我使用谷歌或工具(CP-SAT解算器)来解决约束问题 我需要对人员进行分类,因此,我创建了以下模型: model = cp_model.CpModel() matches = {} for p in people: for c in categories: # matches[(p, c)] == True if p must be in c matches[(p, c)] = model.NewBoolVar("%s-->%02d
model = cp_model.CpModel()
matches = {}
for p in people:
for c in categories:
# matches[(p, c)] == True if p must be in c
matches[(p, c)] = model.NewBoolVar("%s-->%02d" % (p,c))
其中一个制约因素是,年龄相近的人必须在同一个群体中。因此,对于所有类别,我将一个人的年龄与类别中的平均年龄的绝对差异最小化。为此,我编写了以下最小化约束:
abs_diffs = []
for c in categories:
ages_in_category = [int(ages[p])*matches[(p, c)] for p in people]
for a in ages_in_category:
n = len(ages_in_category)
diff = model.NewIntVar(-50*n, 50*n, name="")
model.Add(diff == n*a - sum(ages_in_category))
abs_diff = model.NewIntVar(0, 100*n, name="")
model.AddAbsEquality(abs_diff, diff)
abs_diffs.append(abs_diff)
model.Minimize(sum(abs_diffs))
我唯一的另一个限制是,一个人必须被分配一个类别,它已经过测试并工作
然而,在sat解算器找到解决方案后,类别中的人完全是随机设置的,而不是基于他们的年龄 我为什么做错了? 为什么解算器不考虑此最小化约束?
编辑:我注意到要最小化的总和在[0,1 000]范围内变化。这不是解决者的问题吗