- Timestamp:
- 02/12/09 14:05:33 (16 years ago)
- Location:
- branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Dispatcher.cs
r1151 r1216 57 57 }; 58 58 59 var dataSetBindings = store.Query("?DataSet <" +Ontology.PredicateInstanceOf.Uri+"> <"+Ontology.TypeDataSet.Uri+"> .");59 var dataSetBindings = store.Query("?DataSet <" + Ontology.PredicateInstanceOf.Uri + "> <" + Ontology.TypeDataSet.Uri + "> ."); 60 60 61 61 // no datasets => do nothing … … 65 65 // find and select all results for this dataset 66 66 var dataSetEntity = (Entity)dataSetBindings.Last().Get("DataSet"); 67 var targetVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("TargetVariable");68 var modelVar = new HeuristicLab.CEDMA.DB.Interfaces.Variable("Model");69 var modelMAPE = new HeuristicLab.CEDMA.DB.Interfaces.Variable("ModelMAPE");70 71 var query = "<" + dataSetEntity.Uri + "> <" + Ontology.PredicateHasModel.Uri + "> ?Model ." + Environment.NewLine +72 "?Model <" + Ontology.TargetVariable.Uri + "> ?TargetVariable ." + Environment.NewLine +73 "?Model <" + Ontology.ValidationMeanAbsolutePercentageError.Uri + "> ?ModelMAPE .";74 75 76 77 var bindings = store.Query(query);78 67 DataSet dataSet = new DataSet(store, dataSetEntity); 79 double[] utilization = new double[dataSet.Problem.AllowedTargetVariables.Count]; 80 int i = 0; 81 int totalN = bindings.Count(); 82 foreach (int targetVariable in dataSet.Problem.AllowedTargetVariables) { 83 var targetVarBindings = bindings.Where(x => (int)((Literal)x.Get("TargetVariable")).Value == targetVariable); 84 if (targetVarBindings.Count() == 0) { 85 utilization[i++] = double.PositiveInfinity; 86 } else { 87 double averageMape = targetVarBindings.Average(x => (double)((Literal)x.Get("ModelMAPE")).Value); 88 double n = targetVarBindings.Count(); 89 utilization[i++] = -averageMape + Math.Sqrt(Math.Log(totalN) / n) * 0.1; 90 } 91 } 92 int[] idx = Enumerable.Range(0, utilization.Length).ToArray(); 93 Array.Sort(utilization, idx); 94 int nConfigurations = utilization.Length; 95 for (int j = nConfigurations - 1; j > nConfigurations * 0.8; j--) { 96 int targetVariable = dataSet.Problem.AllowedTargetVariables[idx[j]]; 97 IEngine engine = CreateEngine(dataSet.Problem, targetVariable); 98 if (engine != null) { 99 QueueJob(new Execution(dataSetEntity, engine, targetVariable)); 100 } 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); 101 76 } 102 77 } … … 122 97 } 123 98 124 private IEngine CreateEngine(Problem problem, int targetVariable) {99 private Execution CreateExecution(Problem problem, int targetVariable) { 125 100 switch (problem.LearningTask) { 126 101 case LearningTask.Classification: return null; 127 102 case LearningTask.Regression: { 128 return CreateStandardGp (problem, targetVariable).Engine;103 return CreateStandardGpExecution(problem, targetVariable, 100, 10); 129 104 } 130 105 case LearningTask.TimeSeries: return null; … … 134 109 } 135 110 136 private StandardGP CreateStandardGp(Problem problem, int targetVariable) {111 private Execution CreateStandardGpExecution(Problem problem, int targetVariable, int maxTreeSize, int maxTreeHeight) { 137 112 ProblemInjector probInjector = new ProblemInjector(problem); 138 113 probInjector.TargetVariable = targetVariable; 139 114 StandardGP sgp = new StandardGP(); 140 115 sgp.SetSeedRandomly = true; 141 sgp.MaxGenerations = 300;142 sgp.PopulationSize = 100 00;116 sgp.MaxGenerations = 2; 117 sgp.PopulationSize = 100; 143 118 sgp.Elites = 1; 144 119 sgp.ProblemInjector = probInjector; 145 return sgp; 120 sgp.MaxTreeHeight = maxTreeHeight; 121 sgp.MaxTreeSize = maxTreeSize; 122 Execution exec = new Execution(sgp.Engine); 123 exec.Description = "StandardGP - Medium Complexity"; 124 return exec; 146 125 } 147 126 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Executer.cs
r1156 r1216 41 41 namespace HeuristicLab.CEDMA.Server { 42 42 public class Executer { 43 private static int MaxActiveJobs {44 get { return 100; }45 }46 43 private Dispatcher dispatcher; 47 44 private JobManager jobManager; 48 45 private IStore store; 46 private Dictionary<WaitHandle, Execution> activeExecutions; 47 48 private TimeSpan StartJobInterval { 49 get { return TimeSpan.FromMilliseconds(500); } 50 } 51 52 private TimeSpan WaitForFinishedJobsTimeout { 53 get { return TimeSpan.FromMilliseconds(100); } 54 } 55 56 private int maxActiveJobs; 57 public int MaxActiveJobs { 58 get { return maxActiveJobs; } 59 set { 60 if (value < 0) throw new ArgumentException("Only positive values are allowed for MaxActiveJobs"); 61 maxActiveJobs = value; 62 } 63 } 49 64 50 65 public Executer(Dispatcher dispatcher, IStore store, string gridUrl) { 66 activeExecutions = new Dictionary<WaitHandle, Execution>(); 67 maxActiveJobs = 10; 51 68 this.dispatcher = dispatcher; 52 69 this.store = store; … … 61 78 private void StartJobs() { 62 79 List<WaitHandle> wh = new List<WaitHandle>(); 63 Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle,AtomicOperation>(); 64 Dictionary<WaitHandle, Execution> activeExecutions = new Dictionary<WaitHandle,Execution>(); 80 Dictionary<WaitHandle, AtomicOperation> activeOperations = new Dictionary<WaitHandle, AtomicOperation>(); 65 81 while (true) { 66 82 try { 67 83 // start new jobs as long as there are less than MaxActiveJobs 68 84 while (wh.Count < MaxActiveJobs) { 85 Thread.Sleep(StartJobInterval); 69 86 // get an execution from the dispatcher and execute in grid via job-manager 70 87 Execution execution = dispatcher.GetNextJob(); … … 74 91 wh.Add(opWh); 75 92 activeOperations.Add(opWh, op); 76 activeExecutions.Add(opWh, execution); 93 lock (activeExecutions) { 94 activeExecutions.Add(opWh, execution); 95 } 77 96 } 78 97 } 79 98 // wait until any job is finished 80 99 WaitHandle[] whArr = wh.ToArray(); 81 int readyHandleIndex = WaitHandle.WaitAny(whArr); 82 WaitHandle readyHandle = whArr[readyHandleIndex]; 83 AtomicOperation finishedOp = activeOperations[readyHandle]; 84 Execution finishedExecution = activeExecutions[readyHandle]; 85 wh.Remove(readyHandle); 86 activeExecutions.Remove(readyHandle); 87 activeOperations.Remove(readyHandle); 88 ProcessingEngine finishedEngine = null; 89 try { 90 finishedEngine = jobManager.EndExecuteOperation(finishedOp); 91 } catch (Exception badEx) { 92 Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message); 93 } 94 if (finishedEngine != null) { 95 StoreResults(finishedExecution, finishedEngine); 100 int readyHandleIndex = WaitHandle.WaitAny(whArr, WaitForFinishedJobsTimeout); 101 if (readyHandleIndex != WaitHandle.WaitTimeout) { 102 WaitHandle readyHandle = whArr[readyHandleIndex]; 103 AtomicOperation finishedOp = activeOperations[readyHandle]; 104 wh.Remove(readyHandle); 105 Execution finishedExecution = null; 106 lock (activeExecutions) { 107 finishedExecution = activeExecutions[readyHandle]; 108 activeExecutions.Remove(readyHandle); 109 } 110 activeOperations.Remove(readyHandle); 111 ProcessingEngine finishedEngine = null; 112 try { 113 finishedEngine = jobManager.EndExecuteOperation(finishedOp); 114 } 115 catch (Exception badEx) { 116 Trace.WriteLine("CEDMA Executer: Exception in job execution thread. " + badEx.Message); 117 } 118 if (finishedEngine != null) { 119 StoreResults(finishedExecution, finishedEngine); 120 } 96 121 } 97 122 } … … 115 140 StoreModelAttribute(model, Ontology.TreeHeight, bestModelScope.GetVariableValue<IntData>("TreeHeight", false).Data); 116 141 StoreModelAttribute(model, Ontology.EvaluatedSolutions, bestModelScope.GetVariableValue<IntData>("EvaluatedSolutions", false).Data); 117 142 118 143 byte[] serializedModel = PersistenceManager.SaveToGZip(bestModelScope.GetVariableValue("FunctionTree", false)); 119 144 store.Add(new Statement(model, Ontology.PredicateSerializedData, new Literal(Convert.ToBase64String(serializedModel)))); 120 }145 } 121 146 122 147 private void StoreModelAttribute(Entity model, Entity predicate, object value) { 123 148 store.Add(new Statement(model, predicate, new Literal(value))); 124 149 } 150 151 internal string[] GetJobs() { 152 lock (activeExecutions) { 153 string[] retVal = new string[activeExecutions.Count]; 154 int i = 0; 155 foreach (Execution e in activeExecutions.Values) { 156 retVal[i++] = "Target-Variable: "+e.TargetVariable+" "+e.Description; 157 } 158 return retVal; 159 } 160 } 125 161 } 126 162 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Execution.cs
r1214 r1216 59 59 } 60 60 61 public Execution(Entity dataSetEntity, IEngine engine, string targetVariable) { 62 this.dataSetEntity = dataSetEntity; 61 private string description; 62 public string Description { 63 get { return description; } 64 set { description = value; } 65 } 66 67 public Execution(IEngine engine) { 63 68 this.engine = engine; 64 this.targetVariable = targetVariable;65 69 } 66 70 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/ServerForm.cs
r1060 r1216 49 49 private static readonly string rdfFile = AppDomain.CurrentDomain.BaseDirectory + "rdf_store.db3"; 50 50 private static readonly string rdfConnectionString = "sqlite:rdf:Data Source=\"" + rdfFile + "\""; 51 51 52 52 public ServerForm() { 53 53 InitializeComponent(); … … 56 56 server.Start(); 57 57 addressTextBox.Text = server.CedmaServiceUrl; 58 dispatcher = new Dispatcher(store);59 dispatcher.Start();60 58 } 61 59 62 60 private void refreshTimer_Tick(object sender, EventArgs e) { 63 listBox.DataSource = dispatcher.DispatchQueue;61 listBox.DataSource = executer.GetJobs(); 64 62 } 65 63 66 64 private void connectButton_Click(object sender, EventArgs e) { 65 dispatcher = new Dispatcher(store); 66 dispatcher.Start(); 67 67 executer = new Executer(dispatcher, store, gridAddress.Text); 68 68 executer.Start(); 69 maxActiveJobsUpDown.Enabled = true; 70 maxActiveJobsUpDown.Value = executer.MaxActiveJobs; 69 71 connectButton.Enabled = false; 72 refreshTimer.Start(); 73 } 74 75 private void maxActiveJobsUpDown_ValueChanged(object sender, EventArgs e) { 76 executer.MaxActiveJobs = Convert.ToInt32(maxActiveJobsUpDown.Value); 70 77 } 71 78 } -
branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/ServerForm.designer.cs
r1044 r1216 53 53 this.listBox = new System.Windows.Forms.ListBox(); 54 54 this.refreshTimer = new System.Windows.Forms.Timer(this.components); 55 this.maxActiveJobsUpDown = new System.Windows.Forms.NumericUpDown(); 56 this.label1 = new System.Windows.Forms.Label(); 57 ((System.ComponentModel.ISupportInitialize)(this.maxActiveJobsUpDown)).BeginInit(); 55 58 this.SuspendLayout(); 56 59 // … … 104 107 | System.Windows.Forms.AnchorStyles.Right))); 105 108 this.listBox.FormattingEnabled = true; 106 this.listBox.Location = new System.Drawing.Point(12, 58);109 this.listBox.Location = new System.Drawing.Point(12, 84); 107 110 this.listBox.Name = "listBox"; 108 this.listBox.Size = new System.Drawing.Size(350, 2 77);111 this.listBox.Size = new System.Drawing.Size(350, 251); 109 112 this.listBox.TabIndex = 11; 110 113 // 111 114 // refreshTimer 112 115 // 113 this.refreshTimer.Enabled = true;114 116 this.refreshTimer.Interval = 1000; 115 117 this.refreshTimer.Tick += new System.EventHandler(this.refreshTimer_Tick); 118 // 119 // maxActiveJobsUpDown 120 // 121 this.maxActiveJobsUpDown.Enabled = false; 122 this.maxActiveJobsUpDown.Location = new System.Drawing.Point(106, 59); 123 this.maxActiveJobsUpDown.Maximum = new decimal(new int[] { 124 64, 125 0, 126 0, 127 0}); 128 this.maxActiveJobsUpDown.Name = "maxActiveJobsUpDown"; 129 this.maxActiveJobsUpDown.Size = new System.Drawing.Size(120, 20); 130 this.maxActiveJobsUpDown.TabIndex = 12; 131 this.maxActiveJobsUpDown.ValueChanged += new System.EventHandler(this.maxActiveJobsUpDown_ValueChanged); 132 // 133 // label1 134 // 135 this.label1.AutoSize = true; 136 this.label1.Enabled = false; 137 this.label1.Location = new System.Drawing.Point(12, 61); 138 this.label1.Name = "label1"; 139 this.label1.Size = new System.Drawing.Size(84, 13); 140 this.label1.TabIndex = 13; 141 this.label1.Text = "&Max active jobs:"; 116 142 // 117 143 // ServerForm … … 120 146 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 121 147 this.ClientSize = new System.Drawing.Size(374, 342); 148 this.Controls.Add(this.label1); 149 this.Controls.Add(this.maxActiveJobsUpDown); 122 150 this.Controls.Add(this.listBox); 123 151 this.Controls.Add(this.connectButton); … … 128 156 this.Name = "ServerForm"; 129 157 this.Text = "CEDMA Server"; 158 ((System.ComponentModel.ISupportInitialize)(this.maxActiveJobsUpDown)).EndInit(); 130 159 this.ResumeLayout(false); 131 160 this.PerformLayout(); … … 142 171 private System.Windows.Forms.ListBox listBox; 143 172 private System.Windows.Forms.Timer refreshTimer; 173 private System.Windows.Forms.NumericUpDown maxActiveJobsUpDown; 174 private System.Windows.Forms.Label label1; 144 175 } 145 176 }
Note: See TracChangeset
for help on using the changeset viewer.