Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2119


Ignore:
Timestamp:
06/26/09 17:33:13 (15 years ago)
Author:
gkronber
Message:

Changed dispatcher to configure algorithms to use only the allowed input variables and changed dispatching to dispatch deterministic algorithms again when the input variable set is different.

#676 (Cockpit for the CEDMA Server to control algorithm settings)

Location:
trunk/sources/HeuristicLab.CEDMA.Server/3.3
Files:
5 edited

Legend:

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

    r2094 r2119  
    4242    private DataSet dataset;
    4343    internal event EventHandler Changed;
    44     public IEnumerable<string> AllowedTargetVariables {
     44    private object locker = new object();
     45
     46    public IEnumerable<string> TargetVariables {
    4547      get {
    4648        if (dataset != null) {
     
    5052    }
    5153
    52     public IEnumerable<string> AllowedInputVariables {
     54    public IEnumerable<string> InputVariables {
    5355      get {
    5456        if (dataset != null) {
     
    5860    }
    5961
     62    private List<int> allowedInputVariables;
     63    private List<int> allowedTargetVariables;
     64
    6065    public DispatcherBase(IStore store) {
    6166      this.store = store;
     67      allowedInputVariables = new List<int>();
     68      allowedTargetVariables = new List<int>();
    6269    }
    6370
     
    7077        OnChanged();
    7178      }
    72       int targetVariable = SelectTargetVariable(dataset.Problem.AllowedTargetVariables.ToArray());
    73       IAlgorithm selectedAlgorithm = SelectAlgorithm(targetVariable, dataset.Problem.LearningTask);
    74       string targetVariableName = dataset.Problem.GetVariableName(targetVariable);
     79      if (allowedTargetVariables.Count > 0 && allowedInputVariables.Count > 0) {
     80        int[] targetVariables, inputVariables;
     81        lock (locker) {
     82          targetVariables = allowedTargetVariables.ToArray();
     83          inputVariables = allowedInputVariables.ToArray();
     84        }
    7585
    76       if (selectedAlgorithm != null) {
    77         SetProblemParameters(selectedAlgorithm, dataset.Problem, targetVariable);
    78       }
    79       return selectedAlgorithm;
     86        IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariables, inputVariables, dataset.Problem);
     87       
     88        return selectedAlgorithm;
     89      } else return null;
    8090    }
    8191
    82     public abstract int SelectTargetVariable(int[] targetVariables);
    83     public abstract IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask);
    84 
    85     private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable) {
    86       algo.Dataset = problem.Dataset;
    87       algo.TargetVariable = targetVariable;
    88       algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
    89       algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
    90       algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
    91       algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
    92       algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
    93       algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
    94       ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
    95       foreach (int allowedFeature in problem.AllowedInputVariables) allowedFeatures.Add(new IntData(allowedFeature));
    96 
    97       if (problem.LearningTask == LearningTask.TimeSeries) {
    98         algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
    99         algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
    100         algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
    101       } else if (problem.LearningTask == LearningTask.Classification) {
    102         ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
    103         foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
    104       }
    105     }
    106 
    107     private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
    108       return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
    109     }
    110 
    111     private void OnChanged() {
    112       if (Changed != null) Changed(this, new EventArgs());
    113     }
     92    public abstract IAlgorithm SelectAndConfigureAlgorithm(int[] targetVariables, int[] inputVariables, Problem problem);
    11493
    11594    #region IViewable Members
     
    12099
    121100    #endregion
     101
     102    internal void EnableInputVariable(string name) {
     103      lock (locker)
     104        allowedInputVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name));
     105    }
     106
     107    internal void EnableTargetVariable(string name) {
     108      lock (locker)
     109        allowedTargetVariables.Add(dataset.Problem.Dataset.GetVariableIndex(name));
     110    }
     111
     112    internal void DisableTargetVariable(string name) {
     113      lock (locker)
     114        allowedTargetVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name));
     115    }
     116
     117    internal void DisableInputVariable(string name) {
     118      lock (locker)
     119        allowedInputVariables.Remove(dataset.Problem.Dataset.GetVariableIndex(name));
     120    }
     121
     122    private void OnChanged() {
     123      if (Changed != null) Changed(this, new EventArgs());
     124    }
    122125  }
    123126}
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.Designer.cs

    r2088 r2119  
    3737      this.targetVariableList.Size = new System.Drawing.Size(171, 409);
    3838      this.targetVariableList.TabIndex = 0;
     39      this.targetVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.targetVariableList_ItemCheck);
    3940      //
    4041      // inputVariableList
     
    4546      this.inputVariableList.Size = new System.Drawing.Size(170, 409);
    4647      this.inputVariableList.TabIndex = 1;
     48      this.inputVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.inputVariableList_ItemCheck);
    4749      //
    4850      // targetVariablesLabel
     
    7375      this.Controls.Add(this.targetVariableList);
    7476      this.Name = "DispatcherView";
    75       this.Size = new System.Drawing.Size(417, 427);
     77      this.Size = new System.Drawing.Size(429, 482);
    7678      this.ResumeLayout(false);
    7779      this.PerformLayout();
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.cs

    r2094 r2119  
    2222      base.UpdateControls();
    2323
    24       foreach (string targetVar in dispatcher.AllowedTargetVariables) {
     24      foreach (string targetVar in dispatcher.TargetVariables) {
    2525        targetVariableList.Items.Add(targetVar);
    2626      }
    2727
    28       foreach (string inputVar in dispatcher.AllowedInputVariables) {
     28      foreach (string inputVar in dispatcher.InputVariables) {
    2929        inputVariableList.Items.Add(inputVar);
     30      }
     31    }
     32
     33    private void targetVariableList_ItemCheck(object sender, ItemCheckEventArgs e) {
     34      if (e.NewValue == CheckState.Checked) {
     35        dispatcher.EnableTargetVariable((string)targetVariableList.Items[e.Index]);
     36      } else if (e.NewValue == CheckState.Unchecked) {
     37        dispatcher.DisableTargetVariable((string)targetVariableList.Items[e.Index]);
     38      }
     39    }
     40
     41    private void inputVariableList_ItemCheck(object sender, ItemCheckEventArgs e) {
     42      if (e.NewValue == CheckState.Checked) {
     43        dispatcher.EnableInputVariable((string)inputVariableList.Items[e.Index]);
     44      } else if (e.NewValue == CheckState.Unchecked) {
     45        dispatcher.DisableInputVariable((string)inputVariableList.Items[e.Index]);
    3046      }
    3147    }
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ServerApplication.cs

    r2088 r2119  
    3636      serverControl.Dock = DockStyle.Fill;
    3737      mainForm.Controls.Add(serverControl);
     38      mainForm.Name = "CEDMA Server";
    3839      Application.Run(mainForm);
    3940    }
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/SimpleDispatcher.cs

    r2049 r2119  
    3939namespace HeuristicLab.CEDMA.Server {
    4040  public class SimpleDispatcher : DispatcherBase {
     41    private class AlgorithmConfiguration {
     42      public string name;
     43      public int targetVariable;
     44      public List<int> inputVariables;
     45    }
     46
    4147    private Random random;
    4248    private IStore store;
    43     private Dictionary<int, List<string>> finishedAndDispatchedRuns;
     49    private Dictionary<int, List<AlgorithmConfiguration>> finishedAndDispatchedRuns;
    4450
    4551    public SimpleDispatcher(IStore store)
     
    4753      this.store = store;
    4854      random = new Random();
    49       finishedAndDispatchedRuns = new Dictionary<int, List<string>>();
     55      finishedAndDispatchedRuns = new Dictionary<int, List<AlgorithmConfiguration>>();
    5056      PopulateFinishedRuns();
    5157    }
    5258
    53     public override IAlgorithm SelectAlgorithm(int targetVariable, LearningTask learningTask) {
     59    public override IAlgorithm SelectAndConfigureAlgorithm(int[] targetVariables, int[] inputVariables, Problem problem) {
     60      int targetVariable = SelectTargetVariable(targetVariables);
     61
    5462      DiscoveryService ds = new DiscoveryService();
    5563      IAlgorithm[] algos = ds.GetInstances<IAlgorithm>();
    5664      IAlgorithm selectedAlgorithm = null;
    57       switch (learningTask) {
     65      switch (problem.LearningTask) {
    5866        case LearningTask.Regression: {
    5967            var regressionAlgos = algos.Where(a => (a as IClassificationAlgorithm) == null && (a as ITimeSeriesAlgorithm) == null);
    60             selectedAlgorithm = ChooseDeterministic(targetVariable, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
     68            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, regressionAlgos) ?? ChooseStochastic(regressionAlgos);
    6169            break;
    6270          }
    6371        case LearningTask.Classification: {
    6472            var classificationAlgos = algos.Where(a => (a as IClassificationAlgorithm) != null);
    65             selectedAlgorithm = ChooseDeterministic(targetVariable, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
     73            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, classificationAlgos) ?? ChooseStochastic(classificationAlgos);
    6674            break;
    6775          }
    6876        case LearningTask.TimeSeries: {
    6977            var timeSeriesAlgos = algos.Where(a => (a as ITimeSeriesAlgorithm) != null);
    70             selectedAlgorithm = ChooseDeterministic(targetVariable, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
     78            selectedAlgorithm = ChooseDeterministic(targetVariable, inputVariables, timeSeriesAlgos) ?? ChooseStochastic(timeSeriesAlgos);
    7179            break;
    7280          }
    7381      }
     82
     83      SetProblemParameters(selectedAlgorithm, problem, targetVariable, inputVariables);
     84
    7485      if (selectedAlgorithm != null) {
    75         AddDispatchedRun(targetVariable, selectedAlgorithm.Name);
     86        AddDispatchedRun(targetVariable, inputVariables, selectedAlgorithm.Name);
    7687      }
    7788      return selectedAlgorithm;
    7889    }
    7990
    80     private IAlgorithm ChooseDeterministic(int targetVariable, IEnumerable<IAlgorithm> algos) {
     91    private IAlgorithm ChooseDeterministic(int targetVariable, int[] inputVariables, IEnumerable<IAlgorithm> algos) {
    8192      var deterministicAlgos = algos
    8293        .Where(a => (a as IStochasticAlgorithm) == null)
    83         .Where(a => AlgorithmFinishedOrDispatched(targetVariable, a.Name) == false);
     94        .Where(a => AlgorithmFinishedOrDispatched(targetVariable, inputVariables, a.Name) == false);
    8495
    8596      if (deterministicAlgos.Count() == 0) return null;
     
    93104    }
    94105
    95     public override int SelectTargetVariable(int[] targetVariables) {
     106    public int SelectTargetVariable(int[] targetVariables) {
    96107      return targetVariables[random.Next(targetVariables.Length)];
    97108    }
    98109
    99110    private void PopulateFinishedRuns() {
     111      Dictionary<Entity, Entity> processedModels = new Dictionary<Entity, Entity>();
    100112      var datasetBindings = store
    101113        .Query(
     
    112124          "?Model <" + Ontology.Name + "> ?AlgoName .",
    113125          0, 1000)
    114           .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName") });
     126          .Select(x => new Resource[] { (Literal)x.Get("TargetVariable"), (Literal)x.Get("AlgoName"), (Entity)x.Get("Model") });
    115127
    116128        foreach (Resource[] row in result) {
    117           string targetVariable = (string)((Literal)row[0]).Value;
    118           string algoName = (string)((Literal)row[1]).Value;
    119 
    120           int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable);
    121           if (!AlgorithmFinishedOrDispatched(targetVariableIndex, algoName))
    122             AddDispatchedRun(targetVariableIndex, algoName);
     129          Entity model = ((Entity)row[2]);
     130          if (!processedModels.ContainsKey(model)) {
     131            processedModels.Add(model, model);
     132
     133            string targetVariable = (string)((Literal)row[0]).Value;
     134            string algoName = (string)((Literal)row[1]).Value;
     135            int targetVariableIndex = ds.Problem.Dataset.GetVariableIndex(targetVariable);
     136
     137            var inputVariableLiterals = store
     138              .Query(
     139                "<" + model.Uri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." + Environment.NewLine +
     140                "?InputVariable <" + Ontology.Name + "> ?Name .",
     141                0, 1000)
     142              .Select(x => (Literal)x.Get("Name"))
     143              .Select(l => (string)l.Value)
     144              .Distinct();
     145
     146            List<int> inputVariables = new List<int>();
     147            foreach (string variableName in inputVariableLiterals) {
     148              int variableIndex = ds.Problem.Dataset.GetVariableIndex(variableName);
     149              inputVariables.Add(variableIndex);
     150            }
     151            if (!AlgorithmFinishedOrDispatched(targetVariableIndex, inputVariables.ToArray(), algoName)) {
     152              AddDispatchedRun(targetVariableIndex, inputVariables.ToArray(), algoName);
     153            }
     154          }
    123155        }
    124156      }
    125157    }
    126158
    127     private void AddDispatchedRun(int targetVariable, string algoName) {
     159    private void SetProblemParameters(IAlgorithm algo, Problem problem, int targetVariable, int[] inputVariables) {
     160      algo.Dataset = problem.Dataset;
     161      algo.TargetVariable = targetVariable;
     162      algo.ProblemInjector.GetVariable("TrainingSamplesStart").GetValue<IntData>().Data = problem.TrainingSamplesStart;
     163      algo.ProblemInjector.GetVariable("TrainingSamplesEnd").GetValue<IntData>().Data = problem.TrainingSamplesEnd;
     164      algo.ProblemInjector.GetVariable("ValidationSamplesStart").GetValue<IntData>().Data = problem.ValidationSamplesStart;
     165      algo.ProblemInjector.GetVariable("ValidationSamplesEnd").GetValue<IntData>().Data = problem.ValidationSamplesEnd;
     166      algo.ProblemInjector.GetVariable("TestSamplesStart").GetValue<IntData>().Data = problem.TestSamplesStart;
     167      algo.ProblemInjector.GetVariable("TestSamplesEnd").GetValue<IntData>().Data = problem.TestSamplesEnd;
     168      ItemList<IntData> allowedFeatures = algo.ProblemInjector.GetVariable("AllowedFeatures").GetValue<ItemList<IntData>>();
     169      foreach (int inputVariable in inputVariables) allowedFeatures.Add(new IntData(inputVariable));
     170
     171      if (problem.LearningTask == LearningTask.TimeSeries) {
     172        algo.ProblemInjector.GetVariable("Autoregressive").GetValue<BoolData>().Data = problem.AutoRegressive;
     173        algo.ProblemInjector.GetVariable("MinTimeOffset").GetValue<IntData>().Data = problem.MinTimeOffset;
     174        algo.ProblemInjector.GetVariable("MaxTimeOffset").GetValue<IntData>().Data = problem.MaxTimeOffset;
     175      } else if (problem.LearningTask == LearningTask.Classification) {
     176        ItemList<DoubleData> classValues = algo.ProblemInjector.GetVariable("TargetClassValues").GetValue<ItemList<DoubleData>>();
     177        foreach (double classValue in GetDifferentClassValues(problem.Dataset, targetVariable)) classValues.Add(new DoubleData(classValue));
     178      }
     179    }
     180
     181    private IEnumerable<double> GetDifferentClassValues(HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable) {
     182      return Enumerable.Range(0, dataset.Rows).Select(x => dataset.GetValue(x, targetVariable)).Distinct();
     183    }
     184
     185    private void AddDispatchedRun(int targetVariable, int[] inputVariables, string algoName) {
    128186      if (!finishedAndDispatchedRuns.ContainsKey(targetVariable)) {
    129         finishedAndDispatchedRuns[targetVariable] = new List<string>();
    130       }
    131       finishedAndDispatchedRuns[targetVariable].Add(algoName);
    132     }
    133 
    134     private bool AlgorithmFinishedOrDispatched(int targetVariable, string algoName) {
     187        finishedAndDispatchedRuns[targetVariable] = new List<AlgorithmConfiguration>();
     188      }
     189      AlgorithmConfiguration conf = new AlgorithmConfiguration();
     190      conf.name = algoName;
     191      conf.inputVariables = new List<int>(inputVariables);
     192      conf.targetVariable = targetVariable;
     193      finishedAndDispatchedRuns[targetVariable].Add(conf);
     194    }
     195
     196    private bool AlgorithmFinishedOrDispatched(int targetVariable, int[] inputVariables, string algoName) {
    135197      return
    136198        finishedAndDispatchedRuns.ContainsKey(targetVariable) &&
    137         finishedAndDispatchedRuns[targetVariable].Contains(algoName);
     199        finishedAndDispatchedRuns[targetVariable].Any(x => targetVariable == x.targetVariable &&
     200                                                           algoName == x.name &&
     201                                                           inputVariables.Count() == x.inputVariables.Count() &&
     202                                                           inputVariables.All(v => x.inputVariables.Contains(v)));
    138203    }
    139204  }
Note: See TracChangeset for help on using the changeset viewer.