Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/AgentScheduler.cs @ 393

Last change on this file since 393 was 393, checked in by gkronber, 16 years ago

worked on very basic control to view the tree of agents/runs (ticket #188)

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.CEDMA.DB;
27using HeuristicLab.CEDMA.DB.Interfaces;
28using HeuristicLab.Core;
29using System.Threading;
30using HeuristicLab.CEDMA.Core;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.CEDMA.Server {
34  public class AgentScheduler {
35    private Database database;
36    private List<IEngine> engines;
37    private Dictionary<IEngine, AgentEntry> agent;
38    private string serverUri;
39
40    public AgentScheduler(Database database, string serverUri) {
41      this.database = database;
42      this.serverUri = serverUri;
43      engines = new List<IEngine>();
44      agent = new Dictionary<IEngine, AgentEntry>();
45    }
46
47    public void Run() {
48      while(true) {
49        ClearFinishedEngines();
50        CreateNewEngines();
51        if(engines.Count == 0) Thread.Sleep(10000);
52        else StepAllEngines();
53      }
54    }
55    private void ClearFinishedEngines() {
56      List<IEngine> finishedEngines = new List<IEngine>();
57      foreach(IEngine e in engines) {
58        if(e.Terminated) {
59          finishedEngines.Add(e);
60        }
61      }
62      foreach(IEngine e in finishedEngines) {
63        engines.Remove(e);
64        AgentEntry entry = agent[e];
65        entry.Status = ProcessStatus.Finished;
66        database.UpdateAgent(entry.Id, entry.Status);
67      }
68    }
69    private void CreateNewEngines() {
70      IEnumerable<AgentEntry> agents = database.GetAgents(ProcessStatus.Waiting).Where(a=>a.ControllerAgent);
71      foreach(AgentEntry a in agents) {
72        SequentialEngine.SequentialEngine engine = new HeuristicLab.SequentialEngine.SequentialEngine();
73        Agent newAgent = (Agent)DbPersistenceManager.Restore(a.RawData);
74        engine.OperatorGraph.Clear();
75        foreach(IOperator op in newAgent.OperatorGraph.Operators) engine.OperatorGraph.AddOperator(op);
76        engine.OperatorGraph.InitialOperator = newAgent.OperatorGraph.InitialOperator;
77        engine.Reset();
78
79        // initialize CEDMA variables for the execution of the agent
80        engine.GlobalScope.AddVariable(new Variable("AgentId", new IntData((int)a.Id)));
81        engine.GlobalScope.AddVariable(new Variable("CedmaServerUri", new StringData(serverUri)));
82
83        agent[engine] = a;
84        engines.Add(engine);
85        a.Status = ProcessStatus.Active;
86        database.UpdateAgent(a.Id, a.Status);
87      }
88    }
89    private void StepAllEngines() {
90      for(int steps = 0; steps < 100; steps++) {
91        foreach(IEngine engine in engines) {
92          engine.ExecuteStep();
93        }
94        Thread.Sleep(100); // prevent overload
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.