Пример #1
0
  SetCovering(const SizeOptions& opt) 
  : 
    x(*this, num_alternatives, 0, 1),
    z(*this, 0, 999999)
  {

    // costs per alternative
    int _costs[] = {19, 16, 18, 13, 15, 19, 15, 17, 16, 15};
    IntArgs costs(num_alternatives, _costs);

    // the alternatives and the objects they contain
    int _a[] = {
   // 1 2 3 4 5 6 7 8  the objects 
      1,0,0,0,0,1,0,0,  // alternative 1
      0,1,0,0,0,1,0,1,  // alternative 2
      1,0,0,1,0,0,1,0,  // alternative 3
      0,1,1,0,1,0,0,0,  // alternative 4
      0,1,0,0,1,0,0,0,  // alternative 5
      0,1,1,0,0,0,0,0,  // alternative 6
      0,1,1,1,0,0,0,0,  // alternative 7
      0,0,0,1,1,0,0,1,  // alternative 8
      0,0,1,0,0,1,0,1,  // alternative 9
      1,0,0,0,0,1,1,0,  // alternative 10
    };
    IntArgs a(num_alternatives*num_objects, _a);


    for(int j = 0; j < num_objects; j++) {
      IntVarArgs tmp; 
      for(int i = 0; i < num_alternatives; i++) {
        tmp << expr(*this, x[i]*a[i*num_objects+j]);
      }
      if (opt.size() == 0) {
        // set partition problem:
        // objects must be covered _exactly_ once
        rel(*this, sum(tmp) == 1);
      } else {
        // set covering problem
        // all objects must be covered _at least_ once
        rel(*this, sum(tmp) >= 1);
      }      
    }

    if (opt.search() == SEARCH_DFS) {
      if (opt.size() == 0) {
        rel(*this, z <= 49);
      } else {
        rel(*this, z <= 45);
      }
    }
    
    linear(*this, costs, x, IRT_EQ, z);

    branch(*this, x, INT_VAR_NONE(), INT_VAL_MIN()); 

  }
Пример #2
0
  Investments(const SizeOptions& opt) 
  :    
    x(*this, num_projects, 0, 1),
    total_persons(*this, 0, max_persons),
    total_budget(*this, 0, max_budget),
    total_projects(*this, 0, max_budget),
    total_values(*this, 0, 99999) // large number
  {

    int _values[] = {600,400,100,150, 80,120,200,220, 90,380,290,130, 80,270,280};
    IntArgs values(num_projects, _values);

    int _budgets[] = {35,34,26,12,10,18,32,11,10,22,27,18,16,29,22};
    IntArgs budgets(num_projects, _budgets);

    int _personell[] = {5,3,4,2,2,2,4,1,1,5,3,2,2,4,3};
    IntArgs personell(num_projects, _personell);


    int num_requires = 5;
    // 1-based
    int _requires[] = {
      3, 15,
      4, 15,
      8, 7,
      13, 2,
      14, 2
    };
    IntArgs requires(num_requires*2, _requires);

    int num_not_with = 6;
    // 1-based
    int _not_with[] = 
      {
        1, 10,
        5, 6,
        6, 5,
        10, 1,
        11, 15,
        15, 11
      };
    IntArgs not_with(num_not_with*2, _not_with); 

    linear(*this, personell, x, IRT_EQ, total_persons);
    linear(*this, budgets, x, IRT_EQ, total_budget);
    rel(*this, total_projects==sum(x)); 
    linear(*this, values, x, IRT_EQ, total_values);
    
    // show all optimal solutions (there is exactly one)
    if (opt.search() == SEARCH_DFS) {
      rel(*this, total_values >= 2370);
    }

    //
    // resource limits:
    //

    // total_budget <= max_budget
    rel(*this, total_budget <= max_budget);

    // total_persons <= max_persons
    rel(*this, total_persons <= max_persons);

    // total_projects <= max_projects
    rel(*this, total_projects <= max_projects);


    /*
      Special requirements, using standard Integer Programming "tricks"
    */

    // projects that require other projects
    for(int i = 0; i < num_requires; i++) {
      int r1 = requires[i*2+0] - 1;
      int r2 = requires[i*2+1] - 1;
      rel(*this, x[r1] - x[r2] <= 0);
    }

    // projects excluding other projects
    for(int i = 0; i < num_not_with; i++) {
      int n1 = not_with[i*2+0]-1;
      int n2 = not_with[i*2+1]-1;
      rel(*this, x[n1] + x[n2] <= 1);
    }


    //
    // Branching.
    //
    branch(*this, x, INT_VAR_MAX_MAX(), INT_VAL_MAX()); 


  }