public List <int> FindPath(AlgorithmParameters parameters)
        {
            if (parameters.Instance.V_size >= 40 && size_limit)
            {
                Console.WriteLine("Instance is too big to brute force");
                return(null);
            }
            _parameters = parameters;
            Initialize();
            var paths = GenerateFeasiblePaths();

            if (current_strike >= max_strikes)
            {
                Console.WriteLine("Brute Force Timeout");
                return(null);
            }
            var costs     = PricePaths(paths);
            var min_index = FindMaxIndex(costs);

            if (min_index != -1)
            {
                return(paths[min_index]);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private static (Parameters, TpeParameters) Suggest(SearchSpace space, List <Result> history)
        {
            Parameters    formattedParam = new Parameters();
            TpeParameters param          = new TpeParameters();

            int pipelineIndex  = SuggestCategorical(history, "__pipeline__", space.pipelines.Count);
            var chosenPipeline = space.pipelines[pipelineIndex];

            foreach (AlgorithmSpace algo in space.algorithms.Values)
            {
                if (chosenPipeline.Contains(algo.name))
                {
                    var formattedAlgo = new AlgorithmParameters();
                    formattedParam[algo.name] = formattedAlgo;

                    foreach (ParameterRange range in algo)
                    {
                        if (range.isCategorical)
                        {
                            int index = SuggestCategorical(history, range.tag, range.size);
                            param[range.tag]          = index;
                            formattedAlgo[range.name] = range.categoricalValues[index];
                        }
                        else
                        {
                            double x = SuggestNumerical(history, range.tag, range.low, range.high, range.isLogDistributed, range.isInteger);
                            param[range.tag]          = x;
                            formattedAlgo[range.name] = range.isInteger ? ((int)x).ToString() : x.ToString();
                        }
                    }
                }
            }

            return(formattedParam, param);
        }
Exemplo n.º 3
0
 public List <int> FindPath(AlgorithmParameters parameters)
 {
     _parameters = parameters;
     if (_parameters.Instance.V_size > 20)
     {
         return(_result);
     }
     Initialize();
     ChooseBestPath();
     return(_result);
 }
Exemplo n.º 4
0
    public IEnumerable <RecursiveAlgorithm> RunAlgorithm(AlgorithmParameters parameters)
    {
        //Push parameters onto the stack
        _parameterStack.Push(parameters);
        //Return the current state of the algorithm before we start running
        yield return(this);

        //Now execute the algorithm and return subsequent states
        foreach (var state in Execute())
        {
            yield return(state);
        }
    }
Exemplo n.º 5
0
        private double CalculateReliability(List <int> Path, AlgorithmParameters param)
        {
            if (Path == null || Path.Count <= 1)
            {
                return(0);
            }
            var reliability = 1.0;

            for (int i = 1; i < Path.Count; i++)
            {
                reliability *= param.p[Path[i]];
            }
            return(reliability);
        }
Exemplo n.º 6
0
        public IActionResult Post([FromBody] PipelineViewModel value)
        {
            var             _id             = Guid.Parse(userManager.GetUserId(User));
            Profile         profile         = dataCenterContext.Profiles.Where(x => x.Id == _id).First();
            Algorithm       algorithm       = dataCenterContext.Algorithms.Find(Guid.Parse(value.AlgorithmId));
            CompleteDataSet completeDataSet = dataCenterContext.CompleteDataSets.Find(Guid.Parse(value.DataSetId));
            Pipeline        pipeline        = new Pipeline
            {
                Algorithm          = algorithm,
                Id                 = Guid.NewGuid(),
                Name               = value.Name,
                Description        = value.Description,
                NumberOfContainers = value.NumberOfContainers,
                GetCompleteDataSet = completeDataSet
            };
            List <PipelineParameter> parameters = new List <PipelineParameter>();

            foreach (var parameter in value.Parameters)
            {
                AlgorithmParameters algorithmParameter = dataCenterContext.AlgorithmParameters.Find(Guid.Parse(parameter.Id));
                parameters.Add(new PipelineParameter
                {
                    Id                 = Guid.NewGuid(),
                    Pipeline           = pipeline,
                    Value              = parameter.Value,
                    AlgorithmParameter = algorithmParameter
                });
            }
            pipeline.PipelineParameters = parameters;
            ProfilePipeline profilePipeline = new ProfilePipeline
            {
                Pipeline = pipeline,
                Profile  = profile
            };

            dataCenterContext.ProfilePipeline.Add(profilePipeline);
            dataCenterContext.SaveChanges();

            RabbitMqService rabbitMqService = new RabbitMqService();

            rabbitMqService.SendMessage(pipeline.Id + "~" + algorithm.MasterImage);
            for (int i = 0; i < pipeline.NumberOfContainers - 1; i++)
            {
                rabbitMqService.SendMessage(pipeline.Id + "~" + algorithm.SlaveImage);
            }

            return(Ok());
        }
Exemplo n.º 7
0
        public IActionResult Post([FromBody] AlgorithmParameterViewModel value)
        {
            Algorithm           algorithm  = dataCenterContext.Algorithms.Find(Guid.Parse(value.Algorithm));
            AlgorithmParameters parameters = new AlgorithmParameters
            {
                Algorithm   = algorithm,
                DataType    = value.DataType,
                Description = value.Description,
                Id          = Guid.NewGuid(),
                Name        = value.Name
            };

            dataCenterContext.AlgorithmParameters.Add(parameters);
            dataCenterContext.SaveChanges();
            return(Ok());
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Applies the simulated annealing algorithm to a given <paramref name="problemInput" />.
        /// </summary>
        /// <typeparam name="TInput">The type of the input data.</typeparam>
        /// <typeparam name="TSolution">The type of the solution.</typeparam>
        /// <param name="problemInput">The problem input.</param>
        /// <param name="problemInputLength">The problem input length.</param>
        /// <param name="evaluateInitialSolutionFunc">Function used to evaluate initial complete solution.</param>
        /// <param name="solutionTransitionFunc">Function used to transition solution.</param>
        /// <param name="evaluateSolutionCostFunc">Function used to evaluate the full cost of a solution.</param>
        /// <param name="algorithmParams">The algorithm parameters.</param>
        /// <returns>
        ///     The approximated solution.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="problemInput" /> or <paramref name="evaluateInitialSolutionFunc" /> or
        ///     <paramref name="solutionTransitionFunc" /> or <paramref name="evaluateSolutionCostFunc" /> or
        ///     <paramref name="algorithmParams" /> are null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown if <paramref name="problemInputLength" /> is less than or equal to
        ///     zero.
        /// </exception>
        public static TSolution Evaluate <TInput, TSolution>(
            [NotNull] TInput problemInput,
            int problemInputLength,
            [NotNull] Func <TInput, TSolution> evaluateInitialSolutionFunc,
            [NotNull] Func <TSolution, TInput, int, int, double> solutionTransitionFunc,
            [NotNull] Func <TSolution, double> evaluateSolutionCostFunc,
            [NotNull] AlgorithmParameters algorithmParams)
        {
            Validate.ArgumentNotNull(nameof(problemInput), problemInput);
            Validate.ArgumentNotNull(nameof(evaluateInitialSolutionFunc), evaluateInitialSolutionFunc);
            Validate.ArgumentGreaterThanZero(nameof(problemInputLength), problemInputLength);
            Validate.ArgumentNotNull(nameof(solutionTransitionFunc), solutionTransitionFunc);
            Validate.ArgumentNotNull(nameof(evaluateSolutionCostFunc), evaluateSolutionCostFunc);
            Validate.ArgumentNotNull(nameof(algorithmParams), algorithmParams);

            var temperature         = algorithmParams.InitialTemperature;
            var solution            = evaluateInitialSolutionFunc(problemInput);
            var currentSolutionCost = evaluateSolutionCostFunc(solution);
            var random = new Random();

            const double kb = 1.3807e-16;

            for (var i = 0; i < algorithmParams.CoolingSteps; i++)
            {
                temperature *= algorithmParams.CoolingAlpha;
                var startCost = currentSolutionCost;

                for (var j = 0; j < algorithmParams.IterationsPerCoolingStep; j++)
                {
                    var i1   = random.Next(problemInputLength);
                    var i2   = random.Next(problemInputLength);
                    var flip = random.NextDouble();

                    var delta    = solutionTransitionFunc(solution, problemInput, i1, i2);
                    var exponent = -delta / currentSolutionCost / (kb * temperature);
                    var merit    = Math.Pow(Math.E, exponent);

                    if (delta < 0)
                    {
                        currentSolutionCost = currentSolutionCost + delta;
                    }
                    else
                    {
                        if (merit > flip)
                        {
                            currentSolutionCost = currentSolutionCost + delta;
                        }
                        else
                        {
                            solutionTransitionFunc(solution, problemInput, i1, i2);
                        }
                    }
                }

                if (currentSolutionCost - startCost < 0)
                {
                    temperature = temperature / algorithmParams.CoolingAlpha;
                }
            }

            return(solution);
        }
Exemplo n.º 9
0
        public static T[][] Evaluate <T>(
            [NotNull] IList <T> sequence,
            int partitionLength,
            [NotNull] Func <T[], double> evaluatePartitionCostFunc,
            [NotNull] AlgorithmParameters algorithmParams)
        {
            Validate.ArgumentNotNull(nameof(sequence), sequence);
            Validate.ArgumentNotNull(nameof(evaluatePartitionCostFunc), evaluatePartitionCostFunc);
            Validate.ArgumentNotNull(nameof(algorithmParams), algorithmParams);
            Validate.ArgumentGreaterThanZero(nameof(partitionLength), partitionLength);

            var result = Evaluate(sequence, sequence.Count, input =>
            {
                Assert.NotNull(input);

                /* Create partitions */
                var partitions = new List <T[]>();
                for (var i = 0; i < input.Count / partitionLength; i++)
                {
                    partitions.Add(new T[partitionLength]);
                }

                var remainder = input.Count % partitionLength;
                if (remainder != 0)
                {
                    partitions.Add(new T[remainder]);
                }

                var array = partitions.Select(s => new Partition <T>(s, evaluatePartitionCostFunc(s))).ToArray();

                for (var x = 0; x < input.Count; x++)
                {
                    array[x / partitionLength].Items[x % partitionLength] = input[x];
                }

                foreach (var t in array)
                {
                    t.Cost = evaluatePartitionCostFunc(t.Items);
                }

                return(array);
            },
                                  (solution, input, i1, i2) =>
            {
                Assert.NotNull(solution);
                Assert.NotNull(input);
                Assert.Condition(i1 >= 0 && i1 < input.Count);
                Assert.Condition(i2 >= 0 && i2 < input.Count);

                var partition1 = solution[i1 / partitionLength];
                var partition2 = solution[i2 / partitionLength];

                var temp = partition1.Items[i1 % partitionLength];
                partition1.Items[i1 % partitionLength] = partition2.Items[i2 % partitionLength];
                partition2.Items[i2 % partitionLength] = temp;

                var newCost1 = evaluatePartitionCostFunc(partition1.Items);
                var newCost2 = evaluatePartitionCostFunc(partition2.Items);

                var delta       = newCost1 - partition1.Cost + (newCost2 - partition2.Cost);
                partition1.Cost = newCost1;
                partition2.Cost = newCost2;

                return(delta);
            },
                                  solution =>
            {
                Assert.NotNull(solution);
                return(solution.Sum(s => s.Cost));
            },
                                  algorithmParams);

            return(result.Select(s => s.Items).ToArray());
        }