Changeset 17570
- Timestamp:
- 05/28/20 16:26:46 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 1 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorMultiObjectiveProblem.cs
r17567 r17570 38 38 public int Dimension { 39 39 get { return DimensionRefParameter.Value.Value; } 40 protected set { DimensionRefParameter.Value.Value = value; } 40 41 } 41 42 -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorProblem.cs
r17567 r17570 39 39 public int Dimension { 40 40 get { return DimensionRefParameter.Value.Value; } 41 protected set { DimensionRefParameter.Value.Value = value; } 41 42 } 42 43 -
branches/2521_ProblemRefactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs
r17226 r17570 284 284 285 285 public override void ConfigureOperators(IEnumerable<IItem> operators) { 286 base.ConfigureOperators(operators); 286 287 ConfigureTreeOperators(operators.OfType<ISymbolicExpressionTreeOperator>()); 287 288 ConfigureSizeConstraintOperators(operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r17567 r17570 118 118 public virtual void ConfigureOperators(IEnumerable<IItem> operators) { 119 119 ConfigureSingleObjectiveImprovementOperators(operators.OfType<ISingleObjectiveImprovementOperator>()); 120 ConfigureSingleObjectivePathRelinker(operators.OfType<ISingleObjectivePathRelinker>()); 120 121 } 121 122 … … 123 124 foreach (var op in operators) { 124 125 op.SolutionParameter.ActualName = Name; 126 op.SolutionParameter.Hidden = true; 127 } 128 } 129 130 protected virtual void ConfigureSingleObjectivePathRelinker(IEnumerable<ISingleObjectivePathRelinker> operators) { 131 foreach (var op in operators.OfType<ISingleObjectivePathRelinker>()) { 132 op.ParentsParameter.ActualName = Name; 133 op.ParentsParameter.Hidden = true; 125 134 } 126 135 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r17513 r17570 138 138 139 139 private void RegisterEvents() { 140 EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged();141 EvaluatorParameter.ValueChanged += (o, e) => OnEvaluatorChanged();140 EncodingParameter.ValueChanged += (o, e) => { ParameterizeOperators(); OnEncodingChanged(); }; 141 EvaluatorParameter.ValueChanged += (o, e) => { ParameterizeOperators(); OnEvaluatorChanged(); }; 142 142 //var multiEncoding = Encoding as MultiEncoding; 143 143 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; 144 144 } 145 145 146 protected override void ParameterizeOperators() { 147 base.ParameterizeOperators(); 148 Parameterize(); 149 } 150 146 151 protected virtual void OnEncodingChanged() { 147 Parameterize();148 149 152 OnOperatorsChanged(); 150 153 OnReset(); … … 163 166 op.EncodingParameter.ActualName = EncodingParameter.Name; 164 167 168 Encoding.ConfigureOperators(Operators); 165 169 //var multiEncoding = Encoding as MultiEncoding; 166 170 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r17567 r17570 72 72 BestKnownQualityParameter = cloner.Clone(original.BestKnownQualityParameter); 73 73 MaximizationParameter = cloner.Clone(original.MaximizationParameter); 74 Parameterize Operators();74 Parameterize(); 75 75 RegisterEventHandlers(); 76 76 } … … 90 90 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 91 91 92 Parameterize Operators();92 Parameterize(); 93 93 RegisterEventHandlers(); 94 94 } … … 105 105 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 106 106 107 Parameterize Operators();107 Parameterize(); 108 108 RegisterEventHandlers(); 109 109 } … … 111 111 [StorableHook(HookType.AfterDeserialization)] 112 112 private void AfterDeserialization() { 113 Parameterize Operators();113 Parameterize(); 114 114 RegisterEventHandlers(); 115 115 } … … 190 190 protected override void OnEvaluatorChanged() { 191 191 base.OnEvaluatorChanged(); 192 Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged; 193 } 194 195 protected virtual void QualityParameterOnActualNameChanged(object sender, EventArgs e) { 192 196 ParameterizeOperators(); 193 197 } 194 198 195 private void ParameterizeOperators() { 199 protected override void ParameterizeOperators() { 200 base.ParameterizeOperators(); 201 Parameterize(); 202 } 203 204 private void Parameterize() { 196 205 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>()) 197 206 op.Evaluate = Evaluate; … … 200 209 foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>()) 201 210 op.GetNeighbors = GetNeighbors; 211 foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) { 212 op.SolutionVariableName = Encoding.Name; 213 op.QualityVariableName = Evaluator.QualityParameter.ActualName; 214 } 202 215 } 203 216 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/Problem.cs
r17513 r17570 157 157 158 158 159 protected virtual void ParameterizeOperators() { 160 161 } 162 159 163 160 164 #region events 161 165 private void Operators_Changed(object sender, EventArgs e) { 162 166 OnOperatorsChanged(); 167 ParameterizeOperators(); 163 168 } 164 169 public event EventHandler OperatorsChanged; -
branches/2521_ProblemRefactoring/HeuristicLab.Parameters/3.3/OptionalValueParameter.cs
r17567 r17570 45 45 get { return this.value; } 46 46 set { 47 if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");48 47 DoSetValue(value); 49 48 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/DeceptiveTrapProblem.cs
r17567 r17570 39 39 [Storable] public IFixedValueParameter<IntValue> TrapSizeParameter { get; private set; } 40 40 [Storable] public IFixedValueParameter<IntValue> NumberOfTrapsParameter { get; private set; } 41 42 public new int Dimension { 43 get { return base.Dimension; } 44 set { DimensionRefParameter.Value.Value = value; } 45 } 46 41 47 42 public int TrapSize { 48 43 get { return TrapSizeParameter.Value.Value; } … … 63 58 Parameters.Add(TrapSizeParameter = new FixedValueParameter<IntValue>("Trap Size", "", new IntValue(7))); 64 59 Parameters.Add(NumberOfTrapsParameter = new FixedValueParameter<IntValue>("Number of Traps", "", new IntValue(7))); 65 DimensionRefParameter.Value = new IntValue(TrapSize * NumberOfTraps); 60 DimensionRefParameter.ReadOnly = true; 61 Dimension = TrapSize * NumberOfTraps; 66 62 67 63 RegisterEventHandlers(); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/HIFFProblem.cs
r17567 r17570 38 38 public new int Dimension { 39 39 get { return base.Dimension; } 40 set { DimensionRefParameter.Value.Value= value; }40 set { base.Dimension = value; } 41 41 } 42 42 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/OneMaxProblem.cs
r17567 r17570 36 36 public new int Dimension { 37 37 get { return base.Dimension; } 38 set { DimensionRefParameter.Value.Value= value; }38 set { base.Dimension = value; } 39 39 } 40 40 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblemInstances.cs
r17567 r17570 45 45 46 46 public int Dimension { 47 get => Encoding.Length;48 set => Encoding.Length= value;47 get => DimensionRefParameter.Value.Value; 48 set => DimensionRefParameter.Value.Value = value; 49 49 } 50 50 … … 59 59 : base(new BinaryVectorEncoding()) { 60 60 Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the vector.", Encoding.LengthParameter)); 61 DimensionRefParameter.Value = new IntValue(Dimension, @readonly: false);62 61 // TODO: Add and parameterize additional operators, 63 62 } … … 263 262 264 263 public int Dimension { 265 get => Encoding.Length;266 set => Encoding.Length= value;264 get => DimensionRefParameter.Value.Value; 265 set => DimensionRefParameter.Value.Value = value; 267 266 } 268 267 … … 277 276 : base(new BinaryVectorEncoding()) { 278 277 Parameters.Add(DimensionRefParameter = new ReferenceParameter<IntValue>("Dimension", "The dimension of the vector.", Encoding.LengthParameter)); 279 DimensionRefParameter.Value = new IntValue(Dimension, @readonly: false);280 278 // TODO: Add and parameterize additional operators, 281 279 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/HeuristicLab.Problems.Knapsack-3.3.csproj
r16723 r17570 126 126 <Compile Include="Plugin.cs" /> 127 127 <Compile Include="Properties\AssemblyInfo.cs" /> 128 <Compile Include="Analyzers\BestKnapsackSolutionAnalyzer.cs" />129 128 <Compile Include="SimilarityCalculators\KnapsackSimilarityCalculator.cs" /> 130 129 </ItemGroup> … … 223 222 </Reference> 224 223 </ItemGroup> 224 <ItemGroup /> 225 225 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 226 226 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r17567 r17570 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using System.Threading; … … 41 40 42 41 #region Parameter Properties 43 public ValueParameter<IntValue> KnapsackCapacityParameter { 44 get { return (ValueParameter<IntValue>)Parameters["KnapsackCapacity"]; } 45 } 46 public ValueParameter<IntArray> WeightsParameter { 47 get { return (ValueParameter<IntArray>)Parameters["Weights"]; } 48 } 49 public ValueParameter<IntArray> ValuesParameter { 50 get { return (ValueParameter<IntArray>)Parameters["Values"]; } 51 } 52 public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter { 53 get { return (OptionalValueParameter<BinaryVector>)Parameters["BestKnownSolution"]; } 54 } 42 [Storable] public ValueParameter<IntValue> KnapsackCapacityParameter { get; private set; } 43 [Storable] public ValueParameter<IntArray> WeightsParameter { get; private set; } 44 [Storable] public ValueParameter<IntArray> ValuesParameter { get; private set; } 45 [Storable] public OptionalValueParameter<BinaryVector> BestKnownSolutionParameter { get; private set; } 55 46 #endregion 56 47 57 48 #region Properties 58 public new int Dimension {59 get { return base.Dimension; }60 set { DimensionRefParameter.Value.Value = value; }61 }62 49 public int KnapsackCapacity { 63 50 get { return KnapsackCapacityParameter.Value.Value; } … … 75 62 get { return BestKnownSolutionParameter.Value; } 76 63 set { BestKnownSolutionParameter.Value = value; } 77 }78 private BestKnapsackSolutionAnalyzer BestKnapsackSolutionAnalyzer {79 get { return Operators.OfType<BestKnapsackSolutionAnalyzer>().FirstOrDefault(); }80 64 } 81 65 #endregion … … 85 69 private KnapsackProblem(KnapsackProblem original, Cloner cloner) 86 70 : base(original, cloner) { 71 KnapsackCapacityParameter = cloner.Clone(original.KnapsackCapacityParameter); 72 WeightsParameter = cloner.Clone(original.WeightsParameter); 73 ValuesParameter = cloner.Clone(original.ValuesParameter); 74 BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter); 87 75 RegisterEventHandlers(); 88 76 } 89 77 public KnapsackProblem() 90 78 : base(new BinaryVectorEncoding("Selection")) { 79 DimensionRefParameter.ReadOnly = true; 91 80 Maximization = true; 92 Parameters.Add( new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(1)));93 Parameters.Add( new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5)));94 Parameters.Add( new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5)));95 Parameters.Add( new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance."));96 97 DimensionRefParameter.Value = new IntValue(Weights.Length, @readonly: true); 81 Parameters.Add(KnapsackCapacityParameter = new ValueParameter<IntValue>("KnapsackCapacity", "Capacity of the Knapsack.", new IntValue(1))); 82 Parameters.Add(WeightsParameter = new ValueParameter<IntArray>("Weights", "The weights of the items.", new IntArray(5))); 83 Parameters.Add(ValuesParameter = new ValueParameter<IntArray>("Values", "The values of the items.", new IntArray(5))); 84 Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<BinaryVector>("BestKnownSolution", "The best known solution of this Knapsack instance.")); 85 Dimension = Weights.Length; 86 98 87 InitializeRandomKnapsackInstance(); 99 88 … … 114 103 } 115 104 105 public override void Analyze(BinaryVector[] solutions, double[] qualities, ResultCollection results, IRandom random) { 106 base.Analyze(solutions, qualities, results, random); 107 108 var best = GetBestSolution(solutions, qualities); 109 110 if (double.IsNaN(BestKnownQuality) || IsBetter(best.Item2, BestKnownQuality)) { 111 BestKnownQuality = best.Item2; 112 BestKnownSolution = (BinaryVector)best.Item1.Clone(); 113 } 114 115 IResult result; 116 if (!results.TryGetValue("Best Knapsack Solution", out result)) { 117 results.Add(result = new Result("Best Knapsack Solution", typeof(KnapsackSolution))); 118 } 119 var solution = (KnapsackSolution)result.Value; 120 if (solution == null) { 121 solution = new KnapsackSolution((BinaryVector)best.Item1.Clone(), new DoubleValue(best.Item2), 122 KnapsackCapacityParameter.Value, WeightsParameter.Value, ValuesParameter.Value); 123 result.Value = solution; 124 } else { 125 if (IsBetter(best.Item2, solution.Quality.Value)) { 126 solution.BinaryVector = (BinaryVector)best.Item1.Clone(); 127 solution.Quality = new DoubleValue(best.Item2); 128 solution.Capacity = KnapsackCapacityParameter.Value; 129 solution.Weights = WeightsParameter.Value; 130 solution.Values = ValuesParameter.Value; 131 } 132 } 133 } 134 116 135 public override IDeepCloneable Clone(Cloner cloner) { 117 136 return new KnapsackProblem(this, cloner); … … 124 143 125 144 private void RegisterEventHandlers() { 126 Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;127 KnapsackCapacityParameter.ValueChanged += KnapsackCapacityParameter_ValueChanged;128 145 WeightsParameter.ValueChanged += WeightsParameter_ValueChanged; 129 WeightsParameter.Value.Reset += WeightsValue_Reset;146 WeightsParameter.Value.Reset += (_, __) => SyncValuesToWeights(); 130 147 ValuesParameter.ValueChanged += ValuesParameter_ValueChanged; 131 ValuesParameter.Value.Reset += ValuesValue_Reset;148 ValuesParameter.Value.Reset += (_, __) => SyncWeightsToValues(); 132 149 } 133 150 134 151 #region Events 135 protected override void OnEncodingChanged() {136 base.OnEncodingChanged();137 Parameterize();138 }139 //TODO check with abeham if this is really necessary140 //protected override void OnSolutionCreatorChanged() {141 // base.OnSolutionCreatorChanged();142 // Parameterize();143 //}144 protected override void OnEvaluatorChanged() {145 base.OnEvaluatorChanged();146 Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;147 Parameterize();148 }149 152 protected override void DimensionOnChanged() { 150 153 base.DimensionOnChanged(); … … 155 158 ((IStringConvertibleArray)ValuesParameter.Value).Length = Dimension; 156 159 } 157 Parameterize();158 }159 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {160 Parameterize();161 }162 private void KnapsackCapacityParameter_ValueChanged(object sender, EventArgs e) {163 Parameterize();164 160 } 165 161 private void WeightsParameter_ValueChanged(object sender, EventArgs e) { 166 Parameterize(); 167 WeightsParameter.Value.Reset += WeightsValue_Reset; 168 } 169 private void WeightsValue_Reset(object sender, EventArgs e) { 162 WeightsParameter.Value.Reset += (_, __) => SyncValuesToWeights(); 163 SyncValuesToWeights(); 164 } 165 private void ValuesParameter_ValueChanged(object sender, EventArgs e) { 166 ValuesParameter.Value.Reset += (_, __) => SyncWeightsToValues(); 167 SyncWeightsToValues(); 168 } 169 private void SyncWeightsToValues() { 170 if (WeightsParameter.Value != null && ValuesParameter.Value != null) { 171 ((IStringConvertibleArray)WeightsParameter.Value).Length = Values.Length; 172 Dimension = Values.Length; 173 } 174 } 175 private void SyncValuesToWeights() { 170 176 if (WeightsParameter.Value != null && ValuesParameter.Value != null) { 171 177 ((IStringConvertibleArray)ValuesParameter.Value).Length = Weights.Length; 172 178 Dimension = Weights.Length; 173 179 } 174 Parameterize();175 }176 private void ValuesParameter_ValueChanged(object sender, EventArgs e) {177 Parameterize();178 ValuesParameter.Value.Reset += ValuesValue_Reset;179 }180 private void ValuesValue_Reset(object sender, EventArgs e) {181 if (WeightsParameter.Value != null && ValuesParameter.Value != null) {182 ((IStringConvertibleArray)WeightsParameter.Value).Length = Values.Length;183 Dimension = Values.Length;184 }185 Parameterize();186 180 } 187 181 #endregion … … 189 183 #region Helpers 190 184 private void InitializeOperators() { 191 Operators.Add(new KnapsackImprovementOperator()); 192 Operators.Add(new KnapsackPathRelinker()); 193 Operators.Add(new KnapsackSimultaneousPathRelinker()); 194 Operators.Add(new QualitySimilarityCalculator()); 195 Operators.Add(new NoSimilarityCalculator()); 196 197 Operators.Add(new BestKnapsackSolutionAnalyzer()); 185 Operators.AddRange(new IItem[] { new KnapsackImprovementOperator(), 186 new KnapsackPathRelinker(), new KnapsackSimultaneousPathRelinker(), 187 new QualitySimilarityCalculator(), new NoSimilarityCalculator(), 188 new KnapsackOneBitflipMoveEvaluator()}); 198 189 Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>())); 199 190 200 Operators.Add(new KnapsackOneBitflipMoveEvaluator()); 191 ParameterizeOperators(); 192 } 193 194 protected override void ParameterizeOperators() { 195 base.ParameterizeOperators(); 201 196 Parameterize(); 202 197 } 198 203 199 private void Parameterize() { 204 var operators = new List<IItem>();205 206 if (BestKnapsackSolutionAnalyzer != null) {207 operators.Add(BestKnapsackSolutionAnalyzer);208 BestKnapsackSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;209 BestKnapsackSolutionAnalyzer.MaximizationParameter.Hidden = true;210 BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;211 BestKnapsackSolutionAnalyzer.BestKnownQualityParameter.Hidden = true;212 BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;213 BestKnapsackSolutionAnalyzer.BestKnownSolutionParameter.Hidden = true;214 BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name;215 BestKnapsackSolutionAnalyzer.KnapsackCapacityParameter.Hidden = true;216 BestKnapsackSolutionAnalyzer.WeightsParameter.ActualName = WeightsParameter.Name;217 BestKnapsackSolutionAnalyzer.WeightsParameter.Hidden = true;218 BestKnapsackSolutionAnalyzer.ValuesParameter.ActualName = ValuesParameter.Name;219 BestKnapsackSolutionAnalyzer.ValuesParameter.Hidden = true;220 }221 200 foreach (var op in Operators.OfType<IKnapsackMoveEvaluator>()) { 222 operators.Add(op);223 201 op.KnapsackCapacityParameter.ActualName = KnapsackCapacityParameter.Name; 224 202 op.KnapsackCapacityParameter.Hidden = true; … … 227 205 op.ValuesParameter.ActualName = ValuesParameter.Name; 228 206 op.ValuesParameter.Hidden = true; 229 230 var bitflipMoveEval = op as IKnapsackOneBitflipMoveEvaluator; 231 if (bitflipMoveEval != null) { 232 foreach (var moveOp in Encoding.Operators.OfType<IOneBitflipMoveQualityOperator>()) { 233 moveOp.MoveQualityParameter.ActualName = bitflipMoveEval.MoveQualityParameter.ActualName; 234 moveOp.MoveQualityParameter.Hidden = true; 235 } 236 } 237 } 238 foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) { 239 operators.Add(op); 240 op.SolutionParameter.ActualName = Encoding.Name; 241 op.SolutionParameter.Hidden = true; 242 } 243 foreach (var op in Operators.OfType<ISingleObjectivePathRelinker>()) { 244 operators.Add(op); 245 op.ParentsParameter.ActualName = Encoding.Name; 246 op.ParentsParameter.Hidden = true; 247 } 248 foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) { 249 operators.Add(op); 250 op.SolutionVariableName = Encoding.Name; 251 op.QualityVariableName = Evaluator.QualityParameter.ActualName; 252 } 253 254 if (operators.Count > 0) Encoding.ConfigureOperators(Operators); 207 } 255 208 } 256 209 #endregion
Note: See TracChangeset
for help on using the changeset viewer.