コード例 #1
0
ファイル: SolverCPLEX.java プロジェクト: lipiji/jilp
  /*
   * (non-Javadoc)
   *
   * @see net.sf.javailp.Solver#solve(net.sf.javailp.Problem)
   */
  public Result solve(Problem problem) {
    Map<IloNumVar, Object> numToVar = new HashMap<IloNumVar, Object>();
    Map<Object, IloNumVar> varToNum = new HashMap<Object, IloNumVar>();

    try {
      IloCplex cplex = new IloCplex();

      initWithParameters(cplex);

      for (Object variable : problem.getVariables()) {
        VarType varType = problem.getVarType(variable);
        Number lowerBound = problem.getVarLowerBound(variable);
        Number upperBound = problem.getVarUpperBound(variable);

        double lb = (lowerBound != null ? lowerBound.doubleValue() : Double.NEGATIVE_INFINITY);
        double ub = (upperBound != null ? upperBound.doubleValue() : Double.POSITIVE_INFINITY);

        final IloNumVarType type;
        switch (varType) {
          case BOOL:
            type = IloNumVarType.Bool;
            break;
          case INT:
            type = IloNumVarType.Int;
            break;
          default: // REAL
            type = IloNumVarType.Float;
            break;
        }

        IloNumVar num = cplex.numVar(lb, ub, type);

        numToVar.put(num, variable);
        varToNum.put(variable, num);
      }

      for (Constraint constraint : problem.getConstraints()) {
        IloLinearNumExpr lin = cplex.linearNumExpr();
        Linear linear = constraint.getLhs();
        convert(linear, lin, varToNum);

        double rhs = constraint.getRhs().doubleValue();

        switch (constraint.getOperator()) {
          case LE:
            cplex.addLe(lin, rhs);
            break;
          case GE:
            cplex.addGe(lin, rhs);
            break;
          default: // EQ
            cplex.addEq(lin, rhs);
        }
      }

      if (problem.getObjective() != null) {
        IloLinearNumExpr lin = cplex.linearNumExpr();
        Linear objective = problem.getObjective();
        convert(objective, lin, varToNum);

        if (problem.getOptType() == OptType.MIN) {
          cplex.addMinimize(lin);
        } else {
          cplex.addMaximize(lin);
        }
      }

      for (Hook hook : hooks) {
        hook.call(cplex, varToNum);
      }

      if (!cplex.solve()) {
        cplex.end();
        return null;
      }

      final Result result;
      if (problem.getObjective() != null) {
        Linear objective = problem.getObjective();
        result = new ResultImpl(objective);
      } else {
        result = new ResultImpl();
      }

      for (Entry<Object, IloNumVar> entry : varToNum.entrySet()) {
        Object variable = entry.getKey();
        IloNumVar num = entry.getValue();
        VarType varType = problem.getVarType(variable);

        double value = cplex.getValue(num);
        if (varType.isInt()) {
          int v = (int) Math.round(value);
          result.putPrimalValue(variable, v);
        } else {
          result.putPrimalValue(variable, value);
        }
      }

      cplex.end();

      return result;

    } catch (IloException e) {
      e.printStackTrace();
    }

    return null;
  }
コード例 #2
0
ファイル: TuneSet.java プロジェクト: renvieir/ioc
  public static void main(String[] args) {
    if (args.length < 1) {
      usage();
      return;
    }
    try {
      IloCplex cplex = new IloCplex();

      String fixedfile = null;
      String tunedfile = null;
      int tunemeasure = 0;
      boolean mset = false;
      Vector<String> filenames = new Vector<String>();

      for (int i = 0; i < args.length; ++i) {
        if (args[i].charAt(0) != '-') {
          filenames.add(args[i]);
          continue;
        }
        switch (args[i].charAt(1)) {
          case 'a':
            tunemeasure = 1;
            mset = true;
            break;
          case 'm':
            tunemeasure = 2;
            mset = true;
            break;
          case 'f':
            fixedfile = args[++i];
            break;
          case 'o':
            tunedfile = args[++i];
            break;
        }
      }

      System.out.println("Problem set:");
      for (String name : filenames) {
        System.out.println("  " + name);
      }

      if (mset) cplex.setParam(IloCplex.Param.Tune.Measure, tunemeasure);

      IloCplex.ParameterSet paramset = null;

      if (fixedfile != null) {
        cplex.readParam(fixedfile);
        paramset = cplex.getParameterSet();
        cplex.setDefaults();
      }

      int tunestat = cplex.tuneParam(filenames.toArray(new String[0]), paramset);

      if (tunestat == IloCplex.TuningStatus.Complete) System.out.println("Tuning complete.");
      else if (tunestat == IloCplex.TuningStatus.Abort) System.out.println("Tuning abort.");
      else if (tunestat == IloCplex.TuningStatus.TimeLim) System.out.println("Tuning time limit.");
      else System.out.println("Tuning status unknown.");

      if (tunedfile != null) {
        cplex.writeParam(tunedfile);
        System.out.println("Tuned parameters written to file '" + tunedfile + "'");
      }
      cplex.end();
    } catch (IloException e) {
      System.err.println("Concert exception caught: " + e);
    }
  }