Test Eclipse
Open eclipse and select your workspace. If you start out with a blue screen with some circular buttons saying Welcome, Tutorial, etc., click Go to Workbench. Otherwise, you are already at the workbench. Go to File → New → Java Project. Name your project sandbox
, then click the button Finish. You now have a new project!
In the Package Explorer (left side) right click the arrow next to sandbox
to expand the project. Right click on the src
folder and then go to new → class. Name the class TestClass
. Under "Which method stubs would you like to create?" check the box next to public static void main(String[] args)
. Hit the Finish button. Then replace
// TODO Auto-generated method stub
by
System.out.println("Hello World!");
Save the file TestClass.java
by hitting Ctrl+s or the save button (top left on the menu bar). To run your code, go to the top center of the menu bar and hit the run icon (its a green circle with a white arrow inside), or go to the menu bar and hit Run → Run. The Console should pop up on the bottom with the output of your program.
Test Cplex from Eclipse
Open Eclipse and expand the sandbox
project we created previously. Create a new class called KnapsackSolver
. Next we are going to connect to Cplex. Right click on sandbox
in the package explorer and select new->folder
. Give it the name lib
and click Finish. Find the file cplex.jar in your Cplex installation. On my Windows installation, it was located at
- Windows:
C:\Program Files\IBM\ILOG\CPLEX_Studio125\cplex\lib\cplex.jar
- Linux:
/opt/IBM/ILOG/CPLEX_Studio125/cplex/lib/cplex.jar
- Mac:
/Users/<yourusername>/Applications/IBM/ILOG/CPLEX_Studio125/cplex/lib/cplex.jar
Copy the file cplex.jar and paste it in the newly created lib
folder in our project. Right click on sandbox
in the package explorer, then select Build Path → Configure Build Path... Select the Libraries tab, then hit the Add JARs button (see here for an explanation of what all these buttons do). Select the file sandbox/lib/cplex.jar
, then hit OK until we have exited all the menus.
Open up KnapsackSolver
and replace the text of the file with the following:
import java.util.Arrays; import ilog.concert.IloException; import ilog.concert.IloIntVar; import ilog.cplex.IloCplex; public class KnapsackSolver { public static double[] solveKnapsack(double[] values, double[] weights, double capacity){ if(weights.length != values.length){ throw new RuntimeException("values.length was " + values.length + " and weights.length was" + weights.length + " but they must be the same"); } try { IloCplex cplex = new IloCplex(); IloIntVar[] x = cplex.boolVarArray(values.length); cplex.addLe(cplex.scalProd(x, weights), capacity); cplex.addMaximize(cplex.scalProd(x, values)); cplex.solve(); return cplex.getValues(x); } catch (IloException e) { throw new RuntimeException(); } } public static void main(String[] args){ double[] values = new double[]{2,4,5,6}; double[] weights= new double[]{2,3,4,7}; double capacity = 8; double[] solution = solveKnapsack(values,weights,capacity); System.out.println(Arrays.toString(solution)); } }
Run the code. The final line of output should of course be [0.0, 1.0, 1.0, 0.0]
, up to some precision. Upload the output of the program as your assignment here If you get the error
Exception in thread "main" java.lang.UnsatisfiedLinkError: ilog.cplex.Cplex.CPXopenCPLEX([I)J
then your PATH was not set properly. The easiest way to deal with this is to add a virtual machine argument to the Run Configuration. Starting from the menu bar at the top, select Run → Run Configurations... and then on the left, make sure Java Application → KnapsackSolver is selected. In the center panel, pick the ( x )=Arguments tab. In the second box, titled VM Arguments, add the line:
- Linux:
-Djava.library.path=/opt/ibm/ILOG/CPLEX_Studio125/cplex/bin/x86-64_sles10_4.1
- Windows:
-Djava.library.path=C:\Program Files\IBM\ILOG\CPLEX_Studio125\cplex\bin\x64_win64
- Mac:
-Djava.library.path=/Users/<yourusername>/Applications/IBM/ILOG/CPLEX_Studio125/cplex/bin/x86-64_darwin
As you can see, the VM argument is telling java the directory that the c code of cplex is located in.
Finally, hit Run on the bottom.