首页 > 解决方案 > 如何计算使总和最小化的点?

问题描述

我试图尽量减少成分的总和。

例如,A 产品和 B 产品中维生素 A 的总量必须超过 C。此外,应尽量减少过量。

我根据数据做了 18 个函数。(维生素,碳水化合物,蛋白质〜等)

我使用 apache simplexor 来获得每个函数的最小值。(我将所有函数添加到约束中,并通过将每个函数添加到目标函数来计算。)

我得到了以下结果,但是,我想要一个最小化总差异的点。(差异 = min - C)

我英语不好,希望你能理解我的问题。感谢您阅读我的问题。

这是我的代码。

public class Simplex3D {
public static void cal(double[] a, double[] b, double[] c, double[] d) {
    //a, b, and c are the coefficient of functions 
    //d is the amount of ingredient that should be exceeded.
    System.out.println();
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 0, 1,0 }, Relationship.GEQ, 0));
    constraints.add(new LinearConstraint(new double[] { 1, 0,0 }, Relationship.GEQ, 0));
    constraints.add(new LinearConstraint(new double[] { 0, 0,1 }, Relationship.GEQ, 0));
    //x, y, z >=0
    constraints.add(new LinearConstraint(new double[] { a[5]*100, b[5]*100, c[5]*100 }, Relationship.GEQ, d[5]));
    constraints.add(new LinearConstraint(new double[] { a[16]*100, b[16]*100, c[16]*100 }, Relationship.GEQ, d[16]));
    constraints.add(new LinearConstraint(new double[] { a[4]*100, b[4]*100, c[4]*100 }, Relationship.GEQ, d[4]));
    for(int i=0;i<18;i++) {

    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { a[i]*100, b[i]*100,c[i]*100 }, 0);
    // create and run the solver
    SimplexSolver solver = new SimplexSolver();
    //solver.optimize(f, constraints, GoalType.MINIMIZE, new NonNegativeConstraint(true));
    PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true));

    // get the solution
    double x = solution.getPoint()[0];
    double y = solution.getPoint()[1];
    double z = solution.getPoint()[2];
    double min = solution.getValue();
    double diff= min-d[i];

    System.out.println("x: "+x+" y: "+y+" z: "+z+" min: "+min+" diff: "+diff);
}
}

}

标签: javaoptimizationlinear-programmingleast-squaressimplex

解决方案


我根据数据做了 18 个函数。

因此,您创建了 18 个独立问题并解决了它们,获得了 18 个解决方案。这不是要走的路。

请注意,您的问题只有三个变量。这可能描述了单个产品如何混合,但您似乎有十八种产品。你必须在问题中描述所有这些。假设 3 种成分和 18 种产品,这意味着单个问题中有 3 * 18 个变量。

只有这样,您才能表达关于差异总和的目标。


这可能对您有帮助,也可能对您没有帮助。无论如何,你没有编程问题。相反,您正在努力解决问题。您的 LP 问题听起来很经典,找到类似的问题并找出您的 LP 有何不同。


推荐阅读