-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduling.jl
663 lines (574 loc) · 18.1 KB
/
Scheduling.jl
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
module Scheduling
#######################################
##### DEFINE DEPENDENCIES #####
#######################################
using JuMP
using Gurobi
using XLSX
using DataFrames
using Dates
using CSV
using StatsBase
using DelimitedFiles
using Dates
using Random
using Test
using DataStructures
using Logging
# using WebSockets
####################################
##### EXPORT FUNCTIONS #####
####################################
############ MAIN FUNCTIONS
export large_neighborhood_search
export extract_solution_from_model
export load_sequence
export build_initial_model
export add_constraints!
export create_fix
export create_initial_solution
export solve_job_shop
export create_weights
export weight_to_principle_mapping
export large_neighborhood_search_job_shop
export weeklymodel
export solve_job_shop
export adapter_schedule_environment
export test_speed
export reset_scheduler_agent!
####################################
##### TEST FUNCTIONS #####
####################################
export test_scheduler_agent
############ TYPES
export Solution
export Data
export PreprocessData
export Model
export Period
export All_Data
export IndividualOrderFixing
export Fixing
export OrderFixing
export WeightFixing
export LoadFixing
export SolutionJobShop
export Preprocessing
export JobShopData
export WorkOrder
export ScheduleEnvironment
export OrdersPeriod
export UnloadingPoint
############ VISUALIZATION
export Extract_Orders_From_Solution
export Plot_Order
export Make_Yaxis
export Square
export gantt_chart
######### DATA HANDLING ###########
export create_scheduling_environment
export main
export result_excel
export postProcess
export readInstance
export output
export crew_generator
export write_output
export check_orders_individually
export order
####### MACROS AND GENERATED #######
export @p
export @pt
export @order
export ≂
####################################
##### EXPORT MAS AGENTS #####
####################################
export Agent
export SchedulerAgent
export WorkerAgent
export ActivityAgent
export WorkcenterAgent
export WorkPlannerAgent
export MessageAgent
####################################
##### EXPORT MAS MESSAGES #####
####################################
export Message
export SchedulerMessage
export WorkPlannerMessage
export WorkerMessage
export ShutdownSchedulerMessage
####################################
##### EXPORT MAS FUNCTIONS #####
####################################
export initialize_worker_agents
export initialize_activity_agents
export initialize_scheduler_agents
export initialize_message_agent
export scheduler_internal!
export message_internal!
##########################################################################
# DEFINE DISPATCH TYPES
##########################################################################
abstract type Configuration end
struct InitialConfiguration <: Configuration
orders::Union{Vector{Int}, Colon}
InitialConfiguration() = new(:)
InitialConfiguration(orders) = new(orders)
end
struct IterateConfiguration <: Configuration end
abstract type Fixing end
struct OperationFixing <: Fixing
exclusion::Vector{Int}
number_of_operations::Int
interval::Int
end
struct OrderFixing <: Fixing
exclusion::Vector{Int}
number_of_orders::Int
interval::Int
end
mutable struct WeightFixing <: Fixing
exclusion::Vector{Int}
number_of_orders::Int
interval::Int
rate::Float64
weights::Vector{Float64}
end
struct MustDoFixing <: Fixing
exclusion::Vector{Int}
interval::Vector{Int}
end
struct IndividualOrderFixing <: Fixing
exclusion::Vector{Int}
order::Int
end
struct LoadFixing <: Fixing
exclusion::Vector{Int}
number_of_orders::Int
interval::Int
orders::Vector{Int}
iteration::Int
end
##########################################################################
##### DEFINE DATA TYPES #####
##########################################################################
struct Preprocessing
file_materials::String
file_activities::DataFrame
file_workers::String
first_day::Date
first_week::Int
last_week::Int
operations::UnitRange
days::UnitRange
hours_per_day::Int
end
```
Crucial Insight. Large structs are very impractical, it is just the same as it is for functions.
Structs raction.l This refactoring this throevel of abstraction. refactoring this throughout the
code will be crucial. There seems to be a lot of coding lessons that I need to learn. For this code
to be useful on the other side. Reading software arch
```
struct OrderData
operations::Int
workorder_number::Vector{Int}
workorder_number_for_operation::Vector{Int}
operation_activity::Vector{Int}
workcenter_for_operation::Vector{Symbol}
remaining_work::Vector{Float64}
number::Vector{Int}
material_expected_date::Vector{Union{Missing, DateTime}}
earliest_start_datetime::Vector{Date}
earliest_finish_datetime::Vector{Date}
order_description::Vector{String}
opr_user_status::Vector{String} # combine these?
opr_system_status::Vector{String} # combine these?
order_system_status::Vector{String} # combine these?
order_user_status::Vector{String} # combine these?
notes_1::Vector{String}
notes_2::Vector{Int}
earliest_allowed_start_date::Vector{Date}
latest_allowed_finish_date::Vector{Date}
order_type::Vector{String}
priority::Vector{Union{Int, String}}
abc::Vector{String}
object_description::Vector{String}
unloading_point::Vector{String}
work::Vector{Float64}
work_performed::Vector{Float64}
functional_location::Vector{String} #! Should be handled differently
revision::Vector{String}
exe_start::Vector{Int}
exe_finish::Vector{Int}
crew::Dict{Int, Vector{String}}
absence::Dict{Int, Tuple{Int, Int}}
workcenter_names::Vector{Symbol}
operating_time_workcenter::Vector{Float64}
duration::Vector{Int}
crew_df::DataFrame
end
abstract type Data end
struct StatusCodes
SMAT::Vector{Bool}
NMAT::Vector{Bool}
CMAT::Vector{Bool}
WMAT::Vector{Bool}
PMAT::Vector{Bool}
PCNF::Vector{Bool}
AWSC::Vector{Bool}
WELL::Vector{Bool}
SCH::Vector{Bool}
Unloading_Point::Vector{Bool}
end
struct StatusCodesOrder
SMAT::Bool
NMAT::Bool
CMAT::Bool
WMAT::Bool
PMAT::Bool
PCNF::Bool
AWSC::Bool
WELL::Bool
SCH::Bool
Unloading_Point::Bool
end
struct TwoWeekData <: Data
operations::Vector{Int}
possible_start::Vector{Int}
target_finish::Vector{Int}
weight::Vector{Float32}
work_centers::Vector{Int}
workers::Dict{Int, Tuple{String, Int}}
work_required::Vector{Float16}
pob_limit::Int
orders::Vector{Int}
days::Vector{Int}
operating_time::Vector{Int}
duration::Vector{Int}
order_to_operation::Vector{Vector{Int}}
workcenter_to_operation::Vector{Vector{Int}}
operation_to_workcenter::Vector{Int}
operation_to_workers::Vector{Vector{Int}}
operation_to_individuals::Vector{Vector{Int}}
start_start::Matrix{Int}
finish_start::Matrix{Int}
capacity_per_crew::Matrix{Int}
max_crew::Matrix{Int}
min_crew::Matrix{Int}
people_for_operation::Vector{Int}
level::Vector{Int}
order_priority::Vector{Int}
order_weight::Vector{Int}
order_work::Vector{Float64}
must_do_orders::Vector{Int}
can_do_orders::Vector{Int}
patterns::Array{Int, 3}
workcenter_names::Vector{String}
oto_matrix::Matrix{Int}
exe_start::Vector{Int}
exe_finish::Vector{Int}
weekly_capacity::Vector{Int}
end
struct ModelData <: Data
operations::Vector{Int}
possible_start::Vector{Int}
target_finish::Vector{Int}
weight::Vector{Float32}
work_centers::Vector{Int}
workers::Dict{Int, Tuple{String, Int}}
work_required::Vector{Float16}
pob_limit::Int
orders::Vector{Int}
days::Vector{Int}
operating_time::Vector{Int}
duration::Vector{Int}
order_to_operation::Vector{Vector{Int}}
workcenter_to_operation::Vector{Vector{Int}}
operation_to_workcenter::Vector{Int}
operation_to_workers::Vector{Vector{Int}}
operation_to_individuals::Vector{Vector{Int}}
start_start::Matrix{Int}
finish_start::Matrix{Int}
capacity_per_crew::Matrix{Int}
max_crew::Matrix{Int}
min_crew::Matrix{Int}
people_for_operation::Vector{Int}
level::Vector{Int}
order_priority::Vector{Int}
order_weight::Vector{Int}
order_work::Vector{Float64}
must_do_orders::Vector{Int}
can_do_orders::Vector{Int}
patterns::Array{Int, 3}
workcenter_names::Vector{String}
oto_matrix::Matrix{Int}
exe_start::Vector{Int}
exe_finish::Vector{Int}
end
struct JobShopData <: Data
operations::Vector{Int}
possible_start::Vector{Int}
target_finish::Vector{Int}
weight::Vector{Float32}
workers::UnitRange
work_required::Vector{Float16}
operation_to_individuals::Vector{Vector{Int}}
start_start::Vector{Int}
finish_start::Vector{Int}
# postpone::Vector{Int}
# order_weight::Vector{Int}
# order_work::Vector{Float64}
fixed::Vector{Bool}
end
# We do not need the exe_start and exe_finish. I believe that the
# possible start and target finish should take care of it.
mutable struct Solution
objective_value::Float64
w::Array{Float64, 2}
p::Array{Float64, 2}
c::Array{Float64, 2}
f::Array{Float64, 1}
d::Array{Float64, 1}
end
mutable struct SolutionJobShop
obj::Float64
x::Matrix{Int}
s::Matrix{Float64}
f::Matrix{Float64}
b::Array{Int, 3}
T::Vector{Float64}
end
```
A period is usually a two week interval where start from monday. I am not sure that the DateTime
type will ever be used, but it would be beneficial to be able to include it at some point.
Making that constructor was a really good idea. We should never use the period id for anything. We
should focus completely applying dates as weel as possible.
```
mutable struct Period
id::Int
period_string::String
start_date::DateTime
end_date::DateTime
Period(id, start_date, end_date) = new(
id,
"$(year(start_date))-W$(week(start_date))-$(week(end_date)-1 == 0 ? 52 : week(end_date)-1)",
start_date,
end_date)
Period(id, period_string, start_date, end_date) = new(id, period_string, start_date, end_date)
end
```
Availability is the time that a worker in on the platform, ideally it should be a DateTime as a
worker can arrive/be available at odd times on the platform.
```
struct Availability
start_date::DateTime
end_date::DateTime
end
```
There is a inherrent dependence between the the message that schedules a
work order and the UnloadingPoint type.
We should have a default period. What should the default period be and is it related to the
algorithm. I should explore the idea of parking spaces again.
We should always schedule everything. Everything should have a period. Except the unresolved work
orders that are requireing manual resolution.
Where should the message come from. It should be sent together with the scheduler input that is
comming from the front-end.
I still feel like having a default period. Hmm... could be nice to have on the
shutdown work orders as a way to place them without scheduling them
Creating types for basic start and basic finish is not a bad idea
either. That would make a lot of sense. Then we would be able to handle many more
pieces of information. Like validity of the date. Meaning if we are free to move it or not.
It should be part of the ordersdates I think, hmm... or maybe not.
```
#? It we should a parking spot where should it be.
mutable struct UnloadingPoint
string::String
present::Bool
period::Union{Period, Nothing}
end
struct FunctionalLocation
string::String
end
struct Revision
string::String
shutdown::Bool
Revision(string) = new(
string,
!occursin(r"NOSD", string)
)
Revision(string, shutdown) = new(string, shutdown)
end
struct OrderText
order_system_status::String
order_user_status::String
order_description::String
object_description::String
notes_1::String
notes_2::Int
end
struct OrderDates
earliest_allowed_start_date::DateTime
latest_allowed_finish_date::DateTime
basic_start::DateTime
basic_finish::DateTime
duration::Day
basic_start_scheduled::DateTime
basic_finish_scheduled::DateTime
material_expected_date::Union{DateTime, Missing}
end
```
We should seperate the dates from the efficient float64 time format.
Remember we will create adapter functions to speed the optimization
process if needed. Raising errors to the scheduler when the interfaces are not upheld is actually a
very good idea.
```
struct Operation
activity::Int
number::Int
work_center::Symbol
preparation_time::Float64
work_remaining::Float64
work_performed::Float64
work_adjusted::Float64
operating_time::Float64
duration::Int
possible_start::Float64
target_finish::Float64
earliest_start_datetime::DateTime
earliest_finish_datetime::DateTime
end
```
Work order should have a LAFD and EASD. The choice to
```
struct WorkOrder
order_number::Int
fixed::Bool
order_weight::Int
priority::Union{Int, String}
order_work::Float64
operations::Vector{Operation}
work_load::Dict{Symbol, Float64}
start_start::Vector{Bool}
finish_start::Vector{Bool}
postpone::Vector{Float64}
order_type::String
status_codes::StatusCodesOrder
order_dates::OrderDates
revision::Revision
unloading_point::UnloadingPoint
functional_location::FunctionalLocation
order_text::OrderText
vendor::Bool
end
struct AssignedWork
order::Union{Int, Nothing}
activity::Union{Int, Nothing}
time::Union{Nothing, Float64}
end
mutable struct OrdersPeriod
period::Period
work_orders::WorkOrder
end
```
Should this struct simply be the worker agent. I am not completely sure. What are the
pros and cons of each. They are already Identical... They should be similar.
They should be kept seperate. The WorkerAgents job is to schedule themselves and communicate
with the other agents.
The trait field is a pain. Brian says that a person goes out as one thing and that
multiskill may be an unwanted side effect of bad scheduling. This is interesting.
It should not be a Vector. Maybe create a primary and secondary trait through a
NamedTuple.
What should we
```
struct Worker
name::String
ID::Int
capacity::Float64
trait::Symbol
availabilities::Vector{Availability}
assigned_activities::Union{Vector{AssignedWork}, Nothing}
end
```
Should this struct be here. I am not sure. The question to be answered here is
if the
```
struct Crew
workers::Vector{Worker}
end
function process_crew(crew::Crew)
work_centers = Dict()
multiskill_tracker = Dict{Tuple{Symbol, Symbol}, Symbol}()
for worker in crew.workers
work_centers[worker.trait] = get!(work_centers, worker.trait, 0) + worker.capacity
# if length(worker.trait) == 2
# multiskill_tracker[worker.trait[1], worker.trait[2]] += worker.capacity
# end
end
return work_centers, multiskill_tracker
end
```
This worker environment is wrong. It takes in the wrong things into consideration. It should also
be noted that the scheduler should in the optimizer rely on the WorkcenterAgents. As any capacity
increases would come in here.
```
struct WorkerEnvironment
crew::Crew
work_centers::Dict{Symbol, Float64}
multiskill_tracker::Dict{Tuple{Symbol, Symbol}, Symbol}
function WorkerEnvironment(crew::Crew)
work_centers, multiskill_tracker = process_crew(crew)
new(crew, work_centers, multiskill_tracker)
end
end
struct SchedulingEnvironment
work_orders::Vector{WorkOrder}
worker_environment::WorkerEnvironment
#! time_and_period::<unknown type>
#! material::Material
#! platform::Platform
end
##########################################################################
##### Define Functions #####
##########################################################################
function add_variables!(model::Model, data::Data, ::Configuration) end
function add_objective!(model::Model, data::Data, ::Configuration) end
function add_constraints!(model::Model, data::Data, ::Configuration) end
##########################################################################
##### INCLUDE FILES #####
##########################################################################
include("experiments/startup.jl")
include("helper_functions_and_extensions.jl")
include("data_processing/create_scheduling_environment.jl")
include("data_processing/output.jl")
include("data_processing/show.jl")
include("Matheuristic/model.jl")
include("experiments/14-week-model.jl")
include("experiments/job-shop-formulation.jl")
include("experiments/gantt_mvp.jl")
include("data_processing/SchedulingDataProcessing.jl")
using .SchedulingDataProcessing: read_in_raw_data
using .SchedulingDataProcessing: platform_selector
include("MultiAgentScheduling/MultiAgentScheduling.jl")
using .MultiAgentScheduling: Agent
using .MultiAgentScheduling: SchedulerAgent
using .MultiAgentScheduling: WorkcenterAgent
using .MultiAgentScheduling: WorkerAgent
using .MultiAgentScheduling: ActivityAgent
using .MultiAgentScheduling: WorkPlannerAgent
using .MultiAgentScheduling: MessageAgent
using .MultiAgentScheduling: Message
using .MultiAgentScheduling: SchedulerMessage
# using .MultiAgentScheduling: WorkPlannerMessage
# using .MultiAgentScheduling: WorkerMessage
using .MultiAgentScheduling: ShutdownSchedulerMessage
using .MultiAgentScheduling: initialize_worker_agents
using .MultiAgentScheduling: initialize_activity_agents
using .MultiAgentScheduling: initialize_scheduler_agents
using .MultiAgentScheduling: initialize_message_agent
using .MultiAgentScheduling: scheduler_internal!
using .MultiAgentScheduling: message_internal!
using .MultiAgentScheduling: reset_scheduler_agent!
include("test_functions.jl")
end # module