Esempio n. 1
0
        protected Algorithm(int populationSize,
                            StopCondition <TAlgorithm, TProblem, TSolution> stopCondition,
                            MainOperator <TAlgorithm, TProblem, TSolution> mainOperator)
        {
            Assert(populationSize > 0);

            Stopwatch    = new Stopwatch();
            RunStatistic = new RunStatistic();

            Problem = null;

            Population = new TSolution[populationSize];
            for (int i = 0; i < Population.Length; i++)
            {
                Population[i] = new TSolution();
            }
            Best = new TSolution
            {
                Fitness = double.MinValue
            };

            Random = null;

            StopCondition = stopCondition.DeepClone();
            MainOperator  = mainOperator.DeepClone();
        }
 public IterativeAlgorithm(int populationSize,
                           StopCondition <IterativeAlgorithm <TProblem, TSolution>, TProblem, TSolution>
                           stopCondition,
                           MainOperator <IterativeAlgorithm <TProblem, TSolution>, TProblem, TSolution>
                           mainOperator,
                           NewGenerationCreator <IterativeAlgorithm <TProblem, TSolution>, TProblem, TSolution>
                           newGenerationCreator)
     : base(populationSize, stopCondition, mainOperator)
 {
     NewGenerationCreator = newGenerationCreator.DeepClone();
 }
        public override void Run()
        {
            Stopwatch.Restart();

            MainOperator.Initialize(Population);
            UpdateBest();

            while (StopCondition.GetResult() == StopResult.NotStop)
            {
                var newGeneration = MainOperator.Apply(Population);
                Population = NewGenerationCreator.Create(Population, newGeneration);
                UpdateBest();
            }

            Stopwatch.Stop();
        }
Esempio n. 4
0
        public bool Broadcast <T>(T message) where T : class
        {
            IActorMessage msg         = null;
            var           handlesType = typeof(T);

            if (!(message is IActorMessage))
            {
                msg = new SimpleMessage <T>(message);
            }
            else
            {
                msg = (message as IActorMessage);
            }

            if ((msg is ControlMessages.HangUp))
            {
                foreach (var t in msgTypeToOperator)
                {
                    foreach (var q in t.Value)
                    {
                        q.OnMessageRecieved(msg);
                    }
                }
                return(true);
            }

            if (!msgTypeToOperator.ContainsKey(handlesType))
            {
                if (!IsUsingSingleOperator())
                {
                    throw new NoOperatorRegisteredToSupportTypeException();
                }

                return(MainOperator.OnMessageRecieved(msg));
            }

            bool recieved = true;

            for (int i = msgTypeToOperator[handlesType].Count - 1; i >= 0 && i < msgTypeToOperator[handlesType].Count; --i)
            {
                var t = msgTypeToOperator[handlesType].ElementAt(i);
                recieved &= t.OnMessageRecieved(msg);
            }

            return(recieved);
        }
Esempio n. 5
0
 public void test()
 {
     MainOperator.doClick(5);
 }
Esempio n. 6
0
        public bool OnMessageRecieved <T>(T msg) where T : class, IActorMessage
        {
            if (msg is ControlMessages.HangUp)
            {
                Broadcast(msg);
                UnRegisterAll();
                msg.Status?.TrySetResult(msg);
                return(true);
            }

            if (IsUsingSingleOperator())
            {
                return(MainOperator.OnMessageRecieved <T>(msg));
            }

            IOperator op = null;

            switch (this.MessageDispatchProcedure)
            {
            case MessageDispatchProcedureType.RandomSelection:
            {
                var handlesType = LocalSwitchboard.HandleValueTypeMessage(typeof(T));

                if (!msgTypeToOperator.ContainsKey(handlesType))
                {
                    throw new NoOperatorRegisteredToSupportTypeException(handlesType.ToString());
                }

                int nextIndex = rand.Next(0, msgTypeToOperator[handlesType].Count - 1);

                op = msgTypeToOperator[handlesType].ElementAt(nextIndex);
            }
            break;

            case MessageDispatchProcedureType.BroadcastToAll:
                return(Broadcast <T>(msg));

            case MessageDispatchProcedureType.RoundRobin:
            {
                var handlesType = msg.GetType();

                if (!msgTypeToOperator.ContainsKey(handlesType))
                {
                    throw new NoOperatorRegisteredToSupportTypeException(handlesType.ToString());
                }
                if (((ConcurrentQueue <IOperator>)msgTypeToOperator[handlesType]).Count == 1)
                {
                    if (!((ConcurrentQueue <IOperator>)msgTypeToOperator[handlesType]).TryPeek(out op))
                    {
                        throw new CouldNotRetrieveNextOperatorFromQueueException();
                    }
                }
                else
                {
                    if (!((ConcurrentQueue <IOperator>)msgTypeToOperator[handlesType]).TryDequeue(out op))
                    {
                        throw new CouldNotRetrieveNextOperatorFromQueueException();
                    }

                    //Re-Add the operator to the bottom of the queue.
                    ((ConcurrentQueue <IOperator>)msgTypeToOperator[handlesType]).Enqueue(op);
                }
            }
            break;

            case MessageDispatchProcedureType.LeastBusy:
            {
                var handlesType = LocalSwitchboard.HandleValueTypeMessage(typeof(T));

                if (!msgTypeToOperator.ContainsKey(handlesType))
                {
                    throw new NoOperatorRegisteredToSupportTypeException(handlesType.ToString());
                }

                ulong leastCount = ulong.MaxValue;
                for (int i = msgTypeToOperator[handlesType].Count - 1; i >= 0 && i < msgTypeToOperator[handlesType].Count; --i)
                {
                    IOperator tempOp = msgTypeToOperator[handlesType].ElementAt(i);

                    if (leastCount > tempOp.Count)
                    {
                        leastCount = tempOp.Count;
                        op         = tempOp;
                    }

                    if (0 == tempOp.Count)
                    {
                        break;
                    }
                }
            }
            break;

            default:
                throw new UnsupportedDispatchImplementationException();
            }

            return(op.OnMessageRecieved <T>(msg));
        }