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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.CEDMA.DB;
|
---|
27 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using System.Threading;
|
---|
30 | using HeuristicLab.CEDMA.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 |
|
---|
33 | namespace 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 | }
|
---|