gpt4 book ai didi

java - 对于具有多个或无限数量解决方案的线性规划问题,如何使用 JOptimizer 获得仅一个可行的解决方案?

转载 作者:行者123 更新时间:2023-11-30 06:58:00 26 4
gpt4 key购买 nike

JOptimizer 是一个开源 Java 库,可帮助开发大多数决策支持系统。请参阅:jooptimizer.com/我正在使用 JOptimizer 来获得线性规划问题的最佳解决方案。请参阅:jooptimizer.com/linearProgramming.html

使用它我可以成功获得大多数线性规划问题的答案。例如:最小化 3x+4y,使得 2x+3y >= 8, 5x+2y >= 12, x >= 0, y >= 0 可以使用 JOptimizer 求解,如下所示。

import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;
import org.apache.log4j.BasicConfigurator;

/**
* @author K.P.L.Kanchana
*/

public class Main {

public static void main(String[] args) throws Exception {

// Objective function (plane)
LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {3.0, 4.0}, 0); //minimize 3x+4y

//inequalities (polyhedral feasible set G.X<H )
ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
// x >= 0
inequalities[0] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.00}, 0.0); // focus: -x+0 <= 0
// y >= 0
inequalities[1] = new LinearMultivariateRealFunction(new double[]{0.0, -1.00}, 0.0); // focus: -y+0 <= 0
// 2x+3y >= 8
inequalities[2] = new LinearMultivariateRealFunction(new double[]{-2.0, -3.00}, 8.0); // focus: -2x-3y+8 <= 0
// 5x+2y >= 12
inequalities[3] = new LinearMultivariateRealFunction(new double[]{-5.0, -2.00}, 12.0);// focus: -5x-2y+12 <= 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);
int returnCode = opt.optimize();

double[] sol = opt.getOptimizationResponse().getSolution();

System.out.println("Length: " + sol.length);
for (int i=0; i<sol.length/2; i++){
System.out.println( "X" + (i+1) + ": " + Math.round(sol[i]) + "\ty" + (i+1) + ": " + Math.round(sol[i+1]) );
}
}

}

但是有些线性规划问题具有多个或无限数量的可行解。例如,最大化 4x+3Y,但须满足 8x+6y <= 25、3x+4y <= 15、x >= 0、y >= 0。当我尝试如下解决起诉 JOptimizer 时,它给出了错误。

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]));
}
}

}

我想通过使用 JOptimizer 从无限数量的解决方案中获取一个可行的解决方案来修复此错误。但我不知道怎么办? JOptimizer 库中有特殊命令吗?有人可以说一下吗?我的谷歌驱动器上提供了所有必需的库和依赖项:https://drive.google.com/file/d/0B84k1fZRHSMdak00TjZKNXBKSFU/view?usp=sharingJava 文档可在此处获取:http://joptimizer.com/apidocs/index.html抱歉,如果这是一个奇怪的问题,并感谢所有花时间考虑这个问题的人。

最佳答案

我发现我的代码存在问题。说实话,我从阿尔贝托·特里维拉托那里得到了一些帮助。据我所知,他是 JOptimizer 的开发者。我真的非常感谢他浪费时间来寻找问题。正如他提到的,问题不在于多个解决方案,而在于我向求解器要求的高精度。最好的做法是不要要求超出您真正需要的精度。另请记住,不等式始终采用 G.x < h 的形式,即严格小于(不少于 htan 或 EQUAL),因为 JOptimizer 实现了内点法求解器。

更正的代码:

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); // Here was the issue
or.setTolerance(JOptimizer.DEFAULT_TOLERANCE / 10); // Here 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]));
}
}

}

关于java - 对于具有多个或无限数量解决方案的线性规划问题,如何使用 JOptimizer 获得仅一个可行的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41451074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com