Ejemplo n.º 1
0
        public ParticleState Run(CancellationToken token = new CancellationToken())
        {
            if (SyncWithCpu)
            {
                PushGpuState();
                PullCpuState();
            }
            var i = 0;

            try
            {
                for (i = 0; i < Iterations; i++)
                {
                    token.ThrowIfCancellationRequested();
                    if (evalLogger != null)
                    {
                        evalLogger.IncreaseGpuEvals(ParticlesCount);
                    }
                    RunUpdateVelocityKernel();
                    RunTransposeKernel();

                    if (!SyncWithCpu || i % SyncCounter != 0)
                    {
                        continue;
                    }

                    PushGpuState();
                    PullCpuState();
                }
            }
            catch (OperationCanceledException ex)
            {
            }
            HostPersonalBestValues = DevicePersonalBestValues;
            HostPersonalBests      = DevicePersonalBests;

            var optimization = PsoServiceLocator.Instance.GetService <IOptimization <double[]> >();
            var bestValue    = optimization.WorstValue(1);
            var bestInd      = 0;

            for (var j = 0; j < HostPersonalBestValues.Length; j++)
            {
                if (optimization.IsBetter(bestValue, new[] { HostPersonalBestValues[j] }) > 0)
                {
                    bestInd   = j;
                    bestValue = new[] { HostPersonalBestValues[j] };
                }
            }
            var bestLoc = HostPersonalBests.Skip(DimensionsCount * bestInd).Take(DimensionsCount).ToArray();

            return(new ParticleState(bestLoc, bestValue));
        }