In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces how to configure Cplex's Java environment and API instructions, the content is very detailed, interested friends can refer to, hope to be helpful to you.
00 preface
Because editors generally use C++ and Java, and now it is much more convenient to develop large-scale algorithms using this kind of object-oriented programming language. Based on the above considerations, and the limited time and energy, I only do detailed tutorials on C++ and Java for the time being. Maybe it will be made up later on about matlab and python.
Then, as usual, configure the environment before you begin. Then configure the environment of java first.
01 add environment variable
I have already talked about how to download and install cplex, as shown in the figure:
Make sure this version is installed before we can move on to the next step.
02 Import the CPLEX library into ECIPLSE
The ide commonly used by java editors is eclipse, so just configure it about eclipse. Please set up other development environments by yourselves.
Create a new project, add a package, and add a class with the main function. Don't write the code yet.
Right-click on the project and select build path-> Configure Build Path.
Find Libraries- > Add External JARs. Then navigate to the folder\ lib and add cplex.jar to it.
It's not enough at this point, and you need to add CPLEX's dynamic runtime library so that it can be found when the java program is running.
The specific approach is: right-click the project, select build path-> Configure Build Path. , find Libraries, click JRE System Library, click edit at Native library location, and add the\ bin\ x64_win64 folder under cplex.
There are dynamic libraries needed for the program to run.
03 solve a simple model
A simple linear programming problem:
Copy the following code into the main function:
Try {
IloCplex cplex = new IloCplex (); / / creat a model double [] lb = {0.0,0.0,0.0}; double [] ub = {40.0, Double.MAX_VALUE, Double.MAX_VALUE}; IloNumVar [] x = cplex.numVarArray (3, lb, ub); double [] objvals = {1.0,2.0,3.0}; cplex.addMaximize (cplex.scalProd (x, objvals)) Double [] coeff1 = {- 1.0,1.0,1.0}; double [] coeff2 = {1.0,3.0,1.0}
Cplex.addLe (cplex.scalProd (x, coeff1), 20.0); cplex.addLe (cplex.scalProd (x, coeff2), 30.0)
If (cplex.solve ()) {cplex.output () .println ("Solution status =" + cplex.getStatus ()); cplex.output () .println ("Solution value =" + cplex.getObjValue ()); double [] val = cplex.getValues (x); for (int j = 0; j)
< val.length; j++) cplex.output().println("x" + (j+1) + " = " + val[j]); } cplex.end(); } catch (IloException e) { System.err.println("Concert exception caught: " + e); } 点击运行,出现下面的结果就成功啦:Finally, if you can't find build path, share libraries or anything, make sure the first step is configured correctly! At this point, we can happily use cplex.
04 API description [1]
The overall framework of cplex's java program is generally as follows:
Try {
} catch (IloException e) {System.err.println ("Concert exception caught:" + e);}
1. Create a new cplex class using the IloCplex class.
two。 Use IloNumVar to define solving variables.
3. Use addMaximize or addMinimize to define the solution goal.
4. Use addLe to add constraints.
5. Solve the problem using the solve () method.
6. Use IloNumExpr to define intermediate variables.
Cplex's java api does not support addition, subtraction, multiplication and division symbols, addition must use sum method, subtraction must use diff method, multiplication and division must use prod method.
This is the end of the Java environment and API instructions on how to configure Cplex. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 208
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.