Java中使用CPLEX输入三维索引参数及定义初始化作业车间调度变量
Hey there! Let's tackle your two CPLEX + Java questions one by one—they're critical for getting your job shop scheduling model up and running smoothly.
1. Using 3D Indexed Parameters in Java CPLEX
For 3D parameters (like processing times, machine eligibility rules, etc.) in your model, the most straightforward approach is to use standard Java 3D arrays to store your input data, then reference them directly when building CPLEX constraints or your objective function. Here's how to do it:
Step 1: Define and Populate the 3D Parameter Array
First, initialize a 3D array matching your problem dimensions (jobs n, operations per job m, machines M). You can populate this data from a file, database, or hardcode test values:
// Example dimensions (adjust these to your actual problem size) int n = 5; // Number of jobs int m = 3; // Number of operations per job int M = 4; // Number of machines // 3D array to store a parameter like processing time p[i][j][k] double[][][] processingTime = new double[n][m][M]; // Populate the array (replace with your actual input logic) for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < M; k++) { processingTime[i][j][k] = Math.random() * 10 + 5; // Random time between 5-15 } } }
Step 2: Reference the Parameter in Your Model
When building constraints or the objective function, you can directly access the array using the same indices you'll use for your decision variables. For example, calculating total processing time:
IloLinearExpr totalTime = cplex.linearExpr(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < M; k++) { totalTime.addTerm(processingTime[i][j][k], x[i][j][k]); } } }
Note: If you need more structured parameter handling (like enforcing valid index tuples), you could use
IloTupleSet, but for most job shop scheduling use cases, a standard 3D array is simpler and more efficient.
2. Defining and Initializing 3D Binary Decision Variables x[i][j][k]
Your binary variable x[i][j][k] (1 if job i's j-th operation is assigned to machine k, 0 otherwise) can be created as a 3D array of IloIntVar (since binary variables are just integer variables restricted to 0/1). Here's a step-by-step implementation:
Step 1: Initialize the CPLEX Model
First, create your CPLEX model instance:
IloCplex cplex = new IloCplex();
Step 2: Create the 3D Variable Array
Initialize a 3D array to hold your binary variables, then loop through each index to create individual variables. Use cplex.boolVar() (a shortcut for 0/1 integer variables) and give each variable a meaningful name for debugging:
// Match the dimensions to your problem IloIntVar[][][] x = new IloIntVar[n][m][M]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < M; k++) { // Name variables to align with your 1-based index convention String varName = "x_" + (i+1) + "_" + (j+1) + "_" + (k+1); // Create binary variable (0 or 1) x[i][j][k] = cplex.boolVar(varName); } } }
Key Notes:
cplex.boolVar()is equivalent tocplex.intVar(0, 1)—use whichever you find more readable.- Java uses 0-based arrays, but we added
+1to the variable names to match your 1-based index rule (i∈(1..n), etc.)—this makes it easier to map variables back to your problem description. - Once initialized, you can use these variables in constraints (e.g., ensuring each operation is assigned to exactly one machine) and your objective function.
内容的提问来源于stack exchange,提问作者h.jer

