我很难理解model.scalar方法在 choco 库中的作用
假设我们有一个简单的有界背包问题...给定一个物品清单,其中每个物品都有重量和利润,
选择要装载到卡车上的物品,其中卡车的总重量有限制,并且
至少达到最低利润
或获得最大可能利润的地方
int[] weights = {15, 12, 17, 20, 39, 24, 13}; //the weights of a group of item types int[] profits = {4, 2, 6, 4, 9, 8, 5}; //the profit obtained from each type - must be same length as weights int[] stock = {6, 8, 5, 5, 2, 4, 7}; //the maximum number of each item int capacity = 300; //maximum load weight in the truck int cost = 100; //the profit threshold to be achieved (e.g. the cost of transport) int numTypes = weights.length; //number of object types //calculate the max profit, for setting up the variables //compute as though every piece of stock could be loaded and sold int maxprofit = 0; for (int type = 0; type<numTypes; type++) { maxprofit += stock[type]*profits[type]; } //Create variables IntVar[] load = new IntVar[numTypes]; for (int type = 0; type<numTypes; type++) { load[type] = model.intVar("load"+type, 0, stock[type]); //how many of each type } IntVar totalWgt = model.intVar("total wgt", 0, capacity); //the total weight of selected items IntVar totalProfit = model.intVar("profit", 0, maxprofit); //the total profit of selected items System.out.println("Maxprofit = " + maxprofit); //CONSTRAINTS // what is this doing? model.scalar(load, weights, "=", totalWgt).post(); //constrain the total weight to be less than the capacity model.arithm(totalWgt, "<=", capacity).post(); model.scalar(load, profits, "=", totalProfit).post(); //constraint the total profit to be above the cost model.arithm(totalProfit, ">", cost).post(); // Solve the problem Solver solver = model.getSolver(); // state which variable is to be maximised (or minimised) model.setObjective(Model.MAXIMIZE, totalProfit); while (solver.solve()) { //print the solution System.out.println("Solution " + solver.getSolutionCount()); }
谁能解释一下标量函数在这里实际上在做什么?