Coverage Report - net.sourceforge.combean.test.mathprog.grooml.solvers.AbstractTestSupportedSolver
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTestSupportedSolver
100%
9/9
90%
9/10
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1
100%
6/6
50%
6/12
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1_closure3
100%
1/1
N/A
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1_closure4
100%
2/2
100%
2/2
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1_closure4_closure6
100%
1/1
N/A
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1_closure5
100%
2/2
100%
2/2
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure1_closure5_closure7
100%
1/1
N/A
0
AbstractTestSupportedSolver$_checkAssignmentProblem_closure2
100%
2/2
83%
5/6
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Foobar; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 package net.sourceforge.combean.test.mathprog.grooml.solvers;
 19  
 
 20  
 import net.sourceforge.combean.mathprog.grooml.GroomlInterpreter;
 21  
 
 22  
 abstract class AbstractTestSupportedSolver extends GroovyTestCase {
 23  
 
 24  
     private GroomlInterpreter builder;
 25  
     
 26  
     void setUp() {
 27  30
         this.builder = new GroomlInterpreter();
 28  
     }
 29  
     
 30  
     public abstract String solverId();
 31  
 
 32  
     /**
 33  
      * Sample application: Assign workers to days. Maximize the number
 34  
      * of assignments to preferred days.
 35  
      */
 36  
     
 37  
     protected static final workers = ["alice", "bob", "carol"];
 38  
     protected static final weekdays = ["mon", "tue", "wed", "thu", "fri"];
 39  
     
 40  
     protected static final preferredDays = [
 41  
                                           alice : ["mon", "tue", "thu"],
 42  
                                           bob: ["fri"],
 43  
                                           carol : ["tue", "wed", "fri"]
 44  
                                           ];
 45  
     
 46  
     protected int assignmentScore(String worker, String weekday) {
 47  450
         if (preferredDays[worker].contains(weekday)) {
 48  210
             return 1;
 49  
         }
 50  240
         return 0;
 51  
     }
 52  
     
 53  
     protected void checkAssignmentProblem() {
 54  30
         builder.load{
 55  30
             solver(solverId())
 56  
             // Problem formulated as minimization problem because the
 57  
             // Osi-interface of SYMPHONY has a bug with min/max-handling
 58  30
             min()
 59  
             // Indicator variables x[w,d]: assign day d to worker w
 60  30
             vars("x", [w:workers, d:weekdays]) { -assignmentScore(w, d) }
 61  
             // Every day is assigned exactly one worker
 62  30
             rows("coverdays", [d:weekdays]) {
 63  450
                 [ sum([w:workers]) { "x[$w,$d]"}, "= 1" ]
 64  
             }
 65  
             // Every worker gets at most two tasks
 66  30
             rows("maxtasks", [w:workers]) {
 67  270
                 [ sum([d:weekdays]) { "x[$w,$d]"}, "<= 2" ]
 68  
             }
 69  
         }
 70  
 
 71  30
         builder.solve();
 72  30
         Map assignment = [:];
 73  30
         builder.solutions("x").each{ key, val ->
 74  450
                 if (val > 0.5) {
 75  150
                     assignment[key[1]] = key[0];
 76  
                 }
 77  
         }
 78  
 //        weekdays.each{ d -> println "$d: ${assignment[d]}" }
 79  
 //        print "#satisfied preferences: ${-builder.solutionValue()}"
 80  
 
 81  30
         assertEquals([mon:"alice", tue:"carol", wed:"carol", thu:"alice", fri:"bob"],
 82  
                 assignment);
 83  
     }
 84  
 }