[13556] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using System.Threading;
|
---|
| 4 | using System.Threading.Tasks;
|
---|
| 5 | using DistributedGA.Core;
|
---|
| 6 | using DistributedGA.Core.Domain;
|
---|
| 7 | using DistributedGA.Core.Implementation;
|
---|
| 8 | using DistributedGA.Core.Interface;
|
---|
| 9 | using HeuristicLab.Common;
|
---|
| 10 | using HeuristicLab.Core;
|
---|
| 11 | using HeuristicLab.Data;
|
---|
| 12 | using HeuristicLab.Optimization;
|
---|
| 13 | using HeuristicLab.Parameters;
|
---|
| 14 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 15 |
|
---|
| 16 | namespace DistributedGA.Hive {
|
---|
| 17 | [StorableClass]
|
---|
| 18 | [Creatable(Category = "Haslinger's Very Special XO", Priority = 1000)]
|
---|
| 19 | public class P2PTask : ParameterizedNamedItem, IOptimizer {
|
---|
| 20 |
|
---|
| 21 | [Storable]
|
---|
| 22 | private Log log;
|
---|
| 23 | [Storable]
|
---|
| 24 | private DateTime startTime;
|
---|
| 25 | [Storable]
|
---|
| 26 | private RunCollection runCollection;
|
---|
| 27 |
|
---|
| 28 | #region Constructors and Cloning
|
---|
| 29 | [StorableConstructor]
|
---|
| 30 | protected P2PTask(bool deserializing) { }
|
---|
| 31 | protected P2PTask(P2PTask original, Cloner cloner)
|
---|
| 32 | : base(original, cloner) {
|
---|
| 33 | log = cloner.Clone(original.log);
|
---|
| 34 | startTime = original.startTime;
|
---|
| 35 | runCollection = cloner.Clone(original.runCollection);
|
---|
| 36 | }
|
---|
| 37 | public P2PTask() {
|
---|
| 38 | Name = "P2PTask";
|
---|
| 39 | Description = "P2PTask";
|
---|
| 40 | runCollection = new RunCollection();
|
---|
| 41 |
|
---|
| 42 | Parameters.Add(new ValueParameter<StringValue>("LanIpPrefix", "", new StringValue("10.")));
|
---|
| 43 | Parameters.Add(new ValueParameter<StringValue>("ContactServerURL", "", new StringValue("http://10.42.1.150:9090/DistributedGA.ContactServer/ContactService")));
|
---|
| 44 |
|
---|
| 45 | log = new Log();
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 49 | protected virtual void AfterDeserialization() {
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new P2PTask(this, cloner);
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region ITask Members
|
---|
| 58 | public ExecutionState ExecutionState { get; private set; }
|
---|
| 59 |
|
---|
| 60 | public TimeSpan ExecutionTime { get; private set; }
|
---|
| 61 |
|
---|
| 62 | public void Prepare() {
|
---|
| 63 | Prepare(true);
|
---|
| 64 | }
|
---|
| 65 | public void Prepare(bool clearRuns) {
|
---|
| 66 | // ignore
|
---|
| 67 | ExecutionState = HeuristicLab.Core.ExecutionState.Prepared;
|
---|
| 68 | OnExecutionStateChanged();
|
---|
| 69 | OnPrepared();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | private CancellationTokenSource cts;
|
---|
| 73 | private ManualResetEvent stoppedEvent;
|
---|
| 74 |
|
---|
| 75 | public void Start() {
|
---|
| 76 | Task.Factory.StartNew(() => {
|
---|
| 77 | cts = new CancellationTokenSource();
|
---|
| 78 | stoppedEvent = new ManualResetEvent(false);
|
---|
| 79 | startTime = DateTime.Now;
|
---|
| 80 |
|
---|
| 81 | ExecutionState = HeuristicLab.Core.ExecutionState.Started;
|
---|
| 82 | OnExecutionStateChanged();
|
---|
| 83 | OnStarted();
|
---|
| 84 |
|
---|
| 85 | try {
|
---|
| 86 | log.LogMessage("Starting peer...");
|
---|
| 87 | IMessageHandler h = new PeerNetworkMessageHandler();
|
---|
| 88 | var lanIpPrefix = ((StringValue)(Parameters["LanIpPrefix"].ActualValue)).Value;
|
---|
| 89 | var contactServerUri = ((StringValue)(Parameters["ContactServerURL"].ActualValue)).Value;
|
---|
| 90 | h.Init(lanIpPrefix, contactServerUri);
|
---|
| 91 | PeerInfo pi = h.GetPeerInfo();
|
---|
| 92 | log.LogMessage(string.Format("Peer is hostet at IP: {0} and port: {1}", pi.IpAddress, pi.Port));
|
---|
| 93 | Thread.Sleep(1000 * 20);
|
---|
| 94 | log.LogMessage("Current peers within network:");
|
---|
| 95 | foreach (var item in h.GetCurrentNetwork()) {
|
---|
| 96 | log.LogMessage(string.Format("Peer at {0}:{1}", item.IpAddress, item.Port));
|
---|
| 97 | }
|
---|
| 98 | int i = 1;
|
---|
| 99 | while (i < 10 && !cts.Token.IsCancellationRequested) {
|
---|
| 100 | i++;
|
---|
| 101 | Thread.Sleep(1000 * 10);
|
---|
| 102 | var pop1 = CreatePopulation();
|
---|
| 103 | log.LogMessage("Publish population into network...");
|
---|
| 104 | h.PublishDataToNetwork(pop1);
|
---|
| 105 | log.LogMessage("Population published.");
|
---|
| 106 | log.LogMessage("Recieved populations:");
|
---|
| 107 | foreach (var item in h.GetDataFromNetwork()) {
|
---|
| 108 | log.LogMessage(string.Format("Population with Quality: {0:2} in Iteration {1}", item.Quality, item.IterationNumber));
|
---|
| 109 | }
|
---|
| 110 | ExecutionTime = DateTime.Now - startTime;
|
---|
| 111 | OnExecutionTimeChanged();
|
---|
| 112 | }
|
---|
| 113 | } catch (Exception ex) {
|
---|
| 114 | log.LogMessage(ex.Message);
|
---|
| 115 | log.LogMessage("press any key to continue...");
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | var run = new Run();
|
---|
| 119 | var results = new ResultCollection();
|
---|
| 120 |
|
---|
| 121 | run.Results.Add("Execution Time", new TimeSpanValue(ExecutionTime));
|
---|
| 122 | run.Results.Add("Log", log);
|
---|
| 123 |
|
---|
| 124 | runCollection.Add(run);
|
---|
| 125 |
|
---|
| 126 | stoppedEvent.Set();
|
---|
| 127 | ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
|
---|
| 128 |
|
---|
| 129 | OnExecutionStateChanged();
|
---|
| 130 | OnStopped();
|
---|
| 131 | });
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | public void Pause() {
|
---|
| 136 | if (cts != null) cts.Cancel();
|
---|
| 137 | stoppedEvent.WaitOne();
|
---|
| 138 |
|
---|
| 139 | ExecutionState = HeuristicLab.Core.ExecutionState.Paused;
|
---|
| 140 | OnExecutionStateChanged();
|
---|
| 141 |
|
---|
| 142 | OnPaused();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public void Stop() {
|
---|
| 146 | if (cts != null) cts.Cancel();
|
---|
| 147 | stoppedEvent.WaitOne();
|
---|
| 148 |
|
---|
| 149 | ExecutionState = HeuristicLab.Core.ExecutionState.Stopped;
|
---|
| 150 | OnExecutionStateChanged();
|
---|
| 151 |
|
---|
| 152 | OnStopped();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public event EventHandler Started;
|
---|
| 156 | protected virtual void OnStarted() {
|
---|
| 157 | EventHandler handler = Started;
|
---|
| 158 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | public event EventHandler Stopped;
|
---|
| 162 | protected virtual void OnStopped() {
|
---|
| 163 | EventHandler handler = Stopped;
|
---|
| 164 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | public event EventHandler Paused;
|
---|
| 168 | protected virtual void OnPaused() {
|
---|
| 169 | EventHandler handler = Paused;
|
---|
| 170 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | public event EventHandler Prepared;
|
---|
| 174 | protected virtual void OnPrepared() {
|
---|
| 175 | EventHandler handler = Prepared;
|
---|
| 176 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | public event EventHandler ComputeInParallelChanged;
|
---|
| 181 | protected virtual void OnComputeInParallelChanged() {
|
---|
| 182 | EventHandler handler = ComputeInParallelChanged;
|
---|
| 183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 184 | }
|
---|
| 185 | #endregion
|
---|
| 186 |
|
---|
| 187 | #region Events
|
---|
| 188 | public event EventHandler ExecutionTimeChanged;
|
---|
| 189 | protected virtual void OnExecutionTimeChanged() {
|
---|
| 190 | EventHandler handler = ExecutionTimeChanged;
|
---|
| 191 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 192 | }
|
---|
| 193 | public event EventHandler ExecutionStateChanged;
|
---|
| 194 | protected virtual void OnExecutionStateChanged() {
|
---|
| 195 | EventHandler handler = ExecutionStateChanged;
|
---|
| 196 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 197 | }
|
---|
| 198 | #endregion
|
---|
| 199 |
|
---|
| 200 | public override string ToString() {
|
---|
| 201 | return Name;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | private static SolutionInfo[] CreatePopulation() {
|
---|
| 207 | SolutionInfo si1 = new SolutionInfo() {
|
---|
| 208 | IterationNumber = 1,
|
---|
| 209 | Quality = 3.5f,
|
---|
| 210 | Solution = new Solution() {
|
---|
| 211 | }
|
---|
| 212 | };
|
---|
| 213 | SolutionInfo si2 = new SolutionInfo() {
|
---|
| 214 | IterationNumber = 2,
|
---|
| 215 | Quality = 3.5f,
|
---|
| 216 | Solution = new Solution() {
|
---|
| 217 | }
|
---|
| 218 | };
|
---|
| 219 | SolutionInfo si3 = new SolutionInfo() {
|
---|
| 220 | IterationNumber = 3,
|
---|
| 221 | Quality = 3.5f,
|
---|
| 222 | Solution = new Solution() {
|
---|
| 223 | }
|
---|
| 224 | };
|
---|
| 225 | return new SolutionInfo[] { si1, si2, si3 };
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public System.Collections.Generic.IEnumerable<IOptimizer> NestedOptimizers {
|
---|
| 229 | get { return Enumerable.Empty<IOptimizer>(); }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | public RunCollection Runs {
|
---|
| 233 | get { return runCollection; }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
| 237 |
|
---|
| 238 | }
|
---|
| 239 | }
|
---|