There seems to be an error in the linear constraint coefficient arrays.
You have n*m variables, so the arrays of coefficients for constraints and the objective functions must be n*m long. Unfortunately, SimplexSolver quietly extends the array of constraints if they are shorter than the array of the objective function. Thus, your code did not indicate the correct restrictions leading to an unlimited solution.
Limit 1: the sum of the values ββin all columns must be <= 1
for(int j=0; j<m; j++) { double[] v = new double[n*m]; for(int i=0; i<n; i++) v[i*n + j] = 1; constraints.add(new LinearConstraint(v, Relationship.LEQ, 1)); }
Constraint 2: the sum of a_i, j in all lines must be <= L (Limit)
// n = count of rows for(int i=0; i<n; i++) { double[] v = new double[n*m]; for(int j=0; j<m; j++) v[i*n + j] = A[i][j]; constraints.add(new LinearConstraint(v, Relationship.LEQ, L)); }
Objective Coefficients:
double[] objectiveCoefficients = new double[n * m]; Arrays.fill(objectiveCoefficients, 1.0); LinearObjectiveFunction objective = LinearObjectiveFunction(objectiveCoefficients, 0);
The restriction x_ij <= 1 has already been implemented due to restriction 2. Perhaps this makes it more clear and explicitly specify the restriction for 0 <= x_ij using NonNegativeConstraint :
SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize(objective, constraintSet, GoalType.MAXIMIZE, new NonNegativeConstraint(true));
wero
source share