如何使用Choco solver框架为Java建模特殊集约束

时间:2016-12-30 21:05:48

标签: java constraint-programming choco

我在使用Java中的Choco-solver解决问题和解决问题时遇到了问题,我开始时并不熟悉约束编程,但我们的任务是为以下会议制作座位应用程序:

  • 每张桌子必须至少有6个人,并且总有足够的桌子。
  • 人们应与邻居坐在一起,以最大化共同利益。
  • 在前几天,我们希望尽量减少与早先坐着的人坐在桌子上的人。
  • 人们属于A级或B级,我们希望最大限度地减少每个表中A类的出现。

到目前为止,我们有一个SetVar guest_list,其中包括1个访客数量。 然后将guest_list分区为SetVar []表。

但是如何制定约束,说每个table.size> = 6?

如何在两组之间设置约束,以最小化成员/交叉点?

而且我不知道如何为集合中的邻居建模以最大化共享兴趣。

import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.solver.variables.SetVar;

// constant value declarations
int number_of_guests = 100;
int table_size = 8;
int tables_needed = number_of_guests / table_size + 1;

// this will later be passed as argument, generated for testing
int[] guest_ids = new int[number_of_guests];
for (int i = 0; i < number_of_guests; i++) {
    guest_ids[i] = i;
}

// Model and variables
Model model = new Model("Seating solver");

IntVar min_guests_at_tables = model.intVar(6);
// a list containing all guests in its LB
SetVar guest_list  = model.setVar("guests", guest_ids);
// making empty sets for each table needed, UB is all guests
SetVar[] tables = model.setVarArray(tables_needed, new int[]{}, guest_ids);
// log over table seatings from earlier days, starts empty for day 1.
// want to minimize how many members from tables[x] share with any ser of logged_tables
SetVar[] logged_tables = model.setVarArray(0, new int[]{}, new int[]{});
// a setvar of all interests for each guest, will be imported later.
SetVar[] guest_interests = model.setVarArray(number_of_guests, new int[]{}, new int[]{});
// set of all those members of group A
// random test values inserted
SetVar group_A_members = model.setVar(1,23,54,2);

// Constraints unsure about all of these

// each set must be unique
model.allDifferent(tables);
for (SetVar table : tables) {
    // TODO trying to constrain sizes of each table set to 6 or greater
    model.arithm(model.intVar(table.getLB().size()), ">=", min_guests_at_tables);
// TODO trying to minimize number of shared members in tables, with whats in logged_tables
for (SetVar old_table : logged_tables) {
    // no idea
}
// TODO trying to maximize the amount of interest neigbors in a set share
// no idea

// TODO minimize shared members from table with group_A_members
// no idea how to minimize or maximize
}

// Solving

Solution s = model.getSolver().findSolution();
if (s != null) System.out.println(s.toString());

1 个答案:

答案 0 :(得分:0)

可从模型访问可用约束集,该模型实现IConstraintFactory。查看源代码中的ISetConstraintFactory.java。有与设置大小,联合,分区相关的约束......