1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Encodings.ScheduleEncoding {
|
---|
33 | [StorableType("D2FB1AF9-EF13-4ED2-B3E9-D5BE4E5772EA")]
|
---|
34 | public abstract class ScheduleEncoding<TSchedule> : Encoding<TSchedule>, IScheduleEncoding
|
---|
35 | where TSchedule : class, IScheduleSolution {
|
---|
36 | [Storable] public IValueParameter<ItemList<Job>> JobDataParameter { get; private set; }
|
---|
37 | [Storable] public IFixedValueParameter<IntValue> JobsParameter { get; private set; }
|
---|
38 | [Storable] public IFixedValueParameter<IntValue> ResourcesParameter { get; private set; }
|
---|
39 | [Storable] public IValueParameter<IScheduleDecoder<TSchedule>> DecoderParameter { get; private set; }
|
---|
40 |
|
---|
41 | public ItemList<Job> JobData {
|
---|
42 | get { return JobDataParameter.Value; }
|
---|
43 | }
|
---|
44 | public int Jobs {
|
---|
45 | get { return JobsParameter.Value.Value; }
|
---|
46 | set { JobsParameter.Value.Value = value; }
|
---|
47 | }
|
---|
48 | public int Resources {
|
---|
49 | get { return ResourcesParameter.Value.Value; }
|
---|
50 | set { ResourcesParameter.Value.Value = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IScheduleDecoder<TSchedule> Decoder {
|
---|
54 | get { return DecoderParameter.Value; }
|
---|
55 | set { DecoderParameter.Value = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected ScheduleEncoding(StorableConstructorFlag _) : base(_) { }
|
---|
60 | protected ScheduleEncoding(ScheduleEncoding<TSchedule> original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | JobDataParameter = cloner.Clone(original.JobDataParameter);
|
---|
63 | JobsParameter = cloner.Clone(original.JobsParameter);
|
---|
64 | ResourcesParameter = cloner.Clone(original.ResourcesParameter);
|
---|
65 | DecoderParameter = cloner.Clone(original.DecoderParameter);
|
---|
66 |
|
---|
67 | RegisterParameterEvents();
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected ScheduleEncoding(string name) : this(name, Enumerable.Empty<Job>()) { }
|
---|
71 | protected ScheduleEncoding(string name, IEnumerable<Job> jobData)
|
---|
72 | : base(name) {
|
---|
73 | int jobs = jobData.Count();
|
---|
74 | int resources = jobData.SelectMany(j => j.Tasks).Select(t => t.ResourceNr).Distinct().Count();
|
---|
75 |
|
---|
76 | Parameters.Add(JobDataParameter = new ValueParameter<ItemList<Job>>(Name + ".JobData", new ItemList<Job>(jobData)));
|
---|
77 | Parameters.Add(JobsParameter = new FixedValueParameter<IntValue>(Name + ".Jobs", new IntValue(jobs)));
|
---|
78 | Parameters.Add(ResourcesParameter = new FixedValueParameter<IntValue>(Name + ".Resources", new IntValue(resources)));
|
---|
79 | Parameters.Add(DecoderParameter = new ValueParameter<IScheduleDecoder<TSchedule>>(Name + ".Decoder"));
|
---|
80 |
|
---|
81 | RegisterParameterEvents();
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void RegisterParameterEvents() {
|
---|
85 | ItemListParameterChangeHandler<Job>.Create(JobDataParameter, () => {
|
---|
86 | ConfigureOperators(Operators);
|
---|
87 | OnJobDataChanged();
|
---|
88 | });
|
---|
89 | IntValueParameterChangeHandler.Create(JobsParameter, () => {
|
---|
90 | ConfigureOperators(Operators);
|
---|
91 | OnJobsChanged();
|
---|
92 | });
|
---|
93 | IntValueParameterChangeHandler.Create(ResourcesParameter, () => {
|
---|
94 | ConfigureOperators(Operators);
|
---|
95 | OnResourcesChanged();
|
---|
96 | });
|
---|
97 | ParameterChangeHandler<IScheduleDecoder<TSchedule>>.Create(DecoderParameter, () => {
|
---|
98 | ConfigureOperators(Operators);
|
---|
99 | OnDecoderChanged();
|
---|
100 | });
|
---|
101 | }
|
---|
102 |
|
---|
103 | public Schedule Decode(IScheduleSolution schedule, ItemList<Job> jobData) {
|
---|
104 | return Decoder.DecodeSchedule(schedule, jobData);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override void ConfigureOperators(IEnumerable<IItem> operators) {
|
---|
108 | base.ConfigureOperators(operators);
|
---|
109 | ConfigureCreators(operators.OfType<IScheduleCreator<TSchedule>>());
|
---|
110 | ConfigureCrossovers(operators.OfType<IScheduleCrossover>());
|
---|
111 | ConfigureManipulators(operators.OfType<IScheduleManipulator>());
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void ConfigureCreators(IEnumerable<IScheduleCreator<TSchedule>> creators) {
|
---|
115 | foreach (var creator in creators) {
|
---|
116 | creator.ScheduleParameter.ActualName = Name;
|
---|
117 | creator.JobsParameter.ActualName = JobsParameter.Name;
|
---|
118 | creator.ResourcesParameter.ActualName = ResourcesParameter.Name;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void ConfigureCrossovers(IEnumerable<IScheduleCrossover> crossovers) {
|
---|
123 | foreach (var crossover in crossovers) {
|
---|
124 | crossover.ChildParameter.ActualName = Name;
|
---|
125 | crossover.ParentsParameter.ActualName = Name;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void ConfigureManipulators(IEnumerable<IScheduleManipulator> manipulators) {
|
---|
130 | foreach (var manipulator in manipulators)
|
---|
131 | manipulator.ScheduleParameter.ActualName = Name;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public event EventHandler JobDataChanged;
|
---|
135 | protected virtual void OnJobDataChanged() => JobDataChanged?.Invoke(this, EventArgs.Empty);
|
---|
136 |
|
---|
137 | public event EventHandler JobsChanged;
|
---|
138 | protected virtual void OnJobsChanged() => JobsChanged?.Invoke(this, EventArgs.Empty);
|
---|
139 |
|
---|
140 | public event EventHandler ResourcesChanged;
|
---|
141 | protected virtual void OnResourcesChanged() => ResourcesChanged?.Invoke(this, EventArgs.Empty);
|
---|
142 |
|
---|
143 | public event EventHandler DecoderChanged;
|
---|
144 | protected virtual void OnDecoderChanged() => DecoderChanged?.Invoke(this, EventArgs.Empty);
|
---|
145 | }
|
---|
146 | }
|
---|