~kaaloo/workplan/trunk

« back to all changes in this revision

Viewing changes to src/groovy/com/pilango/workplan/ChocoHelper.groovy

  • Committer: Luis Arias
  • Date: 2008-07-15 20:14:35 UTC
  • Revision ID: luis.arias@pilango.com-20080715201435-13cgdf0octvaofmi
ChocoHelper setupShiftVars and setWorkerDayOffConstraints are tested and working in the limited workerDayOff test context.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import choco.cpmodel.CPModel
12
12
import choco.model.variables.integer.IntegerVariable
13
13
import com.pilango.workplan.services.TimeSlotService
 
14
import org.hibernate.criterion.CriteriaSpecification
14
15
 
15
16
import static choco.Choco.*
16
17
 
18
19
        
19
20
        static TimeSlotService timeSlotService = new TimeSlotService()
20
21
        
 
22
        /**
 
23
         * Choco Model
 
24
         */
21
25
        CPModel m
 
26
                
 
27
        /**
 
28
         * A container for all the different variables involved in the domain model.
 
29
         */
22
30
        ShiftVars [] shiftVars
 
31
        
23
32
    IntegerVariable [] slotIndices
24
33
    IntegerVariable [] slotDistances
25
34
        def total
27
36
        ChocoHelper(CPModel m) {
28
37
                this.m = m
29
38
        }
30
 
        
31
 
        void setup(Site [] sites, Shift [] shifts, Worker [] workers, int daysInMonth) {
32
 
        int id = 0
33
 
        int [] siteIds = sites.collect { it.id }
34
 
        int [] workerIds = workers.collect { it.id }
35
 
        shiftVars = shifts.collect {
36
 
            new ShiftVars(id++, siteIds, workerIds, daysInMonth)
37
 
        }               
38
 
        }
39
 
    
 
39
 
40
40
    void bestTimeSlots(TimeSlot [] emptySlots, TimeSlot [] preferredSlots) {
41
41
        int NB_SLOTS = emptySlots.size()
42
42
        slotIndices = new IntegerVariable [NB_SLOTS]
49
49
        }
50
50
        total = makeBoundIntVar("total", 0, TimeSlot.MAX_SLOT_DISTANCE * NB_SLOTS)
51
51
        m.addConstraint(allDifferent(slotIndices))
52
 
        m.addConstraint(eq(total, sum(slotDistances)))          
 
52
        m.addConstraint(eq(total, sum(slotDistances)))      
53
53
    }
54
 
    
 
54
        
 
55
        void setupShiftVars(Worker [] workers, Shift [] shifts, int daysInMonth) {
 
56
        int [] siteIds = (shifts.collect { it.site.id }).unique()
 
57
        int [] workerIds = workers.collect { it.id }
 
58
        int [] shiftIds = shifts.collect { it.id }
 
59
        shiftVars = shifts.collect {
 
60
            new ShiftVars(shiftIds, siteIds, workerIds, daysInMonth)
 
61
        }
 
62
        IntegerVariable [] vars = shiftVars.collect { it.id }
 
63
        def feasTuples = shifts.collect { shift ->
 
64
                int [] tuple = [shift.id, shift.site.id, shift.dayInMonth]
 
65
                tuple
 
66
        }
 
67
        shiftVars.each { shiftVar ->
 
68
                IntegerVariable [] feasVars = [shiftVar.id, shiftVar.siteId, shiftVar.dayInMonth]
 
69
            m.addConstraint(feasTupleAC(feasTuples, feasVars))
 
70
        }
 
71
        IntegerVariable [] shiftIdVars = shiftVars.collect { it.id } 
 
72
        m.addConstraint(allDifferent(shiftIdVars))
 
73
        }
 
74
        
55
75
    void setWorkerDaysOffConstraints(Worker [] workers) {
56
76
        workers.each { worker ->
57
77
                worker.daysOff.each { dayOff ->
58
78
                        shiftVars.each { shiftVar ->
59
 
                                m.addConstraint(not(and(eq(shiftVar.workerId, worker.id), eq(shiftVar.dayOfMonth, dayOff))))
 
79
                                m.addConstraint(makeExpression(not(and(eq(var(shiftVar.workerId), var(worker.id)), eq(var(shiftVar.dayInMonth), var(dayOff))))))
60
80
                        }
61
81
                }
62
82
        }