1 // Copyright 2011 Hakan Kjellerstrand hakank@gmail.com
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 package com.google.ortools.contrib;
14 
15 import com.google.ortools.Loader;
16 import com.google.ortools.constraintsolver.DecisionBuilder;
17 import com.google.ortools.constraintsolver.IntVar;
18 import com.google.ortools.constraintsolver.OptimizeVar;
19 import com.google.ortools.constraintsolver.Solver;
20 import java.io.*;
21 import java.text.*;
22 import java.util.*;
23 
24 public class CoveringOpl {
25   /** Solves a set covering problem. See http://www.hakank.org/google_or_tools/covering_opl.py */
solve()26   private static void solve() {
27     Solver solver = new Solver("CoveringOpl");
28 
29     //
30     // data
31     //
32     int num_workers = 32;
33     int num_tasks = 15;
34 
35     // Which worker is qualified for each task.
36     // Note: This is 1-based and will be made 0-base below.
37     int[][] qualified = {{1, 9, 19, 22, 25, 28, 31}, {2, 12, 15, 19, 21, 23, 27, 29, 30, 31, 32},
38         {3, 10, 19, 24, 26, 30, 32}, {4, 21, 25, 28, 32}, {5, 11, 16, 22, 23, 27, 31},
39         {6, 20, 24, 26, 30, 32}, {7, 12, 17, 25, 30, 31}, {8, 17, 20, 22, 23},
40         {9, 13, 14, 26, 29, 30, 31}, {10, 21, 25, 31, 32}, {14, 15, 18, 23, 24, 27, 30, 32},
41         {18, 19, 22, 24, 26, 29, 31}, {11, 20, 25, 28, 30, 32}, {16, 19, 23, 31},
42         {9, 18, 26, 28, 31, 32}};
43 
44     int[] cost = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6,
45         6, 6, 7, 8, 9};
46 
47     //
48     // variables
49     //
50     IntVar[] hire = solver.makeIntVarArray(num_workers, 0, 1, "workers");
51     IntVar total_cost = solver.makeScalProd(hire, cost).var();
52 
53     //
54     // constraints
55     //
56     for (int j = 0; j < num_tasks; j++) {
57       // Sum the cost for hiring the qualified workers
58       // (also, make 0-base).
59       int len = qualified[j].length;
60       IntVar[] tmp = new IntVar[len];
61       for (int c = 0; c < len; c++) {
62         tmp[c] = hire[qualified[j][c] - 1];
63       }
64       IntVar b = solver.makeSum(tmp).var();
65       solver.addConstraint(solver.makeGreaterOrEqual(b, 1));
66     }
67 
68     // Objective: Minimize total cost
69     OptimizeVar objective = solver.makeMinimize(total_cost, 1);
70 
71     //
72     // search
73     //
74     DecisionBuilder db =
75         solver.makePhase(hire, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE);
76 
77     solver.newSearch(db, objective);
78 
79     //
80     // output
81     //
82     while (solver.nextSolution()) {
83       System.out.println("Cost: " + total_cost.value());
84       System.out.print("Hire: ");
85       for (int i = 0; i < num_workers; i++) {
86         if (hire[i].value() == 1) {
87           System.out.print(i + " ");
88         }
89       }
90       System.out.println("\n");
91     }
92 
93     solver.endSearch();
94 
95     // Statistics
96     System.out.println();
97     System.out.println("Solutions: " + solver.solutions());
98     System.out.println("Failures: " + solver.failures());
99     System.out.println("Branches: " + solver.branches());
100     System.out.println("Wall time: " + solver.wallTime() + "ms");
101   }
102 
main(String[] args)103   public static void main(String[] args) throws Exception {
104     Loader.loadNativeLibraries();
105     CoveringOpl.solve();
106   }
107 }
108