Changeset 5642 for branches/VNS/HeuristicLab.Algorithms.LocalSearch
- Timestamp:
- 03/09/11 11:17:48 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VNS/HeuristicLab.Algorithms.LocalSearch/3.3/LocalSearchImprovement.cs
r5622 r5642 44 44 private LocalSearchMainLoop loop; 45 45 46 private /*Constrained*/ValueParameter<IMoveGenerator> MoveGeneratorParameter {47 get { return ( /*Constrained*/ValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; }48 } 49 private /*Constrained*/ValueParameter<IMoveMaker> MoveMakerParameter {50 get { return ( /*Constrained*/ValueParameter<IMoveMaker>)Parameters["MoveMaker"]; }51 } 52 private /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter {53 get { return ( /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; }46 private ConstrainedValueParameter<IMoveGenerator> MoveGeneratorParameter { 47 get { return (ConstrainedValueParameter<IMoveGenerator>)Parameters["MoveGenerator"]; } 48 } 49 private ConstrainedValueParameter<IMoveMaker> MoveMakerParameter { 50 get { return (ConstrainedValueParameter<IMoveMaker>)Parameters["MoveMaker"]; } 51 } 52 private ConstrainedValueParameter<ISingleObjectiveMoveEvaluator> MoveEvaluatorParameter { 53 get { return (ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>)Parameters["MoveEvaluator"]; } 54 54 } 55 55 private ValueParameter<IntValue> MaximumIterationsParameter { … … 80 80 81 81 [StorableConstructor] 82 protected LocalSearchImprovement(bool deserializing) : base(deserializing) { } 82 protected LocalSearchImprovement(bool deserializing) : base(deserializing) {} 83 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 Initialize(); 86 } 83 87 protected LocalSearchImprovement(LocalSearchImprovement original, Cloner cloner) 84 88 : base(original, cloner) { 85 89 this.loop = cloner.Clone(original.loop); 90 Initialize(); 86 91 } 87 92 public override IDeepCloneable Clone(Cloner cloner) { … … 92 97 loop = new LocalSearchMainLoop(new BoolValue(true)); 93 98 94 Parameters.Add(new /*Constrained*/ValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution."));95 Parameters.Add(new /*Constrained*/ValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move."));96 Parameters.Add(new /*Constrained*/ValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move."));99 Parameters.Add(new ConstrainedValueParameter<IMoveGenerator>("MoveGenerator", "The operator used to generate moves to the neighborhood of the current solution.")); 100 Parameters.Add(new ConstrainedValueParameter<IMoveMaker>("MoveMaker", "The operator used to perform a move.")); 101 Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveMoveEvaluator>("MoveEvaluator", "The operator used to evaluate a move.")); 97 102 Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(1000))); 98 103 Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(100))); 99 104 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated moves.")); 100 105 Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution.")); 106 107 Initialize(); 108 } 109 110 private void Initialize() { 111 MoveGeneratorParameter.ValueChanged += new EventHandler(MoveGeneratorParameter_ValueChanged); 101 112 } 102 113 103 114 public void Parameterize(IProblem problem) { 104 115 UpdateMoveOperators(problem); 116 ChooseMoveOperators(); 117 118 ParameterizeMoveGenerators(problem as ISingleObjectiveProblem); 119 ParameterizeMoveEvaluators(problem as ISingleObjectiveProblem); 120 ParameterizeMoveMakers(problem as ISingleObjectiveProblem); 121 } 122 123 void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) { 124 ChooseMoveOperators(); 105 125 } 106 126 107 127 private void UpdateMoveOperators(IProblem problem) { 108 /* IMoveGenerator oldMoveGenerator = MoveGenerator; 109 MoveGeneratorParameter.ValidValues.Clear(); 128 IMoveGenerator oldMoveGenerator = MoveGenerator; 129 IMoveMaker oldMoveMaker = MoveMaker; 130 ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator; 131 132 ClearMoveParameters(); 133 110 134 if (problem != null) { 111 135 foreach (IMoveGenerator generator in problem.Operators.OfType<IMoveGenerator>().OrderBy(x => x.Name)) … … 127 151 } 128 152 129 IMoveMaker oldMoveMaker = MoveMaker;130 153 if (oldMoveMaker != null) { 131 154 IMoveMaker mm = MoveMakerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType()); … … 133 156 } 134 157 135 ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator;136 158 if (oldMoveEvaluator != null) { 137 159 ISingleObjectiveMoveEvaluator me = MoveEvaluatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType()); 138 160 if (me != null) MoveEvaluator = me; 139 }*/ 161 } 162 } 163 164 private void ChooseMoveOperators() { 165 IMoveMaker oldMoveMaker = MoveMaker; 166 ISingleObjectiveMoveEvaluator oldMoveEvaluator = MoveEvaluator; 167 168 if (MoveGenerator != null) { 169 List<Type> moveTypes = MoveGenerator.GetType().GetInterfaces().Where(x => typeof(IMoveOperator).IsAssignableFrom(x)).ToList(); 170 foreach (Type type in moveTypes.ToList()) { 171 if (moveTypes.Any(t => t != type && type.IsAssignableFrom(t))) 172 moveTypes.Remove(type); 173 } 174 List<IMoveMaker> validMoveMakers = new List<IMoveMaker>(); 175 List<ISingleObjectiveMoveEvaluator> validMoveEvaluators = new List<ISingleObjectiveMoveEvaluator>(); 176 177 foreach (Type type in moveTypes) { 178 var moveMakers = MoveMakerParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name); 179 foreach (IMoveMaker moveMaker in moveMakers) 180 validMoveMakers.Add(moveMaker); 181 182 var moveEvaluators = MoveEvaluatorParameter.ValidValues.Where(x => type.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name); 183 foreach (ISingleObjectiveMoveEvaluator moveEvaluator in moveEvaluators) 184 validMoveEvaluators.Add(moveEvaluator); 185 } 186 if (oldMoveMaker != null) { 187 IMoveMaker mm = validMoveMakers.FirstOrDefault(x => x.GetType() == oldMoveMaker.GetType()); 188 if (mm != null) MoveMaker = mm; 189 else MoveMaker = validMoveMakers.FirstOrDefault(); 190 } 191 192 if (oldMoveEvaluator != null) { 193 ISingleObjectiveMoveEvaluator me = validMoveEvaluators.FirstOrDefault(x => x.GetType() == oldMoveEvaluator.GetType()); 194 if (me != null) MoveEvaluator = me; 195 else MoveEvaluator = validMoveEvaluators.FirstOrDefault(); 196 } 197 } 140 198 } 141 199 142 200 private void ClearMoveParameters() { 143 /*MoveMakerParameter.ValidValues.Clear(); 144 MoveEvaluatorParameter.ValidValues.Clear();*/ 201 MoveGeneratorParameter.ValidValues.Clear(); 202 MoveMakerParameter.ValidValues.Clear(); 203 MoveEvaluatorParameter.ValidValues.Clear(); 204 } 205 206 private void ParameterizeMoveGenerators(ISingleObjectiveProblem problem) { 207 if (problem != null) { 208 foreach (IMultiMoveGenerator generator in problem.Operators.OfType<IMultiMoveGenerator>()) 209 generator.SampleSizeParameter.ActualName = SampleSizeParameter.Name; 210 } 211 } 212 private void ParameterizeMoveEvaluators(ISingleObjectiveProblem problem) { 213 foreach (ISingleObjectiveMoveEvaluator op in problem.Operators.OfType<ISingleObjectiveMoveEvaluator>()) { 214 op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName; 215 } 216 } 217 private void ParameterizeMoveMakers(ISingleObjectiveProblem problem) { 218 foreach (IMoveMaker op in problem.Operators.OfType<IMoveMaker>()) { 219 op.QualityParameter.ActualName = problem.Evaluator.QualityParameter.ActualName; 220 if (MoveEvaluator != null) 221 op.MoveQualityParameter.ActualName = MoveEvaluator.MoveQualityParameter.ActualName; 222 } 145 223 } 146 224
Note: See TracChangeset
for help on using the changeset viewer.