Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1217


Ignore:
Timestamp:
02/12/09 15:19:34 (16 years ago)
Author:
gkronber
Message:

Improved dispatcher code. Added an interface and a base class to make it easier to try out different dispatch strategies in the future. #419 (Refactor CEDMA plugins)

Location:
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server
Files:
1 added
1 deleted
3 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/DispatcherBase.cs

    r1216 r1217  
    3737
    3838namespace HeuristicLab.CEDMA.Server {
    39   public class Dispatcher {
    40     private List<Execution> dispatchQueue;
    41     public IList<string> DispatchQueue {
    42       get { return dispatchQueue.Select(t => "StandardGP").ToList(); }
     39  public abstract class DispatcherBase :IDispatcher {
     40    public enum ModelComplexity { Low, Medium, High };
     41    public enum Algorithm { StandardGP };
     42
     43    private IStore store;
     44    private ModelComplexity[] possibleComplexities = new ModelComplexity[] { ModelComplexity.Low, ModelComplexity.Medium, ModelComplexity.High };
     45    private Dictionary<LearningTask, Algorithm[]> possibleAlgorithms = new Dictionary<LearningTask, Algorithm[]>() {
     46      {LearningTask.Classification, new Algorithm[] {}},
     47      {LearningTask.Regression, new Algorithm[] { Algorithm.StandardGP }},
     48      {LearningTask.TimeSeries, new Algorithm[] { }}
     49    };
     50
     51    public DispatcherBase(IStore store) {
     52      this.store = store;
    4353    }
    4454
    45     private IStore store;
    46 
    47     public Dispatcher(IStore store) {
    48       this.store = store;
    49       this.dispatchQueue = new List<Execution>();
    50     }
    51 
    52     private void FillDispatchQueue() {
     55    public Execution GetNextJob() {
    5356      // find and select a dataset
    5457      var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
     
    5760      };
    5861
    59       var dataSetBindings = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .");
     62      Entity[] datasets = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .")
     63        .Select(x => (Entity)x.Get("DataSet"))
     64        .ToArray();
    6065
    6166      // no datasets => do nothing
    62       if (dataSetBindings.Count() == 0) return;
     67      if (datasets.Length == 0) return null;
    6368
    64       // assume last dataset is the most interesting one
    65       // find and select all results for this dataset
    66       var dataSetEntity = (Entity)dataSetBindings.Last().Get("DataSet");
     69      Entity dataSetEntity = SelectDataSet(datasets);
    6770      DataSet dataSet = new DataSet(store, dataSetEntity);
    68       Random random = new Random();
    69       int targetVariable = dataSet.Problem.AllowedInputVariables[random.Next(0, dataSet.Problem.AllowedInputVariables.Count)];
     71
     72      int targetVariable = SelectTargetVariable(dataSet, dataSet.Problem.AllowedInputVariables.ToArray());
     73      Algorithm selectedAlgorithm = SelectAlgorithm(dataSet, targetVariable, possibleAlgorithms[dataSet.Problem.LearningTask]);
    7074      string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
    71       Execution exec = CreateExecution(dataSet.Problem, targetVariable);
     75      ModelComplexity selectedComplexity = SelectComplexity(dataSet, targetVariable, selectedAlgorithm, possibleComplexities);
     76
     77      Execution exec = CreateExecution(dataSet.Problem, targetVariable, selectedAlgorithm, selectedComplexity);
    7278      if (exec != null) {
    7379        exec.DataSetEntity = dataSetEntity;
    7480        exec.TargetVariable = targetVariableName;
    75         QueueJob(exec);
     81      }
     82      return exec;
     83    }
     84
     85    public abstract Entity SelectDataSet(Entity[] datasets);
     86    public abstract int SelectTargetVariable(DataSet dataSet, int[] targetVariables);
     87    public abstract Algorithm SelectAlgorithm(DataSet dataSet, int targetVariable, Algorithm[] possibleAlgorithms);
     88    public abstract ModelComplexity SelectComplexity(DataSet dataSet, int targetVariable, Algorithm algorithm, ModelComplexity[] possibleComplexities);
     89
     90    private Execution CreateExecution(Problem problem, int targetVariable, Algorithm algorithm, ModelComplexity complexity) {
     91      switch (algorithm) {
     92        case Algorithm.StandardGP: {
     93            return CreateStandardGpExecution(problem, targetVariable, complexity);
     94          }
     95        default: {
     96            return null;
     97          }
    7698      }
    7799    }
    78100
    79     private void QueueJob(Execution execution) {
    80       dispatchQueue.Add(execution);
    81     }
    82 
    83     public Execution GetNextJob() {
    84       if (dispatchQueue.Count == 0) {
    85         FillDispatchQueue();
    86       }
    87       if (dispatchQueue.Count > 0) {
    88         Execution next = dispatchQueue[0];
    89         dispatchQueue.RemoveAt(0);
    90         return next;
    91       } else
    92         return null;
    93     }
    94 
    95     internal void Start() {
    96       FillDispatchQueue();
    97     }
    98 
    99     private Execution CreateExecution(Problem problem, int targetVariable) {
    100       switch (problem.LearningTask) {
    101         case LearningTask.Classification: return null;
    102         case LearningTask.Regression: {
    103             return CreateStandardGpExecution(problem, targetVariable, 100, 10);
    104           }
    105         case LearningTask.TimeSeries: return null;
    106         case LearningTask.Clustering: return null;
    107         default: return null;
    108       }
    109     }
    110 
    111     private Execution CreateStandardGpExecution(Problem problem, int targetVariable, int maxTreeSize, int maxTreeHeight) {
     101    private Execution CreateStandardGpExecution(Problem problem, int targetVariable, ModelComplexity complexity) {
    112102      ProblemInjector probInjector = new ProblemInjector(problem);
    113103      probInjector.TargetVariable = targetVariable;
     
    118108      sgp.Elites = 1;
    119109      sgp.ProblemInjector = probInjector;
     110
     111      int maxTreeHeight = 10;
     112      int maxTreeSize = 100;
     113      switch (complexity) {
     114        case ModelComplexity.Low: {
     115            maxTreeHeight = 5;
     116            maxTreeSize = 20;
     117            break;
     118          }
     119        case ModelComplexity.Medium: {
     120            maxTreeHeight = 10;
     121            maxTreeSize = 100;
     122            break;
     123          }
     124        case ModelComplexity.High: {
     125            maxTreeHeight = 12;
     126            maxTreeSize = 200;
     127            break;
     128          }
     129      }
     130
    120131      sgp.MaxTreeHeight = maxTreeHeight;
    121132      sgp.MaxTreeSize = maxTreeSize;
    122133      Execution exec = new Execution(sgp.Engine);
    123       exec.Description = "StandardGP - Medium Complexity";
     134      exec.Description = "StandardGP - Complexity: " + complexity;
    124135      return exec;
    125136    }
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Executer.cs

    r1216 r1217  
    4141namespace HeuristicLab.CEDMA.Server {
    4242  public class Executer {
    43     private Dispatcher dispatcher;
     43    private IDispatcher dispatcher;
    4444    private JobManager jobManager;
    4545    private IStore store;
     
    6363    }
    6464
    65     public Executer(Dispatcher dispatcher, IStore store, string gridUrl) {
     65    public Executer(IDispatcher dispatcher, IStore store, string gridUrl) {
    6666      activeExecutions = new Dictionary<WaitHandle, Execution>();
    6767      maxActiveJobs = 10;
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/HeuristicLab.CEDMA.Server.csproj

    r1060 r1217  
    7575  </ItemGroup>
    7676  <ItemGroup>
     77    <Compile Include="DispatcherBase.cs" />
     78    <Compile Include="IDispatcher.cs" />
    7779    <Compile Include="Execution.cs" />
    7880    <Compile Include="Executer.cs" />
    79     <Compile Include="Dispatcher.cs" />
     81    <Compile Include="RandomDispatcher.cs" />
    8082    <Compile Include="Server.cs" />
    8183    <Compile Include="ServerApplication.cs" />
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/RandomDispatcher.cs

    r1216 r1217  
    3737
    3838namespace HeuristicLab.CEDMA.Server {
    39   public class Dispatcher {
    40     private List<Execution> dispatchQueue;
    41     public IList<string> DispatchQueue {
    42       get { return dispatchQueue.Select(t => "StandardGP").ToList(); }
     39  public class RandomDispatcher : DispatcherBase {
     40    private Random random;
     41    public RandomDispatcher(IStore store)
     42      : base(store) {
     43      random = new Random();
    4344    }
    4445
    45     private IStore store;
    46 
    47     public Dispatcher(IStore store) {
    48       this.store = store;
    49       this.dispatchQueue = new List<Execution>();
     46    public override Algorithm SelectAlgorithm(DataSet dataSet, int targetVariable, Algorithm[] possibleAlgorithms) {
     47      return possibleAlgorithms[random.Next(possibleAlgorithms.Length)];
    5048    }
    5149
    52     private void FillDispatchQueue() {
    53       // find and select a dataset
    54       var dataSetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("DataSet");
    55       var dataSetQuery = new Statement[] {
    56         new Statement(dataSetVar, Ontology.PredicateInstanceOf, Ontology.TypeDataSet)
    57       };
    58 
    59       var dataSetBindings = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> .");
    60 
    61       // no datasets => do nothing
    62       if (dataSetBindings.Count() == 0) return;
    63 
    64       // assume last dataset is the most interesting one
    65       // find and select all results for this dataset
    66       var dataSetEntity = (Entity)dataSetBindings.Last().Get("DataSet");
    67       DataSet dataSet = new DataSet(store, dataSetEntity);
    68       Random random = new Random();
    69       int targetVariable = dataSet.Problem.AllowedInputVariables[random.Next(0, dataSet.Problem.AllowedInputVariables.Count)];
    70       string targetVariableName = dataSet.Problem.GetVariableName(targetVariable);
    71       Execution exec = CreateExecution(dataSet.Problem, targetVariable);
    72       if (exec != null) {
    73         exec.DataSetEntity = dataSetEntity;
    74         exec.TargetVariable = targetVariableName;
    75         QueueJob(exec);
    76       }
     50    public override ModelComplexity SelectComplexity(DataSet dataSet, int targetVariable, Algorithm algorithm, ModelComplexity[] possibleComplexities) {
     51      return possibleComplexities[random.Next(possibleComplexities.Length)];
    7752    }
    7853
    79     private void QueueJob(Execution execution) {
    80       dispatchQueue.Add(execution);
     54    public override Entity SelectDataSet(Entity[] datasets) {
     55      return datasets[random.Next(datasets.Length)];
    8156    }
    8257
    83     public Execution GetNextJob() {
    84       if (dispatchQueue.Count == 0) {
    85         FillDispatchQueue();
    86       }
    87       if (dispatchQueue.Count > 0) {
    88         Execution next = dispatchQueue[0];
    89         dispatchQueue.RemoveAt(0);
    90         return next;
    91       } else
    92         return null;
    93     }
    94 
    95     internal void Start() {
    96       FillDispatchQueue();
    97     }
    98 
    99     private Execution CreateExecution(Problem problem, int targetVariable) {
    100       switch (problem.LearningTask) {
    101         case LearningTask.Classification: return null;
    102         case LearningTask.Regression: {
    103             return CreateStandardGpExecution(problem, targetVariable, 100, 10);
    104           }
    105         case LearningTask.TimeSeries: return null;
    106         case LearningTask.Clustering: return null;
    107         default: return null;
    108       }
    109     }
    110 
    111     private Execution CreateStandardGpExecution(Problem problem, int targetVariable, int maxTreeSize, int maxTreeHeight) {
    112       ProblemInjector probInjector = new ProblemInjector(problem);
    113       probInjector.TargetVariable = targetVariable;
    114       StandardGP sgp = new StandardGP();
    115       sgp.SetSeedRandomly = true;
    116       sgp.MaxGenerations = 2;
    117       sgp.PopulationSize = 100;
    118       sgp.Elites = 1;
    119       sgp.ProblemInjector = probInjector;
    120       sgp.MaxTreeHeight = maxTreeHeight;
    121       sgp.MaxTreeSize = maxTreeSize;
    122       Execution exec = new Execution(sgp.Engine);
    123       exec.Description = "StandardGP - Medium Complexity";
    124       return exec;
     58    public override int SelectTargetVariable(DataSet dataSet, int[] targetVariables) {
     59      return targetVariables[random.Next(targetVariables.Length)];
    12560    }
    12661  }
  • branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/ServerForm.cs

    r1216 r1217  
    4444    private Server server;
    4545    private Store store;
    46     private Dispatcher dispatcher;
     46    private IDispatcher dispatcher;
    4747    private Executer executer;
    4848
     
    6363
    6464    private void connectButton_Click(object sender, EventArgs e) {
    65       dispatcher = new Dispatcher(store);
    66       dispatcher.Start();
     65      dispatcher = new RandomDispatcher(store);
    6766      executer = new Executer(dispatcher, store, gridAddress.Text);
    6867      executer.Start();
Note: See TracChangeset for help on using the changeset viewer.