Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/18/14 03:25:58 (10 years ago)
Author:
swagner
Message:

#2205: Worked on optimization networks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmServiceNode.cs

    r11468 r11481  
    2525using System;
    2626using System.Collections.Generic;
     27using System.Linq;
    2728using System.Threading;
    28 using System.Linq;
    2929
    3030namespace HeuristicLab.Optimization.Networks {
     
    3434    private object locker = new object();
    3535    private Dictionary<IAlgorithm, AutoResetEvent> waitHandles = new Dictionary<IAlgorithm,AutoResetEvent>();
     36    private Dictionary<IAlgorithm, Exception> exceptions = new Dictionary<IAlgorithm, Exception>();
    3637
    3738    new public PortCollection Ports {
     
    118119      algorithm.Runs.Clear();
    119120      algorithm.Prepare();
     121      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    120122      algorithm.Stopped += Algorithm_Stopped;
    121       algorithm.Start();
    122       if (WaitHandle.WaitAny(new WaitHandle[] { waitHandles[algorithm], token.WaitHandle }) == 1) {
    123         // retrieve results
    124         foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) {
    125           IResult result = null;
    126           if (algorithm.Results.TryGetValue(p.Name, out result)) {
    127             p.Value = result.Value;
    128           }
    129         }
    130 
    131         lock (locker) {
    132           waitHandles[algorithm].Dispose();
    133           waitHandles.Remove(algorithm);
    134           Runs.Add(algorithm.Runs.ToArray()[0]);
    135         }
    136       } else {  // cancellation
    137         algorithm.Stop();
    138         lock (locker) {
    139           waitHandles[algorithm].Dispose();
    140           waitHandles.Remove(algorithm);
    141         }
    142       }
    143     }
    144 
     123
     124      using (token.Register(() => { algorithm.Stop(); })) {
     125        algorithm.Start();
     126        waitHandles[algorithm].WaitOne();
     127      }
     128
     129      lock (locker) {
     130        waitHandles[algorithm].Dispose();
     131        waitHandles.Remove(algorithm);
     132
     133        Exception ex = null;
     134        if (exceptions.TryGetValue(algorithm, out ex)) {
     135          exceptions.Remove(algorithm);
     136          throw ex;
     137        }
     138      }
     139
     140      // retrieve results
     141      var run = algorithm.Runs.First();
     142      foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) {
     143        IItem result = null;
     144        if (run.Results.TryGetValue(p.Name, out result)) {
     145          p.Value = result;
     146        }
     147      }
     148
     149      lock (locker) {
     150        Runs.Add(run);
     151      }
     152    }
     153
     154    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     155      var algorithm = (IAlgorithm)sender;
     156      lock (locker) {
     157        exceptions.Add(algorithm, e.Value);
     158      }
     159      algorithm.Stop();
     160    }
    145161    private void Algorithm_Stopped(object sender, EventArgs e) {
    146162      lock (locker) {
Note: See TracChangeset for help on using the changeset viewer.