Coverage Report - net.sourceforge.combean.test.mathprog.grooml.TestLPSampleApps
 
Classes in this File Line Coverage Branch Coverage Complexity
TestLPSampleApps
100%
18/18
62%
5/8
0
TestLPSampleApps$_testAssignmentProblem_closure1
100%
1/1
83%
5/6
0
TestLPSampleApps$_testAssignmentProblem_closure2
100%
5/5
50%
6/12
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8
100%
5/5
50%
5/10
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure10
100%
1/1
N/A
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure11
100%
1/1
N/A
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure12
100%
1/1
100%
4/4
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure12_closure14
100%
1/1
N/A
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure13
100%
1/1
75%
6/8
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure8_closure13_closure15
100%
1/1
N/A
0
TestLPSampleApps$_testAssignmentProblem_closure2_closure9
100%
3/3
75%
9/12
0
TestLPSampleApps$_testAssignmentProblem_closure3
100%
1/1
100%
4/4
0
TestLPSampleApps$_testFractionalKnapsack_closure4
100%
4/4
50%
3/6
0
TestLPSampleApps$_testFractionalKnapsack_closure4_closure16
100%
4/4
50%
3/6
0
TestLPSampleApps$_testFractionalKnapsack_closure4_closure16_closure17
100%
1/1
N/A
0
TestLPSampleApps$_testFractionalKnapsack_closure4_closure16_closure18
100%
2/2
100%
4/4
0
TestLPSampleApps$_testFractionalKnapsack_closure4_closure16_closure18_closure19
100%
1/1
N/A
0
TestLPSampleApps$_testFractionalKnapsack_closure5
100%
1/1
100%
4/4
0
TestLPSampleApps$_testIntegerKnapsack_closure6
100%
4/4
50%
3/6
0
TestLPSampleApps$_testIntegerKnapsack_closure6_closure20
100%
4/4
50%
3/6
0
TestLPSampleApps$_testIntegerKnapsack_closure6_closure20_closure21
100%
1/1
N/A
0
TestLPSampleApps$_testIntegerKnapsack_closure6_closure20_closure22
100%
2/2
100%
4/4
0
TestLPSampleApps$_testIntegerKnapsack_closure6_closure20_closure22_closure23
100%
1/1
N/A
0
TestLPSampleApps$_testIntegerKnapsack_closure7
100%
1/1
100%
4/4
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
 19  
 
 20  
 import net.sourceforge.combean.mathprog.grooml.GroomlInterpreter;
 21  
 
 22  
 class TestLPSampleApps extends GroovyTestCase {
 23  
 
 24  
     private GroomlInterpreter builder;
 25  
     
 26  
     void setUp() {
 27  9
         this.builder = new GroomlInterpreter();
 28  
     }
 29  
 
 30  
     /**
 31  
      * Sample application 1: Assign workers to days. Maximize the number
 32  
      * of assignments to preferred days.
 33  
      */
 34  
     void testAssignmentProblem() {
 35  3
         def workers = ["alice", "bob", "carol"];
 36  3
         def weekdays = ["mon", "tue", "wed", "thu", "fri"];
 37  
         
 38  3
         def preferredDays = [ alice : ["mon", "tue"],
 39  
                               bob: ["fri"],
 40  
                               carol : ["wed", "thu"],
 41  
                              ];
 42  
 
 43  3
         def assignmentScore = { worker, weekday ->
 44  90
             preferredDays[worker].contains(weekday) ? 1 : 0
 45  
         }
 46  
 
 47  3
         def assignment = [:];
 48  3
         def score = 0;
 49  3
         builder.exec{
 50  3
             load {
 51  3
                 max()
 52  
                 // Indicator variables x[w,d]: assign day d to worker w
 53  3
                 vars("x", [w:workers, d:weekdays]) { assignmentScore(w, d) }
 54  
                 // Slack variables for the number of tasks that one worker may get
 55  3
                 vars("s", [w:workers]) { [-0.5, 0..1] }
 56  
                 // Every day is assigned exactly one worker
 57  3
                 rows("coverdays", [d:weekdays]) {
 58  60
                     sum(w:workers) { x[w,d] } | 1
 59  
                 }
 60  
                 // Every worker shall get two tasks if possible and at least one
 61  3
                 rows("maxtasks", [w:workers]) {
 62  36
                     sum(d:weekdays) { x[w,d] } + s[w] | 2
 63  
                 }
 64  
             }
 65  3
             solver("qsopt")
 66  3
             solve();
 67  3
             solutions("x").each{ key, val ->
 68  45
                     if (val > 0.5) {
 69  15
                         println "${key[1]}: ${key[0]} ($val)";
 70  15
                         assignment[key[1]] = key[0];
 71  
                     }
 72  
             }
 73  3
             score = solutionValue();
 74  
         }
 75  
 
 76  3
         println builder.getModel().toSummaryString();
 77  15
         weekdays.each{ d -> println "$d: ${assignment[d]}" }
 78  3
         print "#satisfied preferences: $score"
 79  3
         assertEquals([mon:"alice", tue:"alice", wed:"carol", thu:"carol", fri:"bob"],
 80  
                 assignment);
 81  
     }
 82  
     
 83  
     /**
 84  
      * Sample application: Knapsack
 85  
      */
 86  
     def items = ["ring", "money", "diamond", "painting", "statue"]
 87  
     def value = [ring:4, money:2, diamond:10, painting:10, statue:20]
 88  
     def weight = [ring:1, money:2, diamond:1, painting:5, statue: 20]
 89  
 
 90  
     void testFractionalKnapsack() {
 91  3
         builder.exec{
 92  3
             load {
 93  3
                                 max()
 94  
                                 // Indicator variables for the chosen items
 95  3
                                 vars("x", items) { [value[it], 0..1] }
 96  
                                 // Maximum weight to be carried away
 97  3
                                 row("maxweight") {
 98  9
                                     sum([i:items]) { weight[i] * x[i] } << 4
 99  
                                 }
 100  
             }
 101  3
             solver("cbc")
 102  3
             solve()
 103  
         }
 104  3
         def expected = [ring:1, money:0, diamond:1, painting:0.4, statue:0]
 105  3
         builder.solutions("x").each {
 106  15
                         key, val -> assertEquals(expected[key], val, 0.001)            
 107  
         }
 108  
     }
 109  
 
 110  
     void testIntegerKnapsack() {
 111  3
         builder.exec{
 112  3
             load {
 113  3
                                  max()
 114  
                                 // Indicator variables for the chosen items
 115  3
                                 intvars("x", items) { [value[it], 0..1] }
 116  
                                 // Maximum weight to be carried away
 117  3
                                 row("maxweight") {
 118  9
                                     sum([i:items]) { weight[i] * x[i] } << 4
 119  
                                 }
 120  
             }
 121  3
             solver("cbc")
 122  3
             solveMIP()
 123  
         }
 124  
 //        println builder.solutions("x")
 125  3
         def expected = [ring:1, money:1, diamond:1, painting:0, statue:0]
 126  3
         builder.solutions("x").each {
 127  15
                         key, val -> assertEquals("wrong value for '$key'", expected[key], val, 0.001)            
 128  
         }
 129  
     }
 130  
 }