Writing the Java code
Lists the public methods of the Java knapsack algorithm you can use.
The Java algorithm can be found in
<Install_dir>\opl\examples\opl_interfaces\java\javaknapsack\src\javaknapsack\Knapsack.java
It is not the purpose of this tutorial to describe the knapsack algorithm.
Be aware that this implementation of the algorithm might not be optimal as the purpose in this tutorial is to keep it small and simple to read and understand.
The interesting point is that some methods are public and therefore candidates for being called from OPL.
These methods, written as simple OPL Java code, are:
public Knapsack(): This constructor creates an empty knapsack instance that can be reused with different data.
public void updateInputs (IloOplElement oplWeights, IloOplElement oplValues): This method allows you to update data from OPL elements and pass it to the algorithm, using arrays of weights and values.
public double solve (IloOplElement oplSolution, int size): This method runs the algorithm and puts back the solution into the given solution array.
You need to compile the Java source code using Run.bat.
For more information on how to use OPL APIs from Java, please refer to the Interfaces User’s Manual and the Java API OPL Reference Manual.