我要解决的问题是:
/** Maximize 4x+3Y
* Subject to
* 8x+6y <= 25
* 3x+4y <= 15
* x,y >= 0
*/
理论上LP这个问题的优化有无限的解决方案。
我的谷歌硬盘提供的所有必需的库,依赖项: https://drive.google.com/file/d/0B84k1fZRHSMdak00TjZKNXBKSFU/view?usp=sharing
我的代码:
package testJOptimizer;
import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;
/**
*
* @author K.P.L.Kanchana
*/
public class test_4_alternateOptimum {
/**
* @param args the command line arguments
*/
public static void main(String[] args){
// BasicConfigurator.configure();
// Objective function (plane)
LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {-4.0, -3.0}, 0); // maximize 4x+3y
//inequalities (polyhedral feasible set G.X<H )
ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
// 8x+6y <= 25
inequalities[0] = new LinearMultivariateRealFunction(new double[]{8.0, 6.0}, -25); // 8x+6y-25<=0
// 3x+4y <= 15
inequalities[1] = new LinearMultivariateRealFunction(new double[]{1.0, 4.0}, -15); // 3x+4y-15<=0
// x >= 0
inequalities[2] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.0}, 0);
// y >= 0
inequalities[3] = new LinearMultivariateRealFunction(new double[]{0.0, -1.0}, 0);
//optimization problem
OptimizationRequest or = new OptimizationRequest();
or.setF0(objectiveFunction);
or.setFi(inequalities);
//or.setInitialPoint(new double[] {0.0, 0.0});//initial feasible point, not mandatory
or.setToleranceFeas(1.E-9);
or.setTolerance(1.E-9);
//optimization
JOptimizer opt = new JOptimizer();
opt.setOptimizationRequest(or);
try {
int returnCode = opt.optimize();
}
catch (Exception ex) {
ex.printStackTrace();
return;
}
// get the solution
double[] sol = opt.getOptimizationResponse().getSolution();
// display the solution
System.out.println("Length: " + sol.length);
for (int i = 0; i < sol.length; i++) {
System.out.println("answer " + (i+1) + ": " + (sol[i]));
}
}
}
答案 0 :(得分:0)
我发现我的代码存在问题。说实话我得到了alberto trivellato的一些帮助。据我所知,他是开发JOptimizer的人。我非常感谢他浪费时间去寻找问题。 正如他所提到的那样,问题不在于多种解决方案,而在于我向求解者提出的高精度问题。最好的做法是不要求比你真正需要的更精确。还要记住,不等式总是以G.x的形式出现。 h,即严格小于(不小于htan或EQUAL),因为JOptimizer实现了内点法解算器。
更正后的代码:
package testJOptimizer;
import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;
/**
*
* @author K.P.L.Kanchana
*/
public class test_4_alternateOptimum {
/**
* @param args the command line arguments
*/
public static void main(String[] args){
// BasicConfigurator.configure();
// Objective function (plane)
LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {-4.0, -3.0}, 0); // maximize 4x+3y
//inequalities (polyhedral feasible set G.X<H )
ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
// 8x+6y < 25(no equal sign)
inequalities[0] = new LinearMultivariateRealFunction(new double[]{8.0, 6.0}, -25); // 8x+6y-25<0
// 3x+4y < 15
inequalities[1] = new LinearMultivariateRealFunction(new double[]{1.0, 4.0}, -15); // 3x+4y-15<0
// x > 0
inequalities[2] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.0}, 0);
// y > 0
inequalities[3] = new LinearMultivariateRealFunction(new double[]{0.0, -1.0}, 0);
//optimization problem
OptimizationRequest or = new OptimizationRequest();
or.setF0(objectiveFunction);
or.setFi(inequalities);
//or.setInitialPoint(new double[] {0.0, 0.0});//initial feasible point, not mandatory
or.setToleranceFeas(JOptimizer.DEFAULT_FEASIBILITY_TOLERANCE / 10); // There was the issue
or.setTolerance(JOptimizer.DEFAULT_TOLERANCE / 10); // There was the issue
//optimization
JOptimizer opt = new JOptimizer();
opt.setOptimizationRequest(or);
try {
int returnCode = opt.optimize();
}
catch (Exception ex) {
ex.printStackTrace();
return;
}
// get the solution
double[] sol = opt.getOptimizationResponse().getSolution();
// display the solution
System.out.println("Length: " + sol.length);
for (int i = 0; i < sol.length; i++) {
System.out.println("answer " + (i+1) + ": " + (sol[i]));
}
}
}