예제 #1
0
    }     // END WorkerLP

    // This method creates the master ILP (arc variables x and degree constraints).
    //
    // Modeling variables:
    // forall (i,j) in A:
    //    x(i,j) = 1, if arc (i,j) is selected
    //           = 0, otherwise
    //
    // Objective:
    // minimize sum((i,j) in A) c(i,j) * x(i,j)
    //
    // Degree constraints:
    // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1
    // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1
    //
    // Binary constraints on arc variables:
    // forall (i,j) in A: x(i,j) in {0, 1}
    //
    internal static void CreateMasterILP(IModeler model,
                                         Data data,
                                         IIntVar[][] x)
    {
        int i, j;
        int numNodes = data.numNodes;

        // Create variables x(i,j) for (i,j) in A
        // For simplicity, also dummy variables x(i,i) are created.
        // Those variables are fixed to 0 and do not partecipate to
        // the constraints.

        for (i = 0; i < numNodes; ++i)
        {
            x[i] = new IIntVar[numNodes];
            for (j = 0; j < numNodes; ++j)
            {
                x[i][j] = model.BoolVar("x." + i + "." + j);
                model.Add(x[i][j]);
            }
            x[i][i].UB = 0;
        }

        // Create objective function: minimize sum((i,j) in A ) c(i,j) * x(i,j)

        ILinearNumExpr objExpr = model.LinearNumExpr();

        for (i = 0; i < numNodes; ++i)
        {
            objExpr.Add(model.ScalProd(x[i], data.arcCost[i]));
        }
        model.AddMinimize(objExpr);

        // Add the out degree constraints.
        // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1

        for (i = 0; i < numNodes; ++i)
        {
            ILinearNumExpr expr = model.LinearNumExpr();
            for (j = 0; j < i; ++j)
            {
                expr.AddTerm(x[i][j], 1.0);
            }
            for (j = i + 1; j < numNodes; ++j)
            {
                expr.AddTerm(x[i][j], 1.0);
            }
            model.AddEq(expr, 1.0);
        }

        // Add the in degree constraints.
        // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1

        for (i = 0; i < numNodes; ++i)
        {
            ILinearNumExpr expr = model.LinearNumExpr();
            for (j = 0; j < i; ++j)
            {
                expr.AddTerm(x[j][i], 1.0);
            }
            for (j = i + 1; j < numNodes; ++j)
            {
                expr.AddTerm(x[j][i], 1.0);
            }
            model.AddEq(expr, 1.0);
        }
    } // END CreateMasterILP
예제 #2
0
    // This method creates the master ILP (arc variables x and degree constraints).
    //
    // Modeling variables:
    // forall (i,j) in A:
    //    x(i,j) = 1, if arc (i,j) is selected
    //           = 0, otherwise
    //
    // Objective:
    // minimize sum((i,j) in A) c(i,j) * x(i,j)
    //
    // Degree constraints:
    // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1
    // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1
    //
    // Binary constraints on arc variables:
    // forall (i,j) in A: x(i,j) in {0, 1}
    //
    internal static void CreateMasterILP(IModeler model,
                                        Data data,
                                        IIntVar[][] x)
    {
        int i, j;
          int numNodes = data.numNodes;

          // Create variables x(i,j) for (i,j) in A
          // For simplicity, also dummy variables x(i,i) are created.
          // Those variables are fixed to 0 and do not partecipate to
          // the constraints.

          for (i = 0; i < numNodes; ++i)
          {
         x[i] = new IIntVar[numNodes];
         for (j = 0; j < numNodes; ++j)
         {
            x[i][j] = model.BoolVar("x." + i + "." + j);
            model.Add(x[i][j]);
         }
         x[i][i].UB = 0;
          }

          // Create objective function: minimize sum((i,j) in A ) c(i,j) * x(i,j)

          ILinearNumExpr objExpr = model.LinearNumExpr();
          for (i = 0; i < numNodes; ++i)
         objExpr.Add(model.ScalProd(x[i], data.arcCost[i]));
          model.AddMinimize(objExpr);

          // Add the out degree constraints.
          // forall i in V: sum((i,j) in delta+(i)) x(i,j) = 1

          for (i = 0; i < numNodes; ++i)
          {
         ILinearNumExpr expr = model.LinearNumExpr();
         for (j = 0; j < i; ++j) expr.AddTerm(x[i][j], 1.0);
         for (j = i + 1; j < numNodes; ++j) expr.AddTerm(x[i][j], 1.0);
         model.AddEq(expr, 1.0);
          }

          // Add the in degree constraints.
          // forall i in V: sum((j,i) in delta-(i)) x(j,i) = 1

          for (i = 0; i < numNodes; ++i)
          {
         ILinearNumExpr expr = model.LinearNumExpr();
         for (j = 0; j < i; ++j) expr.AddTerm(x[j][i], 1.0);
         for (j = i + 1; j < numNodes; ++j) expr.AddTerm(x[j][i], 1.0);
         model.AddEq(expr, 1.0);
          }
    }