[6475] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Parameters;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.ScheduleEncoding.ScheduleEncoding {
|
---|
| 29 | [Item("DirectScheduleGTCrossover", "Represents a crossover using the GT-Algorithm to cross two direct schedule representations.")]
|
---|
| 30 | [StorableClass]
|
---|
| 31 | public class DirectScheduleGTCrossover : DirectScheduleCrossover {
|
---|
| 32 | [StorableConstructor]
|
---|
| 33 | protected DirectScheduleGTCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 34 | protected DirectScheduleGTCrossover(DirectScheduleGTCrossover original, Cloner cloner)
|
---|
| 35 | : base(original, cloner) {
|
---|
| 36 | }
|
---|
| 37 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 38 | return new DirectScheduleGTCrossover(this, cloner);
|
---|
| 39 | }
|
---|
| 40 | public DirectScheduleGTCrossover()
|
---|
| 41 | : base() {
|
---|
| 42 | Parameters.Add(new LookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | private LookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 47 | get { return (LookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | public static Schedule Apply(IRandom random, Schedule parent1, Schedule parent2, ItemList<Job> jobData, double mutProp) {
|
---|
| 52 | Schedule child = new Schedule(parent1.Resources.Count);
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | //Reset scheduled tasks in result
|
---|
| 56 | foreach (Job j in jobData) {
|
---|
| 57 | foreach (Task t in j.Tasks) {
|
---|
| 58 | t.IsScheduled = false;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //GT-Algorithm
|
---|
| 63 | //STEP 0 - Compute a list of "earliest operations"
|
---|
| 64 | ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobData);
|
---|
| 65 | while (earliestTasksList.Count > 0) {
|
---|
| 66 | //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
|
---|
| 67 | Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, child);
|
---|
| 68 | int conflictedResourceNr = minimal.ResourceNr;
|
---|
| 69 | Resource conflictedResource = child.Resources[conflictedResourceNr];
|
---|
| 70 |
|
---|
| 71 | //STEP 2 - Compute a conflict set of all operations that can be scheduled on the conflicted resource
|
---|
| 72 | ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, jobData, child);
|
---|
| 73 |
|
---|
| 74 | //STEP 3 - Select a task from the conflict set
|
---|
| 75 | int progressOnResource = conflictedResource.Tasks.Count;
|
---|
| 76 | Task selectedTask = null;
|
---|
| 77 | if (random.Next(100) < mutProp) {
|
---|
| 78 | //Mutation
|
---|
| 79 | selectedTask = conflictSet[random.Next(conflictSet.Count)];
|
---|
| 80 | } else {
|
---|
| 81 | //Crossover
|
---|
| 82 | selectedTask = SelectTaskFromConflictSet(conflictSet, ((random.Next(2) == 0) ? parent1 : parent2), conflictedResourceNr, progressOnResource);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | //STEP 4 - Add the selected task to the current schedule
|
---|
| 86 | selectedTask.IsScheduled = true;
|
---|
| 87 | double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, child);
|
---|
| 88 | child.ScheduleTask(selectedTask.ResourceNr, startTime, selectedTask.Duration, selectedTask.JobNr);
|
---|
| 89 |
|
---|
| 90 | //STEP 5 - Back to STEP 1
|
---|
| 91 | earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobData);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return child;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | private static Task SelectTaskFromConflictSet(ItemList<Task> conflictSet, Schedule usedParent, int conflictedResourceNr, int progressOnResource) {
|
---|
| 99 | //Apply Crossover
|
---|
| 100 | foreach (ScheduledTask st in usedParent.Resources[conflictedResourceNr].Tasks) {
|
---|
| 101 | foreach (Task t in conflictSet) {
|
---|
| 102 | if (st.JobNr == t.JobNr)
|
---|
| 103 | return t;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | return conflictSet[0];
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | public override Schedule Cross(IRandom random, Schedule parent1, Schedule parent2) {
|
---|
| 111 | ItemList<Job> jobData = (ItemList<Job>)JobDataParameter.ActualValue.Clone();
|
---|
| 112 | PercentValue mutProp = MutationProbabilityParameter.ActualValue;
|
---|
| 113 | return Apply(random, parent1, parent2, jobData, mutProp.Value);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|