Example #1
0
 private int[] concat(int[] A, int[] B) {
   int aLen = A.length;
   int bLen = B.length;
   int[] C = new int[aLen + bLen];
   System.arraycopy(A, 0, C, 0, aLen);
   System.arraycopy(B, 0, C, aLen, bLen);
   return C;
 }
Example #2
0
 private double[] concat(double[] A, double[] B) {
   int aLen = A.length;
   int bLen = B.length;
   double[] C = new double[aLen + bLen];
   System.arraycopy(A, 0, C, 0, aLen);
   System.arraycopy(B, 0, C, aLen, bLen);
   return C;
 }
  private double[][] createSolution(double[][] solutionOne, double[][] solutionTwo) {
    double[][] solution =
        new double[solutionOne.length + solutionTwo.length][solutionOne[0].length];

    for (int i = 0; i < solutionOne.length; i++)
      System.arraycopy(solutionOne[i], 0, solution[i], 0, solutionOne[i].length);
    for (int i = solutionOne.length; i < solution.length; i++)
      System.arraycopy(
          solutionTwo[i - solutionOne.length],
          0,
          solution[i],
          0,
          solutionTwo[i - solutionOne.length].length);
    return solution;
  }
 private double[][] fitEfficiency(
     double[]
         eff) // Method to make a 2-dimenstional array out of the efficiency in order to use it as
              // input for stage 2
     {
   double[][] fitted =
       new double[1]
           [eff.length]; // 1st dimension: "amount" of different parameters, 2nd dimension: value
                         // for each DMU
   System.arraycopy(eff, 0, fitted[0], 0, eff.length);
   return fitted;
 }
  public static void main(String[] args) throws Exception {
    int status = 127;
    try {
      IloOplFactory.setDebugMode(true);
      IloOplFactory oplF = new IloOplFactory();

      // make master model
      IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
      IloOplSettings settings = oplF.createOplSettings(errHandler);

      IloOplRunConfiguration masterRC =
          oplF.createOplRunConfiguration(
              DATADIR + "/cutstock_change.mod", DATADIR + "/cutstock_change.dat");
      masterRC.setErrorHandler(errHandler);
      IloCplex masterCplex = oplF.createCplex();
      masterCplex.setOut(null);

      masterRC.setCplex(masterCplex);
      IloOplModel masterOpl = masterRC.getOplModel();
      masterOpl.generate();
      IloOplDataElements masterDataElements = masterOpl.makeDataElements();

      int nWdth = masterDataElements.getElement("Amount").asIntMap().getSize();
      ArrayList<IloNumVar> masterVars = new ArrayList<IloNumVar>();
      IloNumVarMap cuts = masterOpl.getElement("Cut").asNumVarMap();
      for (int i = 1; i <= nWdth; i++) {
        masterVars.add(cuts.get(i));
      }

      // prepare sub model source, definition and engine
      IloOplModelSource subSource = oplF.createOplModelSource(DATADIR + "/cutstock-sub.mod");
      IloOplModelDefinition subDef = oplF.createOplModelDefinition(subSource, settings);
      IloCplex subCplex = oplF.createCplex();
      subCplex.setOut(null);

      double best;
      double curr = Double.MAX_VALUE;
      do {
        best = curr;

        // Make master model
        System.out.println("Solve master.");
        if (masterCplex.solve()) {
          curr = masterCplex.getObjValue();
          System.out.println("OBJECTIVE: " + curr);
          status = 0;
        } else {
          System.out.println("No solution!");
          status = 1;
        }

        // prepare data for sub model
        IloOplDataElements subDataElements = oplF.createOplDataElements();
        subDataElements.addElement(masterDataElements.getElement("RollWidth"));
        subDataElements.addElement(masterDataElements.getElement("Size"));
        subDataElements.addElement(masterDataElements.getElement("Duals"));
        // get reduced costs and set them in sub problem
        IloNumMap duals = subDataElements.getElement("Duals").asNumMap();
        for (int i = 1; i < nWdth + 1; i++) {
          IloForAllRange forAll =
              (IloForAllRange) masterOpl.getElement("ctFill").asConstraintMap().get(i);
          duals.set(i, masterCplex.getDual(forAll));
        }
        // make sub model
        IloOplModel subOpl = oplF.createOplModel(subDef, subCplex);
        subOpl.addDataSource(subDataElements);
        subOpl.generate();

        System.out.println("Solve sub.");
        if (subCplex.solve()) {
          System.out.println("OBJECTIVE: " + subCplex.getObjValue());
          status = 0;
        } else {
          System.out.println("No solution!");
          status = 1;
        }
        if (subCplex.getObjValue() > -RC_EPS) break;

        // Add variable in master model
        IloNumVar newVar = masterCplex.numVar(0, Double.MAX_VALUE);
        IloObjective masterObj = masterOpl.getObjective();
        masterCplex.setLinearCoef(masterObj, newVar, 1);
        for (int i = 1; i < nWdth + 1; i++) {
          double coef = subCplex.getValue(subOpl.getElement("Use").asIntVarMap().get(i));
          IloForAllRange forAll =
              (IloForAllRange) masterOpl.getElement("ctFill").asConstraintMap().get(i);
          masterCplex.setLinearCoef(forAll, newVar, coef);
        }
        masterVars.add(newVar);

        subOpl.end();
      } while (best != curr && status == 0);

      IloNumVar[] masterVarsA = new IloNumVar[masterVars.size()];
      masterVars.toArray(masterVarsA);
      masterCplex.add(masterCplex.conversion(masterVarsA, IloNumVarType.Int));
      if (masterCplex.solve()) {
        System.out.println("OBJECTIVE: " + masterCplex.getObjValue());
      }
      oplF.end();
    } catch (IloOplException ex) {
      System.err.println("### OPL exception: " + ex.getMessage());
      ex.printStackTrace();
      status = 2;
    } catch (IloException ex) {
      System.err.println("### CONCERT exception: " + ex.getMessage());
      ex.printStackTrace();
      status = 3;
    } catch (Exception ex) {
      System.err.println("### UNEXPECTED UNKNOWN ERROR ...");
      ex.printStackTrace();
      status = 4;
    }
    System.exit(status);
  }