Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/14/09 19:56:04 (15 years ago)
Author:
gkronber
Message:
  • introduced a variablename to index mapping for SVM models (to make sure we can use the model for prediction in the model analyzer)
  • added support to enable and disable algorithms in the dispatcher and removed DispatcherBase
  • fixed bugs when calculating variable impacts and reading the final model of GP algorithms

#722 (IModel should provide a Predict() method to get predicted values for an input vector)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs

    r2275 r2290  
    3636
    3737namespace HeuristicLab.CEDMA.Server {
    38   public class SimpleDispatcher : DispatcherBase {
     38  public class SimpleDispatcher : IDispatcher {
    3939    private class AlgorithmConfiguration {
    4040      public string name;
     
    4343    }
    4444
     45    private IModelingDatabase database;
     46    public IModelingDatabase Database {
     47      get {
     48        return database;
     49      }
     50    }
     51
     52    private Problem problem;
     53    public Problem Problem {
     54      get {
     55        return problem;
     56      }
     57    }
     58    internal event EventHandler Changed;
     59
     60    public IEnumerable<string> TargetVariables {
     61      get {
     62        return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x));
     63      }
     64    }
     65
     66    public IEnumerable<string> InputVariables {
     67      get {
     68        return TargetVariables;
     69      }
     70    }
     71
     72    private HeuristicLab.Modeling.IAlgorithm[] algorithms;
     73    public IEnumerable<HeuristicLab.Modeling.IAlgorithm> Algorithms {
     74      get {
     75        switch (Problem.LearningTask) {
     76          case LearningTask.Regression: {
     77              return algorithms.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
     78            }
     79          case LearningTask.Classification: {
     80              return algorithms.Where(a => (a as IClassificationAlgorithm) != null);
     81            }
     82          case LearningTask.TimeSeries: {
     83              return algorithms.Where(a => (a as ITimeSeriesAlgorithm) != null);
     84            }
     85        }
     86        return new HeuristicLab.Modeling.IAlgorithm[] { };
     87      }
     88    }
     89
     90    private List<HeuristicLab.Modeling.IAlgorithm> activeAlgorithms;
     91    public IEnumerable<HeuristicLab.Modeling.IAlgorithm> ActiveAlgorithms {
     92      get { return activeAlgorithms; }
     93    }
     94
    4595    private Random random;
     96    private List<int> allowedTargetVariables;
     97    private Dictionary<int, List<int>> activeInputVariables;
    4698    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
    47 
    48     public SimpleDispatcher(IModelingDatabase database, Problem problem)
    49       : base(database, problem) {
    50       random = new Random();
    51       finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
     99    private object locker = new object();
     100
     101    public SimpleDispatcher(IModelingDatabase database, Problem problem) {
     102      this.problem = problem;
     103      this.database = database;
     104      this.random = new Random();
     105
     106      this.finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
     107      this.allowedTargetVariables = new List<int>();
     108      this.activeInputVariables = new Dictionary<int, List<int>>();
     109      this.activeAlgorithms = new List<HeuristicLab.Modeling.IAlgorithm>();
     110      DiscoveryService ds = new DiscoveryService();
     111      this.algorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
     112      problem.Changed += (sender, args) => {
     113        lock (locker) {
     114          allowedTargetVariables.Clear();
     115          activeInputVariables.Clear();
     116          activeAlgorithms.Clear();
     117        }
     118        OnChanged();
     119      };
     120
    52121      PopulateFinishedRuns();
    53122    }
    54123
    55     public override HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
     124    public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
     125      lock (locker) {
     126        if (allowedTargetVariables.Count > 0) {
     127          int[] targetVariables = allowedTargetVariables.ToArray();
     128          int targetVariable = SelectTargetVariable(targetVariables);
     129          int[] inputVariables = activeInputVariables[targetVariable].ToArray();
     130
     131          HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem);
     132
     133          return selectedAlgorithm;
     134        } else return null;
     135      }
     136    }
     137
     138    public virtual int SelectTargetVariable(int[] targetVariables) {
     139      return targetVariables[random.Next(targetVariables.Length)];
     140    }
     141
     142    public HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem) {
     143      HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
    56144      DiscoveryService ds = new DiscoveryService();
    57       HeuristicLab.Modeling.IAlgorithm[] algos = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
    58       HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = null;
    59       switch (problem.LearningTask) {
    60         case LearningTask.Regression: {
    61             var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
    62             selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
    63             break;
    64           }
    65         case LearningTask.Classification: {
    66             var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
    67             selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
    68             break;
    69           }
    70         case LearningTask.TimeSeries: {
    71             var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
    72             selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
    73             break;
    74           }
    75       }
    76 
    77 
     145      var allAlgorithms = ds.GetInstances<HeuristicLab.Modeling.IAlgorithm>();
     146      var allowedAlgorithmTypes = activeAlgorithms.Select(x => x.GetType());
     147      var possibleAlgos =
     148        allAlgorithms
     149        .Where(x => allowedAlgorithmTypes.Contains(x.GetType()) &&
     150          ((x is IStochasticAlgorithm) || !AlgorithmFinishedOrDispatched(targetVariable, inputVariables, x.Name)));
     151      if (possibleAlgos.Count() > 0) selectedAlgorithm = possibleAlgos.ElementAt(random.Next(possibleAlgos.Count()));
    78152      if (selectedAlgorithm != null) {
    79153        SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
    80         AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
     154        if (!(selectedAlgorithm is IStochasticAlgorithm))
     155          AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
    81156      }
    82157      return selectedAlgorithm;
    83     }
    84 
    85     private HeuristicLab.Modeling.IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<HeuristicLab.Modeling.IAlgorithm> algos) {
    86       var deterministicAlgos = algos
    87         .Where(a => (a as IStochasticAlgorithm) == null)
    88         .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
    89 
    90       if (deterministicAlgos.Count() == 0) return null;
    91       return deterministicAlgos.ElementAt(random.Next(deterministicAlgos.Count()));
    92     }
    93 
    94     private HeuristicLab.Modeling.IAlgorithm ChooseStochastic(IEnumerable<HeuristicLab.Modeling.IAlgorithm> regressionAlgos) {
    95       var stochasticAlgos = regressionAlgos.Where(a => (a as IStochasticAlgorithm) != null);
    96       if (stochasticAlgos.Count() == 0) return null;
    97       return stochasticAlgos.ElementAt(random.Next(stochasticAlgos.Count()));
    98158    }
    99159
     
    169229                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
    170230    }
     231
     232    public void EnableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
     233      lock (locker) {
     234        if (!activeAlgorithms.Contains(algo)) activeAlgorithms.Add(algo);
     235      }
     236    }
     237
     238    public void DisableAlgorithm(HeuristicLab.Modeling.IAlgorithm algo) {
     239      lock (locker) {
     240        while (activeAlgorithms.Remove(algo)) ;
     241      }
     242    }
     243
     244    internal void EnableTargetVariable(string name) {
     245      int varIndex = problem.Dataset.GetVariableIndex(name);
     246      lock (locker)
     247        if (!allowedTargetVariables.Contains(varIndex)) allowedTargetVariables.Add(varIndex);
     248    }
     249
     250    internal void DisableTargetVariable(string name) {
     251      int varIndex = problem.Dataset.GetVariableIndex(name);
     252      lock (locker)
     253        while (allowedTargetVariables.Remove(varIndex)) ;
     254    }
     255
     256    internal void EnableInputVariable(string target, string name) {
     257      int targetIndex = problem.Dataset.GetVariableIndex(target);
     258      int inputIndex = problem.Dataset.GetVariableIndex(name);
     259      lock (locker) {
     260        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
     261        if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
     262          activeInputVariables[targetIndex].Add(inputIndex);
     263        }
     264      }
     265    }
     266
     267    internal void DisableInputVariable(string target, string name) {
     268      int targetIndex = problem.Dataset.GetVariableIndex(target);
     269      int inputIndex = problem.Dataset.GetVariableIndex(name);
     270      lock (locker) {
     271        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
     272        while (activeInputVariables[targetIndex].Remove(inputIndex)) ;
     273      }
     274    }
     275
     276    public void OnChanged() {
     277      if (Changed != null) Changed(this, new EventArgs());
     278    }
     279
     280    internal IEnumerable<string> GetInputVariables(string target) {
     281      int targetIndex = problem.Dataset.GetVariableIndex(target);
     282      lock (locker) {
     283        if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
     284        return activeInputVariables[targetIndex]
     285          .Select(i => problem.Dataset.GetVariableName(i));
     286      }
     287    }
     288
     289
     290    #region IViewable Members
     291
     292    public virtual IView CreateView() {
     293      return new DispatcherView(this);
     294    }
     295
     296    #endregion
     297
     298
    171299  }
    172300}
Note: See TracChangeset for help on using the changeset viewer.