Changeset 2290


Ignore:
Timestamp:
08/14/09 19:56:04 (12 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)

Location:
trunk/sources
Files:
16 edited

Legend:

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

    r2258 r2290  
    3737namespace HeuristicLab.CEDMA.Server {
    3838  public abstract class DispatcherBase : IDispatcher {
    39     private IModelingDatabase database;
    40     public IModelingDatabase Database {
    41       get {
    42         return database;
    43       }
    44     }
    45     private List<int> allowedTargetVariables;
    46     private Dictionary<int, List<int>> activeInputVariables;
    47     private Problem problem;
    48     public Problem Problem {
    49       get {
    50         return problem;
    51       }
    52     }
    53     internal event EventHandler Changed;
    54     private object locker = new object();
    55 
    56     public IEnumerable<string> TargetVariables {
    57       get {
    58         if (problem != null) {
    59           return Enumerable.Range(0, problem.Dataset.Columns).Select(x => problem.Dataset.GetVariableName(x));
    60         } else return new string[0];
    61       }
    62     }
    63 
    64     public IEnumerable<string> InputVariables {
    65       get {
    66         if (problem != null) {
    67           return TargetVariables;
    68         } else return new string[0];
    69       }
    70     }
    71 
    72     public DispatcherBase(IModelingDatabase database, Problem problem) {
    73       this.problem = problem;
    74       allowedTargetVariables = new List<int>();
    75       activeInputVariables = new Dictionary<int, List<int>>();
    76       problem.Changed += (sender, args) => {
    77         lock (locker) {
    78           allowedTargetVariables.Clear();
    79           activeInputVariables.Clear();
    80         }
    81         OnChanged();
    82       };
    83       this.database = database;
    84     }
    85 
    86     public HeuristicLab.Modeling.IAlgorithm GetNextJob() {
    87       if (allowedTargetVariables.Count > 0) {
    88         int[] targetVariables, inputVariables;
    89         lock (locker) {
    90           targetVariables = allowedTargetVariables.ToArray();
    91         }
    92 
    93         int targetVariable = SelectTargetVariable(targetVariables);
    94 
    95         lock (locker) {
    96           inputVariables = activeInputVariables[targetVariable].ToArray();
    97         }
    98 
    99         HeuristicLab.Modeling.IAlgorithm selectedAlgorithm = SelectAndConfigureAlgorithm(targetVariable, inputVariables, problem);
    100 
    101         return selectedAlgorithm;
    102       } else return null;
    103     }
    104 
    105     public virtual int SelectTargetVariable(int[] targetVariables) {
    106       Random rand = new Random();
    107       return targetVariables[rand.Next(targetVariables.Length)];
    108     }
    109     public abstract HeuristicLab.Modeling.IAlgorithm SelectAndConfigureAlgorithm(int targetVariable, int[] inputVariables, Problem problem);
    110 
    111     #region IViewable Members
    112 
    113     public IView CreateView() {
    114       return new DispatcherView(this);
    115     }
    116 
    117     #endregion
    118 
    119     internal void EnableTargetVariable(string name) {
    120       lock (locker)
    121         allowedTargetVariables.Add(problem.Dataset.GetVariableIndex(name));
    122     }
    123 
    124     internal void DisableTargetVariable(string name) {
    125       lock (locker)
    126         allowedTargetVariables.Remove(problem.Dataset.GetVariableIndex(name));
    127     }
    128 
    129     internal void EnableInputVariable(string target, string name) {
    130       lock (locker) {
    131         int targetIndex = problem.Dataset.GetVariableIndex(target);
    132         int inputIndex = problem.Dataset.GetVariableIndex(name);
    133         if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
    134         if (!activeInputVariables[targetIndex].Contains(inputIndex)) {
    135           activeInputVariables[targetIndex].Add(inputIndex);
    136         }
    137       }
    138     }
    139 
    140     internal void DisableInputVariable(string target, string name) {
    141       lock (locker) {
    142         int targetIndex = problem.Dataset.GetVariableIndex(target);
    143         int inputIndex = problem.Dataset.GetVariableIndex(name);
    144         if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
    145         while (activeInputVariables[targetIndex].Remove(inputIndex)) { }
    146       }
    147     }
    148 
    149     private void OnChanged() {
    150       if (Changed != null) Changed(this, new EventArgs());
    151     }
    152 
    153     internal IEnumerable<string> GetInputVariables(string target) {
    154       int targetIndex = problem.Dataset.GetVariableIndex(target);
    155       if (!activeInputVariables.ContainsKey(targetIndex)) activeInputVariables[targetIndex] = new List<int>();
    156       return activeInputVariables[targetIndex]
    157         .Select(i => problem.Dataset.GetVariableName(i));
    158     }
    15939  }
    16040}
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.Designer.cs

    r2258 r2290  
    2929      this.inputVariablesLabel = new System.Windows.Forms.Label();
    3030      this.splitContainer = new System.Windows.Forms.SplitContainer();
     31      this.algorithmsListBox = new System.Windows.Forms.CheckedListBox();
     32      this.algorithmsLabel = new System.Windows.Forms.Label();
    3133      this.setAllButton = new System.Windows.Forms.Button();
    3234      this.splitContainer.Panel1.SuspendLayout();
     
    4244      this.targetVariableList.FormattingEnabled = true;
    4345      this.targetVariableList.HorizontalScrollbar = true;
    44       this.targetVariableList.Location = new System.Drawing.Point(6, 16);
     46      this.targetVariableList.Location = new System.Drawing.Point(6, 181);
    4547      this.targetVariableList.Name = "targetVariableList";
    46       this.targetVariableList.Size = new System.Drawing.Size(193, 454);
     48      this.targetVariableList.Size = new System.Drawing.Size(193, 289);
    4749      this.targetVariableList.TabIndex = 0;
    4850      this.targetVariableList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.targetVariableList_ItemCheck);
     
    6567      //
    6668      this.targetVariablesLabel.AutoSize = true;
    67       this.targetVariablesLabel.Location = new System.Drawing.Point(3, 0);
     69      this.targetVariablesLabel.Location = new System.Drawing.Point(3, 165);
    6870      this.targetVariablesLabel.Name = "targetVariablesLabel";
    6971      this.targetVariablesLabel.Size = new System.Drawing.Size(86, 13);
     
    8890      // splitContainer.Panel1
    8991      //
     92      this.splitContainer.Panel1.Controls.Add(this.algorithmsListBox);
     93      this.splitContainer.Panel1.Controls.Add(this.algorithmsLabel);
    9094      this.splitContainer.Panel1.Controls.Add(this.targetVariablesLabel);
    9195      this.splitContainer.Panel1.Controls.Add(this.targetVariableList);
     
    100104      this.splitContainer.SplitterWidth = 1;
    101105      this.splitContainer.TabIndex = 4;
     106      //
     107      // algorithmsListBox
     108      //
     109      this.algorithmsListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     110                  | System.Windows.Forms.AnchorStyles.Right)));
     111      this.algorithmsListBox.FormattingEnabled = true;
     112      this.algorithmsListBox.HorizontalScrollbar = true;
     113      this.algorithmsListBox.Location = new System.Drawing.Point(6, 16);
     114      this.algorithmsListBox.Name = "algorithmsListBox";
     115      this.algorithmsListBox.Size = new System.Drawing.Size(193, 139);
     116      this.algorithmsListBox.TabIndex = 4;
     117      this.algorithmsListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.algorithmsListBox_ItemCheck);
     118      //
     119      // algorithmsLabel
     120      //
     121      this.algorithmsLabel.AutoSize = true;
     122      this.algorithmsLabel.Location = new System.Drawing.Point(3, 0);
     123      this.algorithmsLabel.Name = "algorithmsLabel";
     124      this.algorithmsLabel.Size = new System.Drawing.Size(58, 13);
     125      this.algorithmsLabel.TabIndex = 3;
     126      this.algorithmsLabel.Text = "Algorithms:";
    102127      //
    103128      // setAllButton
     
    136161    private System.Windows.Forms.SplitContainer splitContainer;
    137162    private System.Windows.Forms.Button setAllButton;
     163    private System.Windows.Forms.CheckedListBox algorithmsListBox;
     164    private System.Windows.Forms.Label algorithmsLabel;
    138165  }
    139166}
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/DispatcherView.cs

    r2258 r2290  
    1111namespace HeuristicLab.CEDMA.Server {
    1212  public partial class DispatcherView : ViewBase {
    13     private DispatcherBase dispatcher;
    14     public DispatcherView(DispatcherBase dispatcher)
     13    private SimpleDispatcher dispatcher;
     14    public DispatcherView(SimpleDispatcher dispatcher)
    1515      : base() {
    1616      this.dispatcher = dispatcher;
     
    2626      } else {
    2727        base.UpdateControls();
     28        algorithmsListBox.Items.Clear();
    2829        targetVariableList.Items.Clear();
    2930        inputVariableList.Items.Clear();
     
    3536        foreach (string inputVar in dispatcher.InputVariables) {
    3637          inputVariableList.Items.Add(inputVar, false);
     38        }
     39
     40        foreach (HeuristicLab.Modeling.IAlgorithm algo in dispatcher.Algorithms) {
     41          algorithmsListBox.Items.Add(algo, false);
    3742        }
    3843        targetVariableList.ClearSelected();
     
    8388      }
    8489    }
     90
     91    private void algorithmsListBox_ItemCheck(object sender, ItemCheckEventArgs e) {
     92      if(e.NewValue == CheckState.Checked) {
     93        dispatcher.EnableAlgorithm((HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]);
     94      } else if(e.NewValue == CheckState.Unchecked) {
     95        dispatcher.DisableAlgorithm((HeuristicLab.Modeling.IAlgorithm)algorithmsListBox.Items[e.Index]);
     96      }
     97    }
    8598  }
    8699}
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterBase.cs

    r2238 r2290  
    5858    }
    5959
     60    public int CalculatedJobs {
     61      get;
     62      set;
     63    }
     64    public int StoredJobs {
     65      get;
     66      set;
     67    }
     68
    6069    public ExecuterBase(IDispatcher dispatcher, IModelingDatabase databaseService) {
     70      CalculatedJobs = 0;
    6171      maxActiveJobs = 10;
    6272      this.dispatcher = dispatcher;
    6373      this.databaseService = databaseService;
     74      StoredJobs = databaseService.GetAllModels().Count();
    6475    }
    6576
     
    8394
    8495    protected void StoreResults(HeuristicLab.Modeling.IAlgorithm finishedAlgorithm) {
     96      CalculatedJobs++;
    8597      databaseService.Persist(finishedAlgorithm);
     98      StoredJobs++;
     99      OnChanged();
    86100    }
    87101
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterView.Designer.cs

    r2125 r2290  
    2727      this.maxJobsLabel = new System.Windows.Forms.Label();
    2828      this.maxActiveJobs = new System.Windows.Forms.NumericUpDown();
     29      this.finishedLabel = new System.Windows.Forms.Label();
     30      this.finishedTextBox = new System.Windows.Forms.TextBox();
    2931      ((System.ComponentModel.ISupportInitialize)(this.maxActiveJobs)).BeginInit();
    3032      this.SuspendLayout();
     
    3638                  | System.Windows.Forms.AnchorStyles.Right)));
    3739      this.jobsList.FormattingEnabled = true;
    38       this.jobsList.Location = new System.Drawing.Point(3, 29);
     40      this.jobsList.Location = new System.Drawing.Point(3, 55);
    3941      this.jobsList.Name = "jobsList";
    40       this.jobsList.Size = new System.Drawing.Size(327, 264);
     42      this.jobsList.Size = new System.Drawing.Size(327, 238);
    4143      this.jobsList.TabIndex = 0;
    4244      //
     
    5254      // maxActiveJobs
    5355      //
    54       this.maxActiveJobs.Location = new System.Drawing.Point(98, 3);
     56      this.maxActiveJobs.Location = new System.Drawing.Point(117, 3);
    5557      this.maxActiveJobs.Maximum = new decimal(new int[] {
    5658            200,
     
    6365      this.maxActiveJobs.ValueChanged += new System.EventHandler(this.maxActiveJobs_ValueChanged);
    6466      //
     67      // finishedLabel
     68      //
     69      this.finishedLabel.AutoSize = true;
     70      this.finishedLabel.Location = new System.Drawing.Point(5, 32);
     71      this.finishedLabel.Name = "finishedLabel";
     72      this.finishedLabel.Size = new System.Drawing.Size(106, 13);
     73      this.finishedLabel.TabIndex = 3;
     74      this.finishedLabel.Text = "Stored models (new):";
     75      //
     76      // finishedTextBox
     77      //
     78      this.finishedTextBox.Location = new System.Drawing.Point(117, 29);
     79      this.finishedTextBox.Name = "finishedTextBox";
     80      this.finishedTextBox.ReadOnly = true;
     81      this.finishedTextBox.Size = new System.Drawing.Size(100, 20);
     82      this.finishedTextBox.TabIndex = 4;
     83      //
    6584      // ExecuterView
    6685      //
    6786      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    6887      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     88      this.Controls.Add(this.finishedTextBox);
     89      this.Controls.Add(this.finishedLabel);
    6990      this.Controls.Add(this.maxActiveJobs);
    7091      this.Controls.Add(this.maxJobsLabel);
     
    83104    private System.Windows.Forms.Label maxJobsLabel;
    84105    private System.Windows.Forms.NumericUpDown maxActiveJobs;
     106    private System.Windows.Forms.Label finishedLabel;
     107    private System.Windows.Forms.TextBox finishedTextBox;
    85108  }
    86109}
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterView.cs

    r2094 r2290  
    2424      else {
    2525        base.UpdateControls();
     26        finishedTextBox.Text = executer.StoredJobs+" ("+executer.CalculatedJobs+")";
    2627        maxActiveJobs.Value = executer.MaxActiveJobs;
    2728        jobsList.DataSource = executer.GetJobs();
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/HeuristicLab.CEDMA.Server-3.3.csproj

    r2223 r2290  
    113113    <Compile Include="ExecuterBase.cs" />
    114114    <Compile Include="IExecuter.cs" />
    115     <Compile Include="DispatcherBase.cs" />
    116115    <Compile Include="GridExecuter.cs" />
    117116    <Compile Include="IDispatcher.cs" />
  • trunk/sources/HeuristicLab.CEDMA.Server/3.3/ProblemView.cs

    r2285 r2290  
    111111    private void autoregressiveCheckBox_CheckedChanged(object sender, EventArgs e) {
    112112      problem.AutoRegressive = autoregressiveCheckBox.Checked;
     113      problem.FireChanged();
    113114    }
    114115
     
    145146      problem.TestSamplesStart = int.Parse(testSamplesStartTextBox.Text);
    146147      problem.TestSamplesEnd = int.Parse(testSamplesEndTextBox.Text);
     148      problem.FireChanged();
    147149    }
    148150
     
    193195    }
    194196
    195     #region gui events
    196197    private void radioButton_CheckedChanged(object sender, EventArgs e) {
    197198      minTimeOffsetLabel.Enabled = timeSeriesRadioButton.Checked;
     
    204205      else if (classificationRadioButton.Checked) problem.LearningTask = LearningTask.Classification;
    205206      else if (regressionRadioButton.Checked) problem.LearningTask = LearningTask.Regression;
     207      problem.FireChanged();
    206208    }
    207209
     
    215217      problem.MinTimeOffset = int.Parse(minTimeOffsetTextBox.Text);
    216218      problem.MaxTimeOffset = int.Parse(maxTimeOffsetTextBox.Text);
    217     }
    218     #endregion
     219      problem.FireChanged();
     220    }
    219221  }
    220222}
  • 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}
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/AlgorithmBase.cs

    r2285 r2290  
    412412      Engine.GlobalScope.AddSubScope(bestModelScope);
    413413      IGeneticProgrammingModel tree = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
    414       ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", false);
     414      ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", true);
    415415      IAnalyzerModel model = new AnalyzerModel();
    416416      model.Predictor = new Predictor(evaluator, tree);
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableEvaluationImpactCalculator.cs

    r2285 r2290  
    3232      AddVariableInfo(new VariableInfo("TreeEvaluator", "The evaluator that should be used to evaluate the expression tree", typeof(ITreeEvaluator), VariableKind.In));
    3333      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
    34       AddVariableInfo(new VariableInfo("TreeSize", "Size (number of nodes) of the tree to evaluate", typeof(IntData), VariableKind.In));
    3534    }
    3635
     
    3938      ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    4039      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    41       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    4240      evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    4341
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableQualityImpactCalculator.cs

    r2285 r2290  
    3838      ITreeEvaluator evaluator = GetVariableValue<ITreeEvaluator>("TreeEvaluator", scope, true);
    3939      IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
    40       double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
    4140      evaluator.PrepareForEvaluation(dataset, targetVariable, start, end, gpModel.FunctionTree);
    4241
  • trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegression.cs

    r2285 r2290  
    270270    protected internal virtual IAnalyzerModel CreateLRModel(IScope bestModelScope) {
    271271      IGeneticProgrammingModel tree = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("LinearRegressionModel", false);
    272       ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", false);
     272      ITreeEvaluator evaluator = bestModelScope.GetVariableValue<ITreeEvaluator>("TreeEvaluator", true);
    273273      IAnalyzerModel model = new AnalyzerModel();
    274274      model.Predictor = new Predictor(evaluator, tree);
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs

    r2285 r2290  
    3434  public class Predictor : ItemBase, IPredictor {
    3535    private SVMModel svmModel;
     36    private Dictionary<string, int> variableNames = new Dictionary<string, int>();
    3637    private string targetVariable;
    3738
    3839    public Predictor() : base() { } // for persistence
    3940
    40     public Predictor(SVMModel model, string targetVariable)
     41    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames)
    4142      : base() {
    4243      this.svmModel = model;
    4344      this.targetVariable = targetVariable;
     45      this.variableNames = variableNames;
    4446    }
    4547
     
    4951      RangeTransform transform = svmModel.RangeTransform;
    5052      Model model = svmModel.Model;
     53      // maps columns of the current input dataset to the columns that were originally used in training
     54      Dictionary<int, int> newIndex = new Dictionary<int, int>();
     55      foreach (var pair in variableNames) {
     56        newIndex[input.GetVariableIndex(pair.Key)] = pair.Value;
     57      }
    5158
    52       Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), start, end);
     59      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, start, end);
    5360      Problem scaledProblem = SVM.Scaling.Scale(p, transform);
    5461
     
    7077      clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
    7178      clone.targetVariable = targetVariable;
     79      clone.variableNames = new Dictionary<string, int>(variableNames);
    7280      return clone;
    7381    }
     
    7987      node.Attributes.Append(targetVarAttr);
    8088      node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
     89      XmlNode variablesNode = document.CreateElement("Variables");
     90      foreach (var pair in variableNames) {
     91        XmlNode pairNode = document.CreateElement("Variable");
     92        XmlAttribute nameAttr = document.CreateAttribute("Name");
     93        XmlAttribute indexAttr = document.CreateAttribute("Index");
     94        nameAttr.Value = pair.Key;
     95        indexAttr.Value = XmlConvert.ToString(pair.Value);
     96        pairNode.Attributes.Append(nameAttr);
     97        pairNode.Attributes.Append(indexAttr);
     98        variablesNode.AppendChild(pairNode);
     99      }
     100      node.AppendChild(variablesNode);
    81101      return node;
    82102    }
     
    86106      targetVariable = node.Attributes["TargetVariable"].Value;
    87107      svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
     108
     109      variableNames = new Dictionary<string, int>();
     110      XmlNode variablesNode = node.ChildNodes[1];
     111      foreach (XmlNode pairNode in variablesNode.ChildNodes) {
     112        variableNames[pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value);
     113      }
    88114    }
    89115  }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs

    r2285 r2290  
    99namespace HeuristicLab.SupportVectorMachines {
    1010  public class SVMHelper {
     11
    1112    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) {
     13      return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end);
     14    }
     15
     16    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end) {
    1217      int rowCount = end - start;
    1318      List<int> skippedFeatures = new List<int>();
     
    2429      for (int i = 0; i < rowCount; i++) {
    2530        double value = dataset.GetValue(start + i, targetVariable);
    26           targetVector[i] = value;
     31        targetVector[i] = value;
    2732      }
    28       targetVector = targetVector.Where(x=> !double.IsNaN(x)).ToArray();
     33      targetVector = targetVector.Where(x => !double.IsNaN(x)).ToArray();
    2934
    3035      SVM.Node[][] nodes = new SVM.Node[targetVector.Length][];
     
    3439        tempRow = new List<SVM.Node>();
    3540        for (int col = 0; col < dataset.Columns; col++) {
    36           if (!skippedFeatures.Contains(col) && col!=targetVariable) {
     41          if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) {
    3742            double value = dataset.GetValue(start + row, col);
    3843            if (!double.IsNaN(value))
    39               tempRow.Add(new SVM.Node(col + 1, value));
     44              tempRow.Add(new SVM.Node(columnMapping[col], value));
    4045          }
    4146        }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs

    r2285 r2290  
    448448      model.TestSamplesStart = bestModelScope.GetVariableValue<IntData>("TestSamplesStart", true).Data;
    449449      model.TestSamplesEnd = bestModelScope.GetVariableValue<IntData>("TestSamplesEnd", true).Data;
    450       model.Predictor = new Predictor(bestModelScope.GetVariableValue<SVMModel>("Model", false), model.TargetVariable);
     450      Dictionary<string, int> variableNames = new Dictionary<string, int>();
     451      for (int i = 0; i < ds.Columns; i++) variableNames[ds.GetVariableName(i)] = i;
     452      model.Predictor = new Predictor(bestModelScope.GetVariableValue<SVMModel>("Model", false), model.TargetVariable, variableNames);
    451453
    452454
Note: See TracChangeset for help on using the changeset viewer.