예제 #1
0
        /// <summary>
        /// Constructs a termination condition.
        /// </summary>
        /// <param name="population">The creature population.</param>
        /// <param name="condition">The termination condition to construct.</param>
        /// <param name="param">The termination condition parameter.</param>
        /// <returns>The constructed termination condition.</returns>
        public static ITerminationCondition ConstructTerminationCondition(TerminationCondition condition, object param = null)
        {
            try
            {
                switch (condition)
                {
                case TerminationCondition.FitnessPlateau:
                    var plateauLength = Convert.ToUInt32(param);
                    return(new FitnessPlateau(plateauLength));

                case TerminationCondition.FitnessThreshold:
                    var fitnessThreshold = Convert.ToUInt32(param);
                    return(new FitnessThreshold(fitnessThreshold));

                case TerminationCondition.GenerationThreshold:
                    var generationThreshold = Convert.ToUInt32(param);
                    return(new GenerationThreshold(generationThreshold));

                case TerminationCondition.Timer:
                    var stopTime = (TimeSpan)param;
                    return(new Timer(stopTime));

                default:
                    throw new ArgumentException("Error! Invalid termination condition.");
                }
            }
            catch
            {
                throw new ArgumentException("Error! Invalid constructor argument.");
            }
        }
예제 #2
0
 /// <summary>
 /// Constructor for AnnService
 /// </summary>
 /// <param name="network">Represents the Artificial Neural Network.</param>
 /// <param name="terminationCondition">Represents the termination condition of
 /// Artificial Neural Network.</param>
 /// <param name="learningRate">Represents the learning rate of Artificial Neural Network.</param>
 public AnnService(
     ActivationNetwork network,
     TerminationCondition terminationCondition,
     double learningRate)
 {
     _network  = network;
     _learning = new BackPropagationLearning(network)
     {
         LearningRate = learningRate
     };
     _terminationCondition = terminationCondition;
 }
예제 #3
0
        /// <summary>
        /// Starts the given process and polls it for completion. At every poll
        /// interval, it asks the given <see cref="TerminationCondition"/> if this
        /// process needs to be killed, and kills it if needs to.
        ///
        /// If the CancellationToken is signalled while in this method, the process
        /// will be killed.
        ///
        /// The process should not be running when this method exists.
        /// </summary>
        /// <param name="termCondition">The termination condition for this process</param>
        /// <param name="proc">The unstarted process</param>
        private void RunProcess(TerminationCondition termCondition, Process proc)
        {
            ThrowIfCancellationRequested();

            proc.Start();
            Logger.LogDiagnostic($"Started process: {proc.ProcessName} (pid {proc.Id})");
            // once we start the process, we need to periodically poll it.
            try
            {
                do
                {
                    Logger.LogDiagnostic("Polling process's exit status");
                    ThrowIfCancellationRequested();
                    if (termCondition.ShouldTerminate(proc))
                    {
                        Logger.LogDiagnostic($"ITerminationCondition has requested that this process be terminated");
                        // if we're asked to terminate the process, do so.
                        try
                        {
                            proc.Kill();
                        }
                        catch (InvalidOperationException)
                        {
                            // According to MSDN, this is what gets thrown when you try to kill a process
                            // that is not running.
                            // Since our process could have died between the time we last
                            // checked and now, just accept the exception and move on.
                            // The end result is that the process isn't running anymore.
                        }

                        break;
                    }

                    Logger.LogDiagnostic("ITerminationCondition has not requested that the process be terminated");

                    // sleep for a second before polling again.
                    Thread.Sleep(1000);
                } while (!proc.HasExited);

                Logger.LogDiagnostic("Process has exited, exiting poll loop");
            }
            catch (OperationCanceledException)
            {
                // kill the process if we get Ctrl-C'd.
                proc.Kill();
                throw;
            }
        }
예제 #4
0
        public DeploymentModel Run(DeploymentModel initialDeploymentModel)
        {
            var initialChromosome = ChromosomeFactory.Create(initialDeploymentModel);
            var population        = InitialPopulationCreator.CreateInitialPopulation(initialChromosome, MinPopulationSize, MaxPopulationSize);

            while (!TerminationCondition.HasReached(population, CurrentState))
            {
                var parents   = SelectionStrategyStrategy.SelectChromosomes(MinPopulationSize, population).ToArray();
                var offspring = Cross(parents).ToArray();
                offspring  = Mutate(offspring).ToArray();
                population = ReinsertionStrategy.Reinsert(offspring, population);
                foreach (var deployment in population.Deployments)
                {
                    deployment.Fitness = FitnessEvaluator.Evaluate(deployment, Workload);
                }
                CurrentState.UpdateOnGenerationRan(population);
            }
            return(population.BestDeployment.ToDeploymentModel());
        }
예제 #5
0
        /// <summary>
        /// Runs a single benchmark on a given version of CoreCLR and saves the results.
        /// </summary>
        /// <param name="version">The version of CoreCLR to run on</param>
        /// <param name="bench">The benchmark to run</param>
        /// <returns>The result from running the benchmark</returns>
        private BenchmarkResult RunBenchmark(PreparedCoreClrVersion version, Benchmark bench)
        {
            ThrowIfCancellationRequested();
            Logger.LogAlways($"Running benchmark {bench.Name}");
            string folderName = Path.Combine(Directory.GetCurrentDirectory(), bench.Name);

            Directory.CreateDirectory(folderName);
            Directory.SetCurrentDirectory(folderName);
            m_relativePath.Push(bench.Name);
            try
            {
                using (TerminationCondition condition = ConstructTerminationCondition(bench))
                {
                    ThrowIfCancellationRequested();
                    return(RunBenchmarkImplWithIterations(version, bench, condition));
                }
            }
            finally
            {
                string upOneDir = Path.Combine(Directory.GetCurrentDirectory(), "..");
                Directory.SetCurrentDirectory(upOneDir);
                m_relativePath.Pop();
            }
        }
예제 #6
0
        /// <summary>
        /// Runs a single benchmark by spawning a process and monitoring it until
        /// its exit.
        /// </summary>
        /// <param name="version">The coreclr version to test</param>
        /// <param name="bench">The benchmark to run</param>
        /// <param name="termCondition">The termination condition for this benchmark</param>
        /// <returns>The result from running the benchmark</returns>
        private IterationResult RunBenchmarkImpl(PreparedCoreClrVersion version, Benchmark bench, TerminationCondition termCondition)
        {
            ThrowIfCancellationRequested();
            string coreRun   = Path.Combine(version.Path, Utils.CoreRunName);
            string arguments = bench.Arguments ?? "";

            Debug.Assert(File.Exists(coreRun));
            Debug.Assert(m_executableProbeMap.ContainsKey(bench));
            string exePath = m_executableProbeMap[bench];

            Process proc = new Process();

            proc.StartInfo.FileName        = coreRun;
            proc.StartInfo.Arguments       = exePath + " " + arguments;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow  = false;
            foreach (var pair in bench.EnvironmentVariables)
            {
                proc.StartInfo.Environment[pair.Key] = pair.Value;
            }

            proc.StartInfo.Environment[Constants.ServerGCVariable]     = m_run.Settings.ServerGC ? "1" : "0";
            proc.StartInfo.Environment[Constants.ConcurrentGCVariable] = m_run.Settings.ConcurrentGC ? "1" : "0";

            // run the process!
            RunProcess(termCondition, proc);
            Debug.Assert(proc.HasExited);

            IterationResult result = new IterationResult();

            result.DurationMsec = (long)(proc.ExitTime - proc.StartTime).TotalMilliseconds;
            result.ExitCode     = proc.ExitCode;
            result.Pid          = proc.Id;

            ThrowIfCancellationRequested();
            return(result);
        }
예제 #7
0
        /// <summary>
        /// Runs a single iteration of a benchmark. If no iteration number if specified,
        /// the benchmark is run once.
        /// </summary>
        /// <param name="version">The version of CoreCLR to run the benchmark on</param>
        /// <param name="bench">The benchmark to run</param>
        /// <param name="termCondition">The termination condition for this benchmark</param>
        /// <returns>The result of running the benchmark</returns>
        private BenchmarkResult RunBenchmarkImplWithIterations(PreparedCoreClrVersion version, Benchmark bench, TerminationCondition termCondition)
        {
            ThrowIfCancellationRequested();
            Logger.LogAlways($"Running iterations for benchmark {bench.Name}");
            BenchmarkResult result = new BenchmarkResult();

            result.Benchmark = bench;
            int iterations = bench.Iterations ?? 1;

            for (int i = 0; i < iterations; i++)
            {
                Logger.LogAlways($"Beginning iteration {i} for benchmark {bench.Name}");
                // we'll create subdirectories for every iteration.
                string folderName = Path.Combine(Directory.GetCurrentDirectory(), i.ToString());
                Directory.CreateDirectory(folderName);
                Directory.SetCurrentDirectory(folderName);
                m_relativePath.Push(i.ToString());
                try
                {
                    IterationResult iterResult;
                    string          traceName = bench.Name + ".etl";
                    m_traceCollector.StartTrace(bench.Name + ".etl", m_run.CollectionLevel);
                    try
                    {
                        // we've got everything set up, time to run.
                        iterResult = RunBenchmarkImpl(version, bench, termCondition);
                    }
                    finally
                    {
                        m_traceCollector.StopTrace();
                    }

                    var currentRelativePath = Path.Combine(m_relativePath.Reverse().ToArray());

                    // TODO(segilles, xplat) adding .zip on the end is done by PerfView, perfcollect
                    // probably doesn't do this.
                    iterResult.TracePathLocation = Path.Combine(currentRelativePath, traceName + ".zip");

                    // write out the result json file that the analysis engine is expecting
                    File.WriteAllText(
                        Path.Combine(Directory.GetCurrentDirectory(), Constants.ResultJsonName),
                        JsonConvert.SerializeObject(iterResult, Formatting.Indented));
                    result.Iterations.Add(iterResult);
                }
                finally
                {
                    string upOneDir = Path.Combine(Directory.GetCurrentDirectory(), "..");
                    Directory.SetCurrentDirectory(upOneDir);
                    m_relativePath.Pop();
                }
            }

            // write out the benchmark json
            File.WriteAllText(
                Path.Combine(Directory.GetCurrentDirectory(), Constants.BenchmarkJsonName),
                JsonConvert.SerializeObject(bench, Formatting.Indented));

            ThrowIfCancellationRequested();
            return(result);
        }
예제 #8
0
 public void SetTerminationCondition(TerminationCondition Condition)
 {
     mTerminationCondition = Condition;
 }
예제 #9
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void persistentCallToSecondary(org.neo4j.causalclustering.messaging.CatchUpRequest request, org.neo4j.causalclustering.catchup.CatchUpResponseAdaptor<StoreCopyFinishedResponse> copyHandler, org.neo4j.causalclustering.catchup.CatchupAddressProvider addressProvider, TerminationCondition terminationCondition) throws StoreCopyFailedException
        private void PersistentCallToSecondary(CatchUpRequest request, CatchUpResponseAdaptor <StoreCopyFinishedResponse> copyHandler, CatchupAddressProvider addressProvider, TerminationCondition terminationCondition)
        {
            Org.Neo4j.causalclustering.helper.TimeoutStrategy_Timeout timeout = _backOffStrategy.newTimeout();
            while (true)
            {
                try
                {
                    AdvertisedSocketAddress address = addressProvider.Secondary();
                    _log.info(format("Sending request '%s' to '%s'", request, address));
                    StoreCopyFinishedResponse response = _catchUpClient.makeBlockingRequest(address, request, copyHandler);
                    if (SuccessfulRequest(response, request))
                    {
                        break;
                    }
                }
                catch (CatchUpClientException e)
                {
                    Exception cause = e.InnerException;
                    if (cause is ConnectException)
                    {
                        _log.warn(cause.Message);
                    }
                    else
                    {
                        _log.warn(format("Request failed exceptionally '%s'.", request), e);
                    }
                }
                catch (CatchupAddressResolutionException e)
                {
                    _log.warn("Unable to resolve address for '%s'. %s", request, e.Message);
                }
                terminationCondition();
                AwaitAndIncrementTimeout(timeout);
            }
        }
예제 #10
0
        /// <summary>
        /// Creates a population and let it evolve until termination condition is reached.
        /// </summary>
        /// <param name="terminationCondition">Evolution termination condition evaluated on each new epoch</param>
        /// <returns>The best solution found across the evolution</returns>
        public ISolution EvolveUntil(TerminationCondition terminationCondition)
        {
            var          rnd                  = RandomProvider.GetThreadRandom();
            var          shiftMatrix          = MatrixBuilder.Build(this.problem);
            var          enumerator           = new IncreasingRowsRandomColumns(this.problem.Days, this.problem.Slots);
            var          constraints          = ConstraintsBuilder.Build(this.problem);
            var          chromoProcessor      = new ChromosomeProcessor(shiftMatrix, enumerator, constraints);
            var          evaluator            = new FitnessEvaluator(problem);
            var          fitnessFunction      = new Fitness.FitnessFunction(chromoProcessor, evaluator, shiftMatrix);
            var          chromosomeLength     = shiftMatrix.GetNumberOfUnforcedSlots();
            const double crossoverProbability = 0.90;
            const double mutationProbability  = 0.05;
            const int    elitismPercentage    = 5;

            epochs = 1;
            epochsWithoutFitnessImprovement = 0;
            overallBestFitness = -1;

            log.Debug("Starting population.");
            var population = new Population();

            for (var i = 0; i < populationSize; i++)
            {
                var c = new Double[chromosomeLength];
                for (var k = 0; k < chromosomeLength; k++)
                {
                    c[k] = rnd.NextDouble();
                }
                var ch = new Chromosome(c);
                population.Solutions.Add(ch);
            }

            //create the genetic operators
            var elite = new Elite(elitismPercentage);

            var crossover = new Crossover(crossoverProbability, true)
            {
                CrossoverType = CrossoverType.SinglePoint
            };

            var mutation = new SwapMutate(mutationProbability);

            //create the GA itself
            var ga = new GeneticAlgorithm(population, fitnessFunction.Evaluate);

            //subscribe to the GAs Generation Complete event
            ga.OnGenerationComplete += Ga_OnGenerationComplete;

            //add the operators to the ga process pipeline
            ga.Operators.Add(elite);
            ga.Operators.Add(crossover);
            ga.Operators.Add(mutation);

            //run the GA
            ga.Run((pop, currentGeneration, currentEvaluation) =>
            {
                return(terminationCondition(currentGeneration, epochsWithoutFitnessImprovement, population.MaximumFitness, population.AverageFitness));
            });

            population.GetTop(1)[0].Evaluate(fitnessFunction.Evaluate);
            return(SolutionBuilder.Build(overallBestFitness, shiftMatrix, epochs * population.PopulationSize, this.problem.Items));
        }