Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16497


Ignore:
Timestamp:
01/04/19 12:43:26 (6 years ago)
Author:
jzenisek
Message:

#2288:

  • added possibility to create simple networks (only one input var set per target var, i.e. without junction nodes)
  • fixed minor enumeration bug
  • enabled network view updates
Location:
branches/2288_HeuristicLab.VariableInteractionNetworks
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.cs

    r16295 r16497  
    150150
    151151          var count = group.Count();
    152           foreach (var v in averageImpacts.Keys) {
     152          foreach (var v in averageImpacts.Keys.ToList()) {
    153153            averageImpacts[v] /= count;
    154154          }
     
    327327    }
    328328
     329    public void UpdateNetwork(VariableInteractionNetwork network) {
     330      if (InvokeRequired) {
     331        Invoke((Action<VariableInteractionNetwork>)UpdateNetwork, network);
     332      } else {
     333        graphChart.Graph = network;
     334      }     
     335    }
     336
    329337    #region events
    330338    protected override void OnContentChanged() {
  • branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/VariableInteractionNetwork.cs

    r14655 r16497  
    3535  [StorableClass]
    3636  public class VariableInteractionNetwork : DirectedGraph {
     37
     38    /// <summary>
     39    /// Creates a simple network from a matrix of variable impacts (each row represents a target variable, each column represents an input variable)
     40    /// For each target variable not more than one row can be defined (no junction nodes are build, cf. FromNmseAndVariableImpacts(..) for building more complex networks).
     41    /// The network is acyclic. Values in the diagonal are ignored.
     42    /// The algorithm starts with an empty network and incrementally adds next most relevant input variable for each target variable up to a given threshold
     43    /// In each iteration cycles are broken by removing the weakest link.
     44    /// </summary>
     45    /// <param name="nmse">vector of NMSE values for each target variable</param>
     46    /// <param name="variableImpacts">Variable impacts (smaller is lower impact). Row names and columns names should be set</param>
     47    /// <param name="nmseThreshold">Threshold for NMSE values. Variables with a NMSE value larger than the threshold are considered as independent variables</param>
     48    /// <param name="varImpactThreshold">Threshold for variable impact values. Impacts with a value smaller than the threshold are considered as independent</param>
     49    /// <returns></returns>
     50    public static VariableInteractionNetwork CreateSimpleNetwork(double[] nmse, DoubleMatrix variableImpacts, double nmseThreshold = 0.2, double varImpactThreshold = 0.0) {
     51      if (variableImpacts.Rows != variableImpacts.Columns) throw new ArgumentException();
     52      var network = new VariableInteractionNetwork();
     53      var targets = new Dictionary<string, double>();
     54      string[] varNames = variableImpacts.RowNames.ToArray();
     55      if (nmse.Length != varNames.Length) throw new ArgumentException();
     56
     57      for (int i = 0; i < varNames.Length; i++) {
     58        var name = varNames[i];
     59        var varVertex = new VariableNetworkNode() {Label = name, Weight = nmse[i]};
     60        network.AddVertex(varVertex);
     61        if (nmse[i] < nmseThreshold) {
     62          targets.Add(name, nmse[i]);
     63        }
     64      }
     65
     66      // rel is updated (impacts which are represented in the network are set to zero)
     67      var rel = variableImpacts.CloneAsMatrix();
     68      // make sure the diagonal is not considered
     69      for (int i = 0; i < rel.GetLength(0); i++) rel[i, i] = double.NegativeInfinity;
     70
     71      var addedArcs = AddArcs(network, rel, varNames, targets, varImpactThreshold);
     72      while (addedArcs.Any()) {
     73        var cycles = network.FindShortestCycles().ToList();
     74        while (cycles.Any()) {
     75          // delete weakest link
     76          var weakestArc = cycles.SelectMany(cycle => network.ArcsForCycle(cycle)).OrderBy(a => a.Weight).First();
     77          network.RemoveArc(weakestArc);
     78
     79          cycles = network.FindShortestCycles().ToList();
     80        }
     81
     82        addedArcs = AddArcs(network, rel, varNames, targets, varImpactThreshold);
     83      }
     84
     85      return network;
     86    }
     87
     88    private static List<IArc> AddArcs(VariableInteractionNetwork network, double[,] impacts, string[] varNames, Dictionary<string, double> targets, double threshold = 0.0) {
     89      var newArcs = new List<IArc>();
     90      for (int row = 0; row < impacts.GetLength(0); row++) {
     91        if (!targets.ContainsKey(varNames[row])) continue;
     92
     93        var rowVector = Enumerable.Range(0, impacts.GetLength(0)).Select(col => impacts[row, col]).ToArray();
     94        var max = rowVector.Max();
     95        if (max > threshold) {
     96          var idxOfMax = Array.IndexOf<double>(rowVector, max);
     97          impacts[row, idxOfMax] = double.NegativeInfinity;
     98          var srcName = varNames[idxOfMax];
     99          var dstName = varNames[row];
     100          var srcVertex = network.Vertices.Single(v => v.Label == srcName);
     101          var dstVertex = network.Vertices.Single(v => v.Label == dstName);
     102          var arc = network.AddArc(srcVertex, dstVertex);
     103          arc.Weight = max;
     104          newArcs.Add(arc);
     105        }
     106      }
     107
     108      return newArcs;
     109    }
     110
    37111    /// <summary>
    38112    /// Creates a network from a matrix of variable impacts (each row represents a target variable, each column represents an input variable)
     
    262336    }
    263337
     338    public DoubleMatrix GetSimpleWeightsMatrix() {
     339      var names = Vertices.OfType<VariableNetworkNode>()
     340        .Select(v => v.Label)
     341        .OrderBy(s => s, new NaturalStringComparer()).ToArray();
     342      var w = new double[names.Length, names.Length];
     343
     344      var name2idx = new Dictionary<string, int>();
     345      for (int i = 0; i < names.Length; i++) {
     346        name2idx.Add(names[i], i);
     347      }
     348
     349      foreach (var arc in Arcs) {
     350        if (arc.Target != null) {
     351          var srcVarName = arc.Source.Label;
     352          var dstVarName = arc.Target.Label;         
     353          w[name2idx[dstVarName], name2idx[srcVarName]] = arc.Weight;
     354        }
     355      }
     356
     357      return new DoubleMatrix(w, names, names);
     358    }
     359
    264360    public string ToGraphVizString() {
    265361      Func<string, string> NodeAndEdgeColor = (str) =>
Note: See TracChangeset for help on using the changeset viewer.