Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/05/10 03:30:34 (15 years ago)
Author:
abeham
Message:

Changed SA to use Analyzer #999

Location:
trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealing.cs

    r3616 r3622  
    3333using HeuristicLab.PluginInfrastructure;
    3434using HeuristicLab.Random;
     35using HeuristicLab.Analysis;
    3536
    3637namespace HeuristicLab.Algorithms.SimulatedAnnealing {
     
    8081      get { return (ValueParameter<DoubleValue>)Parameters["EndTemperature"]; }
    8182    }
     83    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
     84      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
     85    }
    8286    #endregion
    8387
     
    119123      set { EndTemperatureParameter.Value = value; }
    120124    }
     125    public MultiAnalyzer Analyzer {
     126      get { return AnalyzerParameter.Value; }
     127      set { AnalyzerParameter.Value = value; }
     128    }
    121129    private RandomCreator RandomCreator {
    122130      get { return (RandomCreator)OperatorGraph.InitialOperator; }
     
    133141      get { return annealingOperators; }
    134142    }
     143    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
    135144    #endregion
    136145
     146    [StorableConstructor]
     147    private SimulatedAnnealing(bool deserializing) : base(deserializing) { }
    137148    public SimulatedAnnealing()
    138149      : base() {
     
    147158      Parameters.Add(new ValueParameter<DoubleValue>("StartTemperature", "The initial temperature.", new DoubleValue(100)));
    148159      Parameters.Add(new ValueParameter<DoubleValue>("EndTemperature", "The final temperature which should be reached when iterations reaches maximum iterations.", new DoubleValue(1e-6)));
    149 
     160      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
     161     
    150162      RandomCreator randomCreator = new RandomCreator();
    151163      SolutionsCreator solutionsCreator = new SolutionsCreator();
     
    172184      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
    173185      mainLoop.ResultsParameter.ActualName = "Results";
     186      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
    174187
    175188      Initialize();
    176189    }
    177     [StorableConstructor]
    178     private SimulatedAnnealing(bool deserializing) : base(deserializing) { }
    179190
    180191    public override IDeepCloneable Clone(Cloner cloner) {
     
    204215      ParameterizeMoveMakers();
    205216      ParameterizeMoveGenerators();
     217      ParameterizeAnalyzers();
     218      UpdateAnalyzers();
    206219      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    207220      base.OnProblemChanged();
     
    218231      ParameterizeMoveEvaluators();
    219232      ParameterizeMoveMakers();
     233      ParameterizeAnalyzers();
    220234      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    221235      base.Problem_EvaluatorChanged(sender, e);
     
    231245      UpdateMoveGenerator();
    232246      UpdateMoveParameters();
     247      UpdateAnalyzers();
    233248      ParameterizeMainLoop();
    234249      ParameterizeMoveEvaluators();
     
    241256      ParameterizeMoveEvaluators();
    242257      ParameterizeMoveMakers();
     258      ParameterizeAnalyzers();
    243259    }
    244260    private void MoveGeneratorParameter_ValueChanged(object sender, EventArgs e) {
     
    260276    [StorableHook(HookType.AfterDeserialization)]
    261277    private void Initialize() {
     278      InitializeAnalyzers();
     279      UpdateAnalyzers();
    262280      if (Problem != null) {
    263281        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     
    278296        AnnealingOperatorParameter.ValidValues.Add(op);
    279297    }
     298    private void InitializeAnalyzers() {
     299      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
     300      ParameterizeAnalyzers();
     301    }
    280302    private void UpdateMoveGenerator() {
    281303      IMultiMoveGenerator oldMoveGenerator = MoveGenerator;
     
    291313      if (MoveGenerator == null) {
    292314        ClearMoveParameters();
     315      }
     316    }
     317    private void ParameterizeAnalyzers() {
     318      qualityAnalyzer.ResultsParameter.ActualName = "Results";
     319      if (Problem != null) {
     320        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     321        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     322        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
    293323      }
    294324    }
     
    367397      }
    368398    }
     399    private void UpdateAnalyzers() {
     400      Analyzer.Operators.Clear();
     401      Analyzer.Operators.Add(qualityAnalyzer);
     402      if (Problem != null) {
     403        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name))
     404          Analyzer.Operators.Add(analyzer);
     405      }
     406    }
    369407    #endregion
    370408  }
  • trunk/sources/HeuristicLab.Algorithms.SimulatedAnnealing/3.3/SimulatedAnnealingMainLoop.cs

    r3522 r3622  
    7777      get { return (ValueLookupParameter<IOperator>)Parameters["AnnealingOperator"]; }
    7878    }
    79     public ValueLookupParameter<IOperator> VisualizerParameter {
    80       get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
    81     }
    82     public LookupParameter<IItem> VisualizationParameter {
    83       get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
     79    public ValueLookupParameter<IOperator> AnalyzerParameter {
     80      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
    8481    }
    8582    public ValueLookupParameter<VariableCollection> ResultsParameter {
     
    112109      Parameters.Add(new ValueLookupParameter<IOperator>("AnnealingOperator", "The operator that modifies the temperature."));
    113110
    114       Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
    115       Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
     111      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
    116112      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
    117113      #endregion
     
    119115      #region Create operators
    120116      VariableCreator variableCreator = new VariableCreator();
    121       BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
    122       BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
    123       QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
    124       Placeholder visualizer1 = new Placeholder();
     117      ResultsCollector resultsCollector1 = new ResultsCollector();
     118      Placeholder analyzer1 = new Placeholder();
    125119      SubScopesProcessor sssp = new SubScopesProcessor();
    126120      ResultsCollector resultsCollector = new ResultsCollector();
    127121      Placeholder annealingOperator = new Placeholder();
    128122      UniformSubScopesProcessor mainProcessor = new UniformSubScopesProcessor();
    129       DataTableValuesCollector valuesCollector = new DataTableValuesCollector();
    130123      Placeholder moveGenerator = new Placeholder();
    131124      UniformSubScopesProcessor moveEvaluationProcessor = new UniformSubScopesProcessor();
     
    137130      IntCounter iterationsCounter = new IntCounter();
    138131      Comparator iterationsComparator = new Comparator();
    139       BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
    140       BestQualityMemorizer bestQualityMemorizer4 = new BestQualityMemorizer();
    141       QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
    142       Placeholder visualizer2 = new Placeholder();
     132      ResultsCollector resultsCollector2 = new ResultsCollector();
     133      Placeholder analyzer2 = new Placeholder();
    143134      ConditionalBranch iterationsTermination = new ConditionalBranch();
    144135     
    145136      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
    146       variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
    147137      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("Temperature", new DoubleValue(double.MaxValue)));
    148138
    149       bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
    150       bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
    151       bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
    152 
    153       bestQualityMemorizer2.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
    154       bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
    155       bestQualityMemorizer2.QualityParameter.ActualName = QualityParameter.Name;
    156 
    157       qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
    158       qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
    159       qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
    160       qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
    161 
    162       visualizer1.Name = "Visualizer (placeholder)";
    163       visualizer1.OperatorParameter.ActualName = VisualizerParameter.Name;
    164 
    165       resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
    166       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(QualityParameter.Name));
    167       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality") { ActualName = "BestQuality" });
    168       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
    169       resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
    170       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
    171       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
    172       resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
    173       resultsCollector.CollectedValues.Add(new LookupParameter<IItem>("Solution Visualization", null, VisualizationParameter.Name));
    174      
     139      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
     140      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
     141      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
     142
     143      analyzer1.Name = "Analyzer (placeholder)";
     144      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
     145
    175146      annealingOperator.Name = "Annealing operator (placeholder)";
    176147      annealingOperator.OperatorParameter.ActualName = AnnealingOperatorParameter.Name;
    177 
    178       valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Quality", null, QualityParameter.Name));
    179       valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
    180       valuesCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
    181       valuesCollector.DataTableParameter.ActualName = "Qualities";
    182148
    183149      moveGenerator.Name = "Move generator (placeholder)";
     
    209175      iterationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
    210176
    211       bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
    212       bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
    213       bestQualityMemorizer3.QualityParameter.ActualName = QualityParameter.Name;
    214 
    215       bestQualityMemorizer4.BestQualityParameter.ActualName = BestKnownQualityParameter.Name;
    216       bestQualityMemorizer4.MaximizationParameter.ActualName = MaximizationParameter.Name;
    217       bestQualityMemorizer4.QualityParameter.ActualName = QualityParameter.Name;
    218 
    219       qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
    220       qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
    221       qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
    222       qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
    223 
    224       visualizer2.Name = "Visualizer (placeholder)";
    225       visualizer2.OperatorParameter.ActualName = VisualizerParameter.Name;
     177      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
     178      resultsCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Temperature"));
     179      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
     180
     181      analyzer2.Name = "Analyzer (placeholder)";
     182      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
    226183
    227184      iterationsTermination.Name = "Iterations termination condition";
     
    231188      #region Create operator graph
    232189      OperatorGraph.InitialOperator = variableCreator;
    233       variableCreator.Successor = bestQualityMemorizer1;
    234       bestQualityMemorizer1.Successor = bestQualityMemorizer2;
    235       bestQualityMemorizer2.Successor = qualityDifferenceCalculator1;
    236       qualityDifferenceCalculator1.Successor = visualizer1;
    237       visualizer1.Successor = sssp;
     190      variableCreator.Successor = resultsCollector1;
     191      resultsCollector1.Successor = analyzer1;
     192      analyzer1.Successor = sssp;
    238193      sssp.Operators.Add(resultsCollector);
    239194      resultsCollector.Successor = null;
    240195      sssp.Successor = annealingOperator;
    241196      annealingOperator.Successor = mainProcessor;
    242       mainProcessor.Operator = valuesCollector;
     197      mainProcessor.Operator = moveGenerator;
    243198      mainProcessor.Successor = iterationsCounter;
    244       valuesCollector.Successor = moveGenerator;
    245199      moveGenerator.Successor = moveEvaluationProcessor;
    246200      moveEvaluationProcessor.Operator = moveEvaluator;
     
    250204      improvesQualityBranch.TrueBranch = moveMaker;
    251205      iterationsCounter.Successor = iterationsComparator;
    252       iterationsComparator.Successor = bestQualityMemorizer3;
    253       bestQualityMemorizer3.Successor = bestQualityMemorizer4;
    254       bestQualityMemorizer4.Successor = qualityDifferenceCalculator2;
    255       qualityDifferenceCalculator2.Successor = visualizer2;
    256       visualizer2.Successor = iterationsTermination;
     206      iterationsComparator.Successor = resultsCollector2;
     207      resultsCollector2.Successor = analyzer2;
     208      analyzer2.Successor = iterationsTermination;
    257209      iterationsTermination.TrueBranch = null;
    258210      iterationsTermination.FalseBranch = annealingOperator;
Note: See TracChangeset for help on using the changeset viewer.