Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding.cs @ 17567

Last change on this file since 17567 was 17567, checked in by abeham, 4 years ago

#2521: work in progress

File size: 7.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace 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    #region Encoding Parameters
37    [Storable]
38    private IValueParameter<ItemList<Job>> jobDataParameter;
39    public IValueParameter<ItemList<Job>> JobDataParameter {
40      get { return jobDataParameter; }
41      set {
42        if (value == null) throw new ArgumentNullException("JobData parameter must not be null.");
43        if (value.Value == null) throw new ArgumentNullException("JobData parameter value must not be null.");
44        if (jobDataParameter == value) return;
45
46        if (jobDataParameter != null) Parameters.Remove(jobDataParameter);
47        jobDataParameter = value;
48        Parameters.Add(jobDataParameter);
49        OnJobDataParameterChanged();
50      }
51    }
52
53    [Storable]
54    private IFixedValueParameter<IntValue> jobsParameter;
55    public IFixedValueParameter<IntValue> JobsParameter {
56      get { return jobsParameter; }
57      set {
58        if (value == null) throw new ArgumentNullException("Jobs parameter must not be null.");
59        if (value.Value == null) throw new ArgumentNullException("Jobs parameter value must not be null.");
60        if (jobsParameter == value) return;
61
62        if (jobsParameter != null) Parameters.Remove(jobsParameter);
63        jobsParameter = value;
64        Parameters.Add(jobsParameter);
65        OnJobsParameterChanged();
66      }
67    }
68    [Storable]
69    private IFixedValueParameter<IntValue> resourcesParameter;
70    public IFixedValueParameter<IntValue> ResourcesParameter {
71      get { return resourcesParameter; }
72      set {
73        if (value == null) throw new ArgumentNullException("Resources parameter must not be null.");
74        if (value.Value == null) throw new ArgumentNullException("Resources parameter value must not be null.");
75        if (resourcesParameter == value) return;
76
77        if (resourcesParameter != null) Parameters.Remove(resourcesParameter);
78        resourcesParameter = value;
79        Parameters.Add(resourcesParameter);
80        OnBoundsParameterChanged();
81      }
82    }
83
84    [Storable]
85    private IValueParameter<IScheduleDecoder<TSchedule>> decoderParameter;
86    public IValueParameter<IScheduleDecoder<TSchedule>> DecoderParameter {
87      get { return decoderParameter; }
88      set {
89        if (value == null) throw new ArgumentNullException("Decoder parameter must not be null.");
90        if (value.Value == null) throw new ArgumentNullException("Decoder parameter value must not be null.");
91        if (decoderParameter == value) return;
92
93        if (decoderParameter != null) Parameters.Remove(decoderParameter);
94        decoderParameter = value;
95        Parameters.Add(decoderParameter);
96        OnDecoderParameterChanged();
97      }
98    }
99    #endregion
100
101    public ItemList<Job> JobData {
102      get { return JobDataParameter.Value; }
103    }
104    public int Jobs {
105      get { return JobsParameter.Value.Value; }
106      set { JobsParameter.Value.Value = value; }
107    }
108    public int Resources {
109      get { return ResourcesParameter.Value.Value; }
110      set { ResourcesParameter.Value.Value = value; }
111    }
112
113    public IScheduleDecoder<TSchedule> Decoder {
114      get { return DecoderParameter.Value; }
115      set { DecoderParameter.Value = value; }
116    }
117
118    [StorableConstructor]
119    protected ScheduleEncoding(StorableConstructorFlag _) : base(_) { }
120    protected ScheduleEncoding(ScheduleEncoding<TSchedule> original, Cloner cloner)
121      : base(original, cloner) {
122      jobDataParameter = cloner.Clone(original.JobDataParameter);
123      jobsParameter = cloner.Clone(original.JobsParameter);
124      resourcesParameter = cloner.Clone(original.ResourcesParameter);
125      decoderParameter = cloner.Clone(original.DecoderParameter);
126    }
127
128    protected ScheduleEncoding(string name) : this(name, Enumerable.Empty<Job>()) { }
129    protected ScheduleEncoding(string name, IEnumerable<Job> jobData)
130      : base(name) {
131      int jobs = jobData.Count();
132      int resources = jobData.SelectMany(j => j.Tasks).Select(t => t.ResourceNr).Distinct().Count();
133
134      jobDataParameter = new ValueParameter<ItemList<Job>>(Name + ".JobData", new ItemList<Job>(jobData));
135      jobsParameter = new FixedValueParameter<IntValue>(Name + ".Jobs", new IntValue(jobs));
136      resourcesParameter = new FixedValueParameter<IntValue>(Name + ".Resources", new IntValue(resources));
137      decoderParameter = new ValueParameter<IScheduleDecoder<TSchedule>>(Name + ".Decoder");
138
139      Parameters.Add(jobDataParameter);
140      Parameters.Add(jobsParameter);
141      Parameters.Add(resourcesParameter);
142      Parameters.Add(decoderParameter);
143    }
144
145    public Schedule Decode(IScheduleSolution schedule, ItemList<Job> jobData) {
146      return Decoder.DecodeSchedule(schedule, jobData);
147    }
148
149    private void OnJobDataParameterChanged() {
150      ConfigureOperators(Operators);
151    }
152    private void OnJobsParameterChanged() {
153      ConfigureOperators(Operators);
154    }
155    private void OnBoundsParameterChanged() {
156      ConfigureOperators(Operators);
157    }
158
159    private void OnDecoderParameterChanged() {
160      ConfigureOperators(Operators);
161    }
162
163
164    public override void ConfigureOperators(IEnumerable<IItem> operators) {
165      base.ConfigureOperators(operators);
166      ConfigureCreators(operators.OfType<IScheduleCreator<TSchedule>>());
167      ConfigureCrossovers(operators.OfType<IScheduleCrossover>());
168      ConfigureManipulators(operators.OfType<IScheduleManipulator>());
169    }
170
171    private void ConfigureCreators(IEnumerable<IScheduleCreator<TSchedule>> creators) {
172      foreach (var creator in creators) {
173        creator.ScheduleParameter.ActualName = Name;
174        creator.JobsParameter.ActualName = JobsParameter.Name;
175        creator.ResourcesParameter.ActualName = ResourcesParameter.Name;
176      }
177    }
178
179    private void ConfigureCrossovers(IEnumerable<IScheduleCrossover> crossovers) {
180      foreach (var crossover in crossovers) {
181        crossover.ChildParameter.ActualName = Name;
182        crossover.ParentsParameter.ActualName = Name;
183      }
184    }
185
186    private void ConfigureManipulators(IEnumerable<IScheduleManipulator> manipulators) {
187      foreach (var manipulator in manipulators)
188        manipulator.ScheduleParameter.ActualName = Name;
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.