Changeset 13728


Ignore:
Timestamp:
03/24/16 14:05:07 (6 years ago)
Author:
bburlacu
Message:

#2288: Added VariableInteractionNetwork graph class. Small improvements to the impacts analyzer. Add license header and improve formatting in CreateTargetVariationExperiment.cs

Location:
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperiment.cs

    r12320 r13728  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2016 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
     22using System;
    223using System.Collections.Generic;
    324using System.Windows.Forms;
     
    627using HeuristicLab.Optimizer;
    728
    8 namespace HeuristicLab.VariableInteractionNetworks
    9 {
    10     internal class CreateTargetVariationExperiment : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider
    11     {
    12         public override string Name
    13         {
    14             get { return "Create Target Variation &Experiment"; }
     29namespace HeuristicLab.VariableInteractionNetworks {
     30  internal class CreateTargetVariationExperiment : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
     31    public override string Name {
     32      get { return "Create Target Variation &Experiment"; }
     33    }
     34    public override IEnumerable<string> Structure {
     35      get { return new string[] { "&Edit" }; }
     36    }
     37    public override int Position {
     38      get { return 2300; }
     39    }
     40    public override string ToolTipText {
     41      get { return "Create an experiment for varying the target variable"; }
     42    }
     43
     44    protected override void OnToolStripItemSet(EventArgs e) {
     45      ToolStripItem.Enabled = false;
     46    }
     47    protected override void OnActiveViewChanged(object sender, EventArgs e) {
     48      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     49      ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked;
     50    }
     51
     52    public override void Execute() {
     53      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     54      if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
     55        using (CreateTargetVariationExperimentDialog dialog = new CreateTargetVariationExperimentDialog((IAlgorithm)activeView.Content)) {
     56          if (dialog.ShowDialog() == DialogResult.OK) MainFormManager.MainForm.ShowContent(dialog.Experiment);
    1557        }
    16         public override IEnumerable<string> Structure
    17         {
    18             get { return new string[] { "&Edit" }; }
    19         }
    20         public override int Position
    21         {
    22             get { return 2300; }
    23         }
    24         public override string ToolTipText
    25         {
    26             get { return "Create an experiment for varying the target variable"; }
    27         }
    28 
    29         protected override void OnToolStripItemSet(EventArgs e)
    30         {
    31             ToolStripItem.Enabled = false;
    32         }
    33         protected override void OnActiveViewChanged(object sender, EventArgs e)
    34         {
    35             IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    36             ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked;
    37         }
    38 
    39         public override void Execute()
    40         {
    41             IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    42             if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked)
    43             {
    44                 using (CreateTargetVariationExperimentDialog dialog = new CreateTargetVariationExperimentDialog((IAlgorithm)activeView.Content))
    45                 {
    46                     if (dialog.ShowDialog() == DialogResult.OK) MainFormManager.MainForm.ShowContent(dialog.Experiment);
    47                 }
    48             }
    49         }
     58      }
    5059    }
     60  }
    5161}
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/HeuristicLab.VariableInteractionNetworks-3.3.csproj

    r12460 r13728  
    166166    <Compile Include="Properties\AssemblyInfo.cs" />
    167167    <Compile Include="SymbolicDataAnalysisVariableImpactsAnalyzer.cs" />
     168    <Compile Include="VariableInteractionNetwork.cs" />
    168169  </ItemGroup>
    169170  <ItemGroup>
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs

    r13665 r13728  
    3838  [StorableClass]
    3939  public class SymbolicRegressionVariableImpactsAnalyzer : SymbolicDataAnalysisAnalyzer {
     40    #region parameter names
    4041    private const string UpdateCounterParameterName = "UpdateCounter";
    4142    private const string UpdateIntervalParameterName = "UpdateInterval";
     
    4748    private const string EstimationLimitsParameterName = "EstimationLimits";
    4849    private const string EvaluatorParameterName = "Evaluator";
    49     private const string VariableImpactsParameterName = "AverageVariableImpacts";
    5050    private const string PercentageBestParameterName = "PercentageBest";
    5151    private const string LastGenerationsParameterName = "LastGenerations";
     
    5353    private const string OptimizeConstantsParameterName = "OptimizeConstants";
    5454    private const string PruneTreesParameterName = "PruneTrees";
     55    private const string AverageVariableImpactsResultName = "Average variable impacts";
     56    private const string AverageVariableImpactsHistoryResultName = "Average variable impacts history";
     57    #endregion
    5558
    5659    private SymbolicDataAnalysisExpressionTreeSimplifier simplifier;
     
    8487    public ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator> EvaluatorParameter {
    8588      get { return (ILookupParameter<ISymbolicRegressionSingleObjectiveEvaluator>)Parameters[EvaluatorParameterName]; }
    86     }
    87     public ILookupParameter<DataTable> VariableImpactsParameter {
    88       get { return (ILookupParameter<DataTable>)Parameters[VariableImpactsParameterName]; }
    8989    }
    9090    public IFixedValueParameter<PercentValue> PercentageBestParameter {
     
    126126      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
    127127      Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName, new IntValue(3)));
    128       Parameters.Add(new LookupParameter<DataTable>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run."));
    129       Parameters.Add(new FixedValueParameter<PercentValue>(PercentageBestParameterName, new PercentValue(100)));
     128      Parameters.Add(new FixedValueParameter<PercentValue>(PercentageBestParameterName, new PercentValue(1)));
    130129      Parameters.Add(new FixedValueParameter<IntValue>(LastGenerationsParameterName, new IntValue(10)));
    131130      Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, new BoolValue(false)));
     
    171170      var results = ResultCollectionParameter.ActualValue;
    172171      int maxGen = MaximumGenerationsParameter.ActualValue.Value;
    173       int gen = ((IntValue)results["Generations"].Value).Value;
     172      int gen = results.ContainsKey("Generations") ? ((IntValue)results["Generations"].Value).Value : 0;
    174173      int lastGen = LastGenerationsParameter.Value.Value;
    175174
     
    195194      var allowedInputVariables = problemData.AllowedInputVariables.ToList();
    196195      DataTable dataTable;
    197       if (VariableImpactsParameter.ActualValue == null) {
     196      if (!results.ContainsKey(AverageVariableImpactsHistoryResultName)) {
    198197        dataTable = new DataTable("Variable impacts", "Average impact of variables over the population");
    199198        dataTable.VisualProperties.XAxisTitle = "Generation";
    200199        dataTable.VisualProperties.YAxisTitle = "Average variable impact";
    201         VariableImpactsParameter.ActualValue = dataTable;
    202         results.Add(new Result("Average variable impacts", "The relative variable relevance calculated as the average relative variable frequency over the whole run.", new DataTable()));
     200        results.Add(new Result(AverageVariableImpactsHistoryResultName, dataTable));
    203201
    204202        foreach (var v in allowedInputVariables) {
    205203          dataTable.Rows.Add(new DataRow(v) { VisualProperties = { StartIndexZero = true } });
    206204        }
    207         VariableImpactsParameter.ActualValue = dataTable;
    208       }
    209       dataTable = VariableImpactsParameter.ActualValue;
     205      }
     206      dataTable = (DataTable)results[AverageVariableImpactsHistoryResultName].Value;
     207
    210208      int nTrees = (int)Math.Round(trees.Length * percentageBest);
    211209      var bestTrees = trees.Take(nTrees).Select(x => (ISymbolicExpressionTree)x.Clone()).ToList();
     
    215213      if (optimizeConstants) {
    216214        for (int i = 0; i < simplifiedTrees.Count; ++i) {
    217           qualities[i].Value = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTrees[i], problemData, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, estimationLimits.Upper, estimationLimits.Lower);
     215          qualities[i].Value = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTrees[i], problemData, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, true, estimationLimits.Upper, estimationLimits.Lower);
    218216        }
    219217      }
     
    243241        var averageImpact = 0d;
    244242        for (int i = 0; i < indices.Count; ++i) {
     243          var tree = simplifiedTrees[i];
    245244          var originalQuality = qualities[i].Value;
    246245          double newQuality;
    247246          if (optimizeConstants) {
    248             newQuality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTrees[i], pd, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, estimationLimits.Upper, estimationLimits.Lower);
     247            newQuality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, pd, problemData.TrainingIndices, applyLinearScaling, constantOptimizationIterations, true, estimationLimits.Upper, estimationLimits.Lower);
    249248          } else {
    250249            var evaluator = EvaluatorParameter.ActualValue;
    251             newQuality = evaluator.Evaluate(this.ExecutionContext, simplifiedTrees[i], pd, pd.TrainingIndices);
     250            newQuality = evaluator.Evaluate(this.ExecutionContext, tree, pd, pd.TrainingIndices);
    252251          }
    253252          averageImpact += originalQuality - newQuality; // impact calculated this way may be negative
     
    257256      }
    258257
    259       results["Average variable impacts"].Value = dataTable;
     258      var averageVariableImpacts = new DoubleMatrix(dataTable.Rows.Count, 1);
     259      var rowNames = dataTable.Rows.Select(x => x.Name).ToList();
     260      averageVariableImpacts.RowNames = rowNames;
     261      for (int i = 0; i < rowNames.Count; ++i) {
     262        averageVariableImpacts[i, 0] = dataTable.Rows[rowNames[i]].Values.Last();
     263      }
     264      if (!results.ContainsKey(AverageVariableImpactsResultName)) {
     265        results.Add(new Result(AverageVariableImpactsResultName, averageVariableImpacts));
     266      } else {
     267        results[AverageVariableImpactsResultName].Value = averageVariableImpacts;
     268      }
    260269      return base.Apply();
    261270    }
Note: See TracChangeset for help on using the changeset viewer.