Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/ScheduleEncoding.cs @ 13469

Last change on this file since 13469 was 13469, checked in by mkommend, 8 years ago

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

File size: 7.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Encodings.ScheduleEncoding {
35  [StorableClass]
36  public abstract class ScheduleEncoding<TSchedule> : Encoding<TSchedule>, IScheduleEncoding
37  where TSchedule : class, ISchedule {
38    #region Encoding Parameters
39    [Storable]
40    private IFixedValueParameter<ItemList<Job>> jobDataParameter;
41    public IFixedValueParameter<ItemList<Job>> JobDataParameter {
42      get { return jobDataParameter; }
43      set {
44        if (value == null) throw new ArgumentNullException("JobData parameter must not be null.");
45        if (value.Value == null) throw new ArgumentNullException("JobData parameter value must not be null.");
46        if (jobDataParameter == value) return;
47
48        if (jobDataParameter != null) Parameters.Remove(jobDataParameter);
49        jobDataParameter = value;
50        Parameters.Add(jobDataParameter);
51        OnJobDataParameterChanged();
52      }
53    }
54
55    [Storable]
56    private IFixedValueParameter<IntValue> jobsParameter;
57    public IFixedValueParameter<IntValue> JobsParameter {
58      get { return jobsParameter; }
59      set {
60        if (value == null) throw new ArgumentNullException("Jobs parameter must not be null.");
61        if (value.Value == null) throw new ArgumentNullException("Jobs parameter value must not be null.");
62        if (jobsParameter == value) return;
63
64        if (jobsParameter != null) Parameters.Remove(jobsParameter);
65        jobsParameter = value;
66        Parameters.Add(jobsParameter);
67        OnJobsParameterChanged();
68      }
69    }
70    [Storable]
71    private IFixedValueParameter<IntValue> resourcesParameter;
72    public IFixedValueParameter<IntValue> ResourcesParameter {
73      get { return resourcesParameter; }
74      set {
75        if (value == null) throw new ArgumentNullException("Resources parameter must not be null.");
76        if (value.Value == null) throw new ArgumentNullException("Resources parameter value must not be null.");
77        if (resourcesParameter == value) return;
78
79        if (resourcesParameter != null) Parameters.Remove(resourcesParameter);
80        resourcesParameter = value;
81        Parameters.Add(resourcesParameter);
82        OnBoundsParameterChanged();
83      }
84    }
85
86    [Storable]
87    private IValueParameter<IScheduleDecoder<TSchedule>> decoderParameter;
88    public IValueParameter<IScheduleDecoder<TSchedule>> DecoderParameter {
89      get { return decoderParameter; }
90      set {
91        if (value == null) throw new ArgumentNullException("Decoder parameter must not be null.");
92        if (value.Value == null) throw new ArgumentNullException("Decoder parameter value must not be null.");
93        if (decoderParameter == value) return;
94
95        if (decoderParameter != null) Parameters.Remove(decoderParameter);
96        decoderParameter = value;
97        Parameters.Add(decoderParameter);
98        OnDecoderParameterChanged();
99      }
100    }
101    #endregion
102
103    public ItemList<Job> JobData {
104      get { return JobDataParameter.Value; }
105    }
106    public int Jobs {
107      get { return JobsParameter.Value.Value; }
108      set { JobsParameter.Value.Value = value; }
109    }
110    public int Resources {
111      get { return ResourcesParameter.Value.Value; }
112      set { ResourcesParameter.Value.Value = value; }
113    }
114
115    public IScheduleDecoder<TSchedule> Decoder {
116      get { return DecoderParameter.Value; }
117      set { DecoderParameter.Value = value; }
118    }
119
120    [StorableConstructor]
121    protected ScheduleEncoding(bool deserializing) : base(deserializing) { }
122    protected ScheduleEncoding(ScheduleEncoding<TSchedule> original, Cloner cloner)
123      : base(original, cloner) {
124      jobDataParameter = cloner.Clone(original.JobDataParameter);
125      jobsParameter = cloner.Clone(original.JobsParameter);
126      resourcesParameter = cloner.Clone(original.ResourcesParameter);
127      decoderParameter = cloner.Clone(original.DecoderParameter);
128    }
129
130    protected ScheduleEncoding() : this("Schedule") { }
131    protected ScheduleEncoding(string name) : this(name, Enumerable.Empty<Job>()) { }
132    protected ScheduleEncoding(string name, IEnumerable<Job> jobData)
133      : base(name) {
134      int jobs = jobData.Count();
135      int resources = jobData.SelectMany(j => j.Tasks).Select(t => t.ResourceNr).Distinct().Count();
136
137      jobDataParameter = new FixedValueParameter<ItemList<Job>>(Name + ".JobData", new ItemList<Job>(jobData));
138      jobsParameter = new FixedValueParameter<IntValue>(Name + ".Jobs", new IntValue(jobs));
139      resourcesParameter = new FixedValueParameter<IntValue>(Name + ".Resources", new IntValue(resources));
140      decoderParameter = new ValueParameter<IScheduleDecoder<TSchedule>>(Name + ".Decoder");
141
142      Parameters.Add(jobDataParameter);
143      Parameters.Add(jobsParameter);
144      Parameters.Add(resourcesParameter);
145      Parameters.Add(decoderParameter);
146    }
147
148    public Schedule Decode(ISchedule schedule, ItemList<Job> jobData) {
149      return Decoder.DecodeSchedule(schedule, jobData);
150    }
151
152    private void OnJobDataParameterChanged() {
153      ConfigureOperators(Operators);
154    }
155    private void OnJobsParameterChanged() {
156      ConfigureOperators(Operators);
157    }
158    private void OnBoundsParameterChanged() {
159      ConfigureOperators(Operators);
160    }
161
162    private void OnDecoderParameterChanged() {
163      ConfigureOperators(Operators);
164    }
165
166
167    public override void ConfigureOperators(IEnumerable<IItem> operators) {
168      ConfigureCreators(operators.OfType<IScheduleCreator<TSchedule>>());
169      ConfigureCrossovers(operators.OfType<IScheduleCrossover>());
170      ConfigureManipulators(operators.OfType<IScheduleManipulator>());
171    }
172
173    private void ConfigureCreators(IEnumerable<IScheduleCreator<TSchedule>> creators) {
174      foreach (var creator in creators) {
175        creator.ScheduleParameter.ActualName = Name;
176        creator.JobsParameter.ActualName = JobsParameter.Name;
177        creator.ResourcesParameter.ActualName = ResourcesParameter.Name;
178      }
179    }
180
181    private void ConfigureCrossovers(IEnumerable<IScheduleCrossover> crossovers) {
182      foreach (var crossover in crossovers) {
183        crossover.ChildParameter.ActualName = Name;
184        crossover.ParentsParameter.ActualName = Name;
185      }
186    }
187
188    private void ConfigureManipulators(IEnumerable<IScheduleManipulator> manipulators) {
189      foreach (var manipulator in manipulators)
190        manipulator.ScheduleParameter.ActualName = Name;
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.