a ortools problem,find min value of the Knapsack problem,but value is caculated by tensorflow model not constant coefficient

huangapple go评论64阅读模式
英文:

a ortools problem,find min value of the Knapsack problem,but value is caculated by tensorflow model not constant coefficient

问题

The code segment you provided is attempting to set up a linear programming problem to minimize cost, but you mentioned that the cost depends on combinations, making the coefficients dynamic. To handle dynamic coefficients in a linear programming problem, you may need to use a different approach, such as using integer programming or constraint programming.

It's essential to formulate your problem correctly to reflect the dynamic nature of the cost. You might need to introduce additional decision variables or constraints to represent this relationship. Without a specific understanding of the problem you're solving, it's challenging to provide a detailed solution.

I recommend referring to the provided documentation links from Google Optimization (Stigler's Diet and Multiple Knapsack) for insights and examples. These resources often include detailed examples of how to formulate and solve various optimization problems using linear programming and related techniques.

If you have specific questions or issues related to your code or problem formulation, feel free to ask, and I'll do my best to assist you further.

英文:
public List<Waste> main(List<Waste> nowStock) throws Exception {
    Loader.loadNativeLibraries();

    HashMap<@Nullable Integer, @Nullable Waste> iToWaste = Maps.newHashMap();

    for (int i = 0; i < nowStock.size(); i++) {
        iToWaste.put(i, nowStock.get(i));
    }

    final int numItems = iToWaste.size();

    final int[] allItemsId = IntStream.range(0, numItems).toArray();

    final double binCapacities = 70;
    //StringBuffer sb = new StringBuffer();
    // Create the linear solver with the SCIP backend.
    MPSolver solver = MPSolver.createSolver("SCIP");
    if (solver == null) {

        throw new Exception("Could not create solver SCIP");

    }

    // Variables.
    MPVariable[] x = new MPVariable[numItems];

    for (int i : allItemsId) {
        x[i] = solver.makeBoolVar("x_" + i);
    }

    // Constraints.
    // Each item is assigned to at most one bin.
    for (int i : allItemsId) {
        MPConstraint constraint = solver.makeConstraint(0, 1, "");
        constraint.setCoefficient(x[i], 1);
    }

    // The amount packed in each bin cannot exceed its capacity.

    MPConstraint constraint = solver.makeConstraint(binCapacities*0.95, binCapacities*1.05, "");

    for (int i : allItemsId) {
        constraint.setCoefficient(x[i], iToWaste.get(i).getWeight().doubleValue());
    }


    // Objective.
    // Minimize cost value of packed items.
    MPObjective costValue = solver.objective();

    List<Waste> wastes = Arrays.stream(x).map(id -> iToWaste.get(id.index())).toList();

    costValue.setOffset(getCost(wastes).doubleValue());

    costValue.setMinimization();


    // Objective.
    // Maximize weight of packed items.
    MPObjective weight = solver.objective();
    for (int i : allItemsId) {
        weight.setCoefficient(x[i], iToWaste.get(i).getWeight().doubleValue());
    }
    weight.setMaximization();
    
    solver.setNumThreads(20);

    final MPSolver.ResultStatus status = solver.solve();


    // Check that the problem has an optimal solution.
    if (status == MPSolver.ResultStatus.OPTIMAL) {
        return Arrays.stream(allItemsId).filter(id->x[id].solutionValue() == 1).mapToObj(id -> iToWaste.get(id)).toList();
    }
    return null;
}

this part code

    MPObjective costValue = solver.objective();

    List<Waste> wastes = Arrays.stream(x).map(id -> iToWaste.get(id.index())).toList();

    costValue.setOffset(getCost(wastes).doubleValue());

    costValue.setMinimization();

i konw it is wrong.i want to find the minimize cost value,its cost is not depend on anyone of the combination,the cost depend on the combination.So the coefficient is dynamic,
how can i solve this problem.Thanks.

https://developers.google.com/optimization/lp/stigler_diet
https://developers.google.com/optimization/pack/multiple_knapsack

these are the docs I refer to

答案1

得分: 1

这不是一个线性问题,因此不使用线性求解器包装器。

英文:

this is not a linear problem. So not using the linear solver wrapper.

huangapple
  • 本文由 发表于 2023年5月13日 17:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定