制定一个约束来排序数组(Choco + java)

时间:2016-10-27 23:51:38

标签: constraints choco

我正在尝试为电话支持中心规划的问题开发一个巧克力解决方案。在从8:00到20:00的12小时内。

变量和约束:

  • 员工人数= 9
  • 每位员工的最低购买时间(h和H)
  • 所有员工的工作时间:42小时< =总小时数< = 42 + C(我的情况下C等于2)
  • 每小时工作的员工数量表(表的大小= 12)

我不能做的对比:

我知道每位员工的工作时间数我无法将其列入跟踪时间:/

结果将是:

Final Result

但直到现在我才得到my result untill now

我认为这有点问题......如果你能救我的命,请告诉我代码中缺少的约束。

我的代码

  package projetppc;

import java.util.Arrays;
import javax.swing.SortingFocusTraversalPolicy;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.variables.IntVar;

public class ProjetPPC {

    public void modelAndSolve() {

        int k = 9;
        int htpj = 12;
        int h = 4;
        int H = 6;
        int C = 2;
        int HT = 42;

        Model model = new Model(k + "- CAT");

        int[] numOfemp = {1, 2, 4, 5, 5, 4, 5, 5, 3, 4, 2, 2};

        IntVar[][] matrix = new IntVar[k][htpj];

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < htpj; j++) {
                matrix[i][j] = model.intVar("(" + i + "," + j + ")", 0, 1);

            }

        }


        model.arithm(matrix[0][0], "=", 1).post();

        int[] coeffs1 = new int[htpj];
        Arrays.fill(coeffs1, 1);

        // constraint 1 et 2
        for (int i = 0; i < k; i++) {
            model.scalar(matrix[i], coeffs1, "<=", H).post();
            model.scalar(matrix[i], coeffs1, ">=", h).post();
        }
        int[] coeffs2 = new int[k];
        Arrays.fill(coeffs2, 1);


        IntVar[][] inversematrix = new IntVar[htpj][k];
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < htpj; j++) {
                inversematrix[j][i] = matrix[i][j];
            }

        }
        // constraint
        for (int i = 0; i < htpj; i++) {
            model.scalar(inversematrix[i], coeffs2, "=", numOfemp[i]).post();

        }

        // constraint
        IntVar[] alltable = new IntVar[k * htpj];

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < htpj; j++) {
                alltable[(htpj * i) + j] = matrix[i][j];
            }
        }

        int[] coeffs3 = new int[k * htpj];
        Arrays.fill(coeffs3, 1);
        model.scalar(alltable, coeffs3, ">=", HT).post();
        model.scalar(alltable, coeffs3, "<=", HT + C).post();




        // solution
        Solution solution = model.getSolver().findSolution();
        if (solution != null) {
            for (int i = 0; i < k; i++) {

                System.out.println("employé " + i + " " + Arrays.toString(matrix[i]));

            }


        } else {

            System.out.println("Pas de solution.");
        }
    }

     public static void main(String[] args) {
        new ProjetPPC().modelAndSolve();
    }
}

0 个答案:

没有答案