我试图尽量减少成分的总和。
例如,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);
}
}
}