Changeset 12568
- Timestamp:
- 07/01/15 21:06:50 (9 years ago)
- Location:
- branches/HeuristicLab.VariableInteractionNetworks
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/VariableInteractionNetworkView.cs
r12321 r12568 87 87 { 88 88 // TODO: Add code when content has been changed and is not null 89 viewHost2.Content = CalculateNodeImportance(CalculateAdjacencyMatrix()); 89 viewHost2.Content = CalculateNodeImportance(CalculateAdjacencyMatrix()); 90 90 var adjMatrix = CalculateAdjacencyMatrix(); 91 91 viewHost1.Content = adjMatrix; … … 110 110 { 111 111 var runCollection = Content; 112 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 112 var inputVariables = ((IRegressionProblemData)runCollection.First().Parameters["ProblemData"]).InputVariables.Select(x => x.Value).ToList(); 113 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).OrderBy(x => inputVariables.IndexOf(x.Key)).ToList(); 113 114 114 115 var allVariableImpacts = runCollection.Select(run => (DoubleMatrix)run.Results[variableImpactResultName]); … … 116 117 from variableName in variableImpact.RowNames 117 118 select variableName).Distinct().ToArray(); 119 120 var vars = new List<Tuple<int, string>>(); 121 122 var allowedInputs = ((IRegressionProblemData)groupRunCollection[0].First().Parameters["ProblemData"]).AllowedInputVariables.ToList(); 123 118 124 var adjMatrix = new DoubleMatrix(variableNames.Length, variableNames.Length); 119 125 120 adjMatrix.RowNames = groupRunCollection.Select(x => x.Key); 126 for (int i = 0; i < adjMatrix.Rows; ++i) 127 { 128 int inputIndex = allowedInputs.FindIndex(x => x == variableNames[i]); 129 vars.Add(new Tuple<int, string>(inputIndex, variableNames[i])); 130 } 131 132 vars.Sort((a, b) => a.Item1.CompareTo(b.Item1)); 133 134 for (int i = 0; i < adjMatrix.Rows; ++i) 135 { 136 variableNames[i] = vars[i].Item2; 137 } 138 adjMatrix.RowNames = variableNames; 121 139 adjMatrix.ColumnNames = adjMatrix.RowNames; 140 122 141 for (int j = 0; j < groupRunCollection.Count; ++j) 123 142 { 124 143 var g = groupRunCollection[j]; 144 125 145 var matrix = CalculateAdjacencyRows(g); 126 var variables = new List<Tuple<string, double>>(); 127 var columnNames = matrix.ColumnNames.ToList(); 146 var variables = new List<double>(); 128 147 129 148 for (int i = 0; i < matrix.Columns; ++i) 130 149 { 131 variables.Add(new Tuple<string, double>(columnNames[i], matrix[0, i])); 132 } 133 variables.Add(new Tuple<string, double>(g.Key, 0)); 134 variables.Sort((a, b) => a.Item1.CompareTo(b.Item1)); 150 variables.Add(matrix[0, i]); 151 } 152 135 153 for (int i = 0; i < variables.Count; ++i) 136 154 { 137 adjMatrix[j, i] = variables[i].Item2; 138 155 if (i == j) 156 { 157 adjMatrix[i, i] = 0; 158 variables.Insert(i, 0); 159 } 160 else 161 adjMatrix[j, i] = variables[i]; 139 162 } 140 163 } … … 144 167 private DoubleMatrix CalculateAdjacencyRows(IEnumerable<IRun> runs) 145 168 { 146 IEnumerable<DoubleMatrix> allVariableImpacts = (from run in runs147 select run.Results[variableImpactResultName]).Cast<DoubleMatrix>();148 var variableNames = (from variableImpact in allVariableImpacts149 from variableName in variableImpact.RowNames150 select variableName).Distinct().ToArray();151 152 List<string> variableNamesList = (from variableName in variableNames153 where GetVariableImpacts(variableName, allVariableImpacts).Any(x => !x.IsAlmost(0.0))154 select variableName)155 .ToList();156 157 169 var runNames = runs.Select(x => x.Name).ToArray(); 158 170 var runsArray = runs.ToArray(); 159 171 DoubleMatrix varImpactMatrix = CalculateVariableImpactMatrix(runsArray, runNames); 160 var targetMatrix = new DoubleMatrix(1, variableNames.Length); 172 173 var targetMatrix = new DoubleMatrix(1, varImpactMatrix.Rows); 161 174 162 175 for (int i = 0; i < varImpactMatrix.Rows; ++i) … … 165 178 } 166 179 167 targetMatrix.RowNames = new[] { "Target" }; 168 targetMatrix.ColumnNames = variableNames; 169 180 targetMatrix.RowNames = new[] { "Impacts" }; 181 targetMatrix.ColumnNames = varImpactMatrix.RowNames; 170 182 return targetMatrix; 171 183 } … … 262 274 } 263 275 264 // adapted from RunCollectionVariableImpactView276 //modified from RunCollectionVariableImpactView 265 277 private DoubleMatrix CalculateVariableImpactMatrix(IRun[] runs, string[] runNames) 266 278 { … … 307 319 } 308 320 } 309 return matrix; 321 322 // sort by median 323 var sortedMatrix = (DoubleMatrix)matrix.Clone(); 324 var sortedIndexes = from i in Enumerable.Range(0, sortedMatrix.Rows) 325 orderby matrix[i, numberOfRuns] 326 select i; 327 328 int targetIndex = 0; 329 foreach (var sourceIndex in sortedIndexes) 330 { 331 for (int c = 0; c < matrix.Columns; c++) 332 sortedMatrix[targetIndex, c] = matrix[sourceIndex, c]; 333 targetIndex++; 334 } 335 sortedMatrix.RowNames = sortedIndexes.Select(i => variableNamesList[i]); 336 337 var vars = new List<Tuple<int, string, double>>(); 338 var rowNames = sortedMatrix.RowNames.ToList(); 339 340 var groupRunCollection = Content.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList(); 341 var inputs = ((IRegressionProblemData)groupRunCollection[0].First().Parameters["ProblemData"]).InputVariables.ToList(); 342 List<string> inp = (from input in inputs 343 select (input.ToString())).ToList(); 344 345 for (int i = 0; i < sortedMatrix.Rows; ++i) 346 { 347 int inputIndex = inp.FindIndex(x => x == rowNames[i]); 348 vars.Add(new Tuple<int, string, double>(inputIndex, rowNames[i], sortedMatrix[i, runNames.Length])); 349 } 350 351 vars.Sort((a, b) => a.Item1.CompareTo(b.Item1)); 352 353 for (int i = 0; i < sortedMatrix.Rows; ++i) 354 { 355 sortedMatrix[i, runNames.Length] = vars[i].Item3; 356 rowNames[i] = vars[i].Item2; 357 } 358 sortedMatrix.RowNames = rowNames; 359 360 return sortedMatrix; 310 361 } 311 362 -
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/Plugin.cs.frame
r12462 r12568 47 47 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic", "3.4")] 48 48 [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")] 49 [PluginDependency("HeuristicLab.Problems.Instances", "3. 4")]49 [PluginDependency("HeuristicLab.Problems.Instances", "3.3")] 50 50 [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 51 51 public class Plugin : PluginBase { -
branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/SymbolicDataAnalysisVariableImpactsAnalyzer.cs
r12460 r12568 11 11 using HeuristicLab.Problems.DataAnalysis.Symbolic; 12 12 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 13 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 13 14 14 15 namespace HeuristicLab.VariableInteractionNetworks … … 33 34 private const string LastGenerationsParameterName = "LastGenerations"; 34 35 private const string MaximumGenerationsParameterName = "MaximumGenerations"; 35 private const string OptimizedConstantsParameterName = "OptimizedConstants"; 36 37 private readonly SymbolicDataAnalysisExpressionTreeSimplifier simplifier; 38 private readonly SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator; 36 private const string OptimizeConstantsParameterName = "OptimizeConstants"; 37 private const string PruneTreesParameterName = "PruneTrees"; 38 39 private SymbolicDataAnalysisExpressionTreeSimplifier simplifier; 40 private SymbolicRegressionSolutionImpactValuesCalculator impactsCalculator; 39 41 40 42 #region parameters … … 87 89 get { return (IFixedValueParameter<IntValue>)Parameters[LastGenerationsParameterName]; } 88 90 } 89 public IFixedValueParameter<BoolValue> OptimizedParameter 90 { 91 get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizedConstantsParameterName]; } 91 public IFixedValueParameter<BoolValue> OptimizeConstantsParameter 92 { 93 get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; } 94 } 95 public IFixedValueParameter<BoolValue> PruneTreesParameter 96 { 97 get { return (IFixedValueParameter<BoolValue>)Parameters[PruneTreesParameterName]; } 92 98 } 93 99 private ILookupParameter<IntValue> MaximumGenerationsParameter … … 119 125 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName)); 120 126 Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName)); 121 Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName)); 127 Parameters.Add(new FixedValueParameter<IntValue>(MaxCOIterationsParameterName)); 122 128 Parameters.Add(new LookupParameter<DataTable>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run.")); 123 129 Parameters.Add(new FixedValueParameter<PercentValue>(PercentageBestParameterName)); 124 130 Parameters.Add(new FixedValueParameter<IntValue>(LastGenerationsParameterName)); 125 Parameters.Add(new FixedValueParameter<BoolValue>(OptimizedConstantsParameterName)); 131 Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, new BoolValue(false))); 132 Parameters.Add(new FixedValueParameter<BoolValue>(PruneTreesParameterName, new BoolValue(false))); 126 133 Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName, "The maximum number of generations which should be processed.")); 127 134 135 impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 128 136 simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 129 impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator();130 137 } 131 138 … … 133 140 protected SymbolicRegressionVariableImpactsAnalyzer(bool deserializing) : base(deserializing) { } 134 141 142 [StorableHook(HookType.AfterDeserialization)] 143 private void AfterDeserialization() 144 { 145 impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 146 simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 147 } 148 135 149 protected SymbolicRegressionVariableImpactsAnalyzer(SymbolicRegressionVariableImpactsAnalyzer original, Cloner cloner) 136 150 : base(original, cloner) 137 151 { 152 impactsCalculator = new SymbolicRegressionSolutionImpactValuesCalculator(); 153 simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier(); 138 154 } 139 155 … … 158 174 int lastGen = LastGenerationsParameter.Value.Value; 159 175 160 if (lastGen > maxGen) 161 lastGen = maxGen; 162 if (maxGen - gen < lastGen) 176 if (gen < maxGen - lastGen) 163 177 return base.Apply(); 164 178 … … 176 190 var estimationLimits = EstimationLimitsParameter.ActualValue; // lookup parameter => ActualValue 177 191 var percentageBest = PercentageBestParameter.Value.Value; 178 var optimizedConstants = OptimizedParameter.Value.Value; 192 var optimizeConstants = OptimizeConstantsParameter.Value.Value; 193 var pruneTrees = PruneTreesParameter.Value.Value; 179 194 180 195 var allowedInputVariables = problemData.AllowedInputVariables.ToList(); … … 188 203 VariableImpactsParameter.ActualValue = datatable; 189 204 results.Add(new Result("Average variable impacts", "The relative variable relevance calculated as the average relative variable frequency over the whole run.", new DataTable())); 190 205 191 206 foreach (var v in allowedInputVariables) 192 207 { … … 197 212 datatable = VariableImpactsParameter.ActualValue; 198 213 int nTrees = (int)Math.Round(trees.Length * percentageBest); 199 200 // simplify trees 201 var simplifiedTrees = trees.Take(nTrees).Select(x => simplifier.Simplify(x)); 214 var bestTrees = trees.Take(nTrees).Select(x => (ISymbolicExpressionTree)x.Clone()).ToList(); 215 216 if (optimizeConstants) 217 { 218 foreach (var tree in bestTrees) 219 SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, maxCOIterations, estimationLimits.Upper, estimationLimits.Lower); 220 } 221 var simplifiedTrees = bestTrees.Select(x => simplifier.Simplify(x)).ToList(); 202 222 var variableCounts = problemData.AllowedInputVariables.ToDictionary(x => x, x => simplifiedTrees.Count(t => t.IterateNodesPrefix().Any(n => n is VariableTreeNode && ((VariableTreeNode)n).VariableName == x))); 203 for each (var simplifiedTree in simplifiedTrees)204 { 205 if (optimizedConstants == true)206 { 207 SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, simplifiedTree, problemData, problemData.TrainingIndices, applyLinearScaling, maxCOIterations, estimationLimits.Upper, estimationLimits.Lower);208 }223 for (int i = 0; i < simplifiedTrees.Count; ++i) 224 { 225 var simplifiedTree = simplifiedTrees[i]; 226 227 if (pruneTrees) 228 simplifiedTree = SymbolicRegressionPruningOperator.Prune(simplifiedTree, impactsCalculator, interpreter, problemData, estimationLimits, problemData.TrainingIndices); 209 229 210 230 var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, simplifiedTree, estimationLimits.Lower, estimationLimits.Upper, problemData, problemData.TrainingIndices, applyLinearScaling); … … 216 236 { 217 237 var avgImpact = g.Average(x => impactsCalculator.CalculateImpactValue(model, x, problemData, problemData.TrainingIndices, quality)); 238 if (double.IsNaN(avgImpact)) 239 avgImpact = 0; 218 240 variableImpacts[g.Key] += avgImpact; 219 241 } … … 222 244 foreach (var pair in variableImpacts) 223 245 { 224 datatable.Rows[pair.Key].Values.Add(pair.Value / variableCounts[pair.Key]); 246 var v = variableCounts[pair.Key] > 0 ? pair.Value / variableCounts[pair.Key] : 0; 247 datatable.Rows[pair.Key].Values.Add(v); 225 248 } 226 249 results["Average variable impacts"].Value = datatable;
Note: See TracChangeset
for help on using the changeset viewer.