visu/house_building_optional.py example

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# --------------------------------------------------------------------------
# Source file provided under Apache License, Version 2.0, January 2004,
# http://www.apache.org/licenses/
# (c) Copyright IBM Corp. 2015, 2016
# --------------------------------------------------------------------------

"""
This is a problem of building five houses. The masonry, roofing,
painting, etc. must be scheduled. Some tasks must necessarily take
place before others and these requirements are expressed through
precedence constraints.

There are three workers, and each worker has a given non-negative
skill level for each task.  Each task requires one worker that will
have to be selected among the ones who have a non null skill level for
that task.  A worker can be assigned to only one task at a time.  Each
house has a deadline. The objective is to maximize the skill levels of
the workers assigned to the tasks while respecting the deadlines.

Please refer to documentation for appropriate setup of solving configuration.
"""

from docplex.cp.model import CpoModel, INTERVAL_MIN
import docplex.cp.utils_visu as visu


#-----------------------------------------------------------------------------
# Initialize the problem data
#-----------------------------------------------------------------------------

NB_HOUSES = 5
DEADLINE = 318
WORKER_NAMES = ['Joe', 'Jack', 'Jim']
NB_WORKERS = len(WORKER_NAMES)

# House building task descriptor
class BuildingTask(object):
    def __init__(self, name, duration, skills):
        self.name = name
        self.duration = duration  # Task duration
        self.skills = skills      # Skills of each worker for this task

# List of tasks to be executed for each house
MASONRY   = BuildingTask('masonry',   35, [9, 5, 0])
CARPENTRY = BuildingTask('carpentry', 15, [7, 0, 5])
PLUMBING  = BuildingTask('plumbing',  40, [0, 7, 0])
CEILING   = BuildingTask('ceiling',   15, [5, 8, 0])
ROOFING   = BuildingTask('roofing',    5, [6, 7, 0])
PAINTING  = BuildingTask('painting',  10, [0, 9, 6])
WINDOWS   = BuildingTask('windows',    5, [8, 0, 5])
FACADE    = BuildingTask('facade',    10, [5, 5, 0])
GARDEN    = BuildingTask('garden',     5, [5, 5, 9])
MOVING    = BuildingTask('moving',     5, [6, 0, 8])

# Tasks precedence constraints (each tuple (X, Y) means X ends before start of Y)
PRECEDENCES = ( (MASONRY, CARPENTRY),
                (MASONRY, PLUMBING),
                (MASONRY, CEILING),
                (CARPENTRY, ROOFING),
                (CEILING, PAINTING),
                (ROOFING, WINDOWS),
                (ROOFING, FACADE),
                (PLUMBING, FACADE),
                (ROOFING, GARDEN),
                (PLUMBING, GARDEN),
                (WINDOWS, MOVING),
                (FACADE, MOVING),
                (GARDEN, MOVING),
                (PAINTING, MOVING),
            )

#-----------------------------------------------------------------------------
# Prepare the data for modeling
#-----------------------------------------------------------------------------

# Assign an index to tasks
ALL_TASKS = (MASONRY, CARPENTRY, PLUMBING, CEILING, ROOFING, PAINTING, WINDOWS, FACADE, GARDEN, MOVING)
for i in range(len(ALL_TASKS)):
    ALL_TASKS[i].id = i


#-----------------------------------------------------------------------------
# Build the model
#-----------------------------------------------------------------------------

# Create model
mdl = CpoModel()

# Initialize model variable sets
total_skill = 0                                 # Expression computing total of skills
worker_tasks = [[] for w in range(NB_WORKERS)]  # Tasks (interval variables) assigned to a each worker
desc = dict()                                   # Map retrieving task from interval variable

# Utility function
def make_house(loc, deadline):
    ''' Create model elements corresponding to the building of a house
    loc      Identification of house location
    deadline Deadline for finishing the house
    '''

    # Create interval variable for each task for this house
    tasks = [mdl.interval_var(size=t.duration,
                          end=(INTERVAL_MIN, deadline),
                          name='H' + str(loc) + '-' + t.name) for t in ALL_TASKS]

    # Add precedence constraints
    for p, s in PRECEDENCES:
        mdl.add(mdl.end_before_start(tasks[p.id], tasks[s.id]))

    # Allocate tasks to workers
    global total_skill
    for t in ALL_TASKS:
        allocs = []
        for w in range(NB_WORKERS):
            if t.skills[w] > 0:
                wt = mdl.interval_var(optional=True, name="H{}-{}({})".format(loc, t.name, WORKER_NAMES[w]))
                worker_tasks[w].append(wt)
                allocs.append(wt)
                total_skill += (t.skills[w] * mdl.presence_of(wt))
                desc[wt] = t
        mdl.add(mdl.alternative(tasks[t.id], allocs))


# Make houses
for h in range(NB_HOUSES):
    make_house(h, DEADLINE)

# Avoid overlapping between tasks of each worker
for w in range(NB_WORKERS):
    mdl.add(mdl.no_overlap(worker_tasks[w]))

# Maximize total of skills
mdl.add(mdl.maximize(total_skill))


#-----------------------------------------------------------------------------
# Solve the model and display the result
#-----------------------------------------------------------------------------

def compact(name):
    # Example: H3-garden -> G3
    #           ^ ^
    loc, task = name[1:].split('-', 1)
    return task[0].upper() + loc

# Solve model
print("Solving model....")
msol = mdl.solve(FailLimit=10000, TimeLimit=10)
print("Solution: ")
msol.print_solution()

# Draw solution
if msol and visu.is_visu_enabled():
    visu.timeline('Solution SchedOptional', 0, DEADLINE)
    for w in range(NB_WORKERS):
        visu.sequence(name=WORKER_NAMES[w])
        for t in worker_tasks[w]:
            wt = msol.get_var_solution(t)
            if wt.is_present():
                if desc[t].skills[w] == max(desc[t].skills):
                    # Green-like color when task is using the most skilled worker
                    color = 'lightgreen'
                else:
                    # Red-like color when task does not use the most skilled worker
                    color = 'salmon'
                visu.interval(wt, color, compact(wt.get_name()))
    visu.show()