Beispiel #1
0
        public override (ResultDto, long) Compute(DefinitionDto definition)
        {
            var random     = new Random();
            var generation = InitializeGeneration(definition, random);

            for (var generationIndex = 0; generationIndex < Generations; generationIndex++)
            {
                var generationSelection = SelectionStrategy.Select(definition, random, generation).ToList();
                var generationNew       = CrossStrategy.Cross(definition.Items.Count, random, generationSelection, PopulationSize, CrossoverProbability).ToList();

                Mutation(random, generationNew);

                generation = generationNew;
                //var currentResult = GetResult(definition, generation);
                //Debug.WriteLine($"{generationIndex}\t{currentResult.Item1.Price}\t{currentResult.Item1.Weight}");
            }

            var result      = GetResult(definition, generation);
            var resultBools = new List <bool>();

            for (int index = 0; index < result.Item2.Count; index++)
            {
                resultBools.Add(result.Item2[index]);
            }
            return(new ResultDto(definition.Id, result.Item1.Price, resultBools), 0L);
        }
Beispiel #2
0
        public byte[] trainSVM(double[][] inputs, int[] outputs)
        {
            IKernel kernel = Gaussian.Estimate(inputs, inputs.Length / 4);

            var numComplexity = kernel.EstimateComplexity(inputs);

            double            complexity = numComplexity;
            double            tolerance  = (double)0.2;
            int               cacheSize  = (int)1000;
            SelectionStrategy strategy   = SelectionStrategy.SecondOrder;

            // Create the learning algorithm using the machine and the training data
            var ml = new MulticlassSupportVectorLearning <IKernel>()
            {
                // Configure the learning algorithm
                Learner = (param) => new SequentialMinimalOptimization <IKernel>()
                {
                    Complexity = complexity,
                    Tolerance  = tolerance,
                    CacheSize  = cacheSize,
                    Strategy   = strategy,
                    Kernel     = kernel
                }
            };

            var ksvm = ml.Learn(inputs, outputs);

            byte[] saved;
            Accord.IO.Serializer.Save(ksvm, out saved);
            return(saved);
        }
    /// <summary>
    /// Select coins in a way that user can pay without a change output (to increase privacy)
    /// and try to find a solution that requires to pay as little extra amount as possible.
    /// </summary>
    /// <param name="strategy">The strategy determines what the algorithm is looking for.</param>
    /// <param name="target">Target value we want to, ideally, sum up from the input values. </param>
    /// <param name="inputEffectiveValues">Dictionary to map back the effective values to their original SmartCoin. </param>
    /// <returns><c>true</c> if a solution was found, <c>false</c> otherwise.</returns>
    internal static bool TryGetCoins(SelectionStrategy strategy, long target, Dictionary <SmartCoin, long> inputEffectiveValues, [NotNullWhen(true)] out IEnumerable <SmartCoin>?selectedCoins, CancellationToken cancellationToken = default)
    {
        selectedCoins = null;

        BranchAndBound branchAndBound = new();

        bool        foundExactMatch = false;
        List <long>?solution        = null;

        try
        {
            foundExactMatch = branchAndBound.TryGetMatch(strategy, out solution, cancellationToken);
        }
        catch (OperationCanceledException)
        {
            Logger.LogInfo("Computing privacy suggestions was cancelled or timed out.");
        }

        // If we've not found an optimal solution then we will use the best.
        if (!foundExactMatch && strategy.GetBestSelectionFound() is long[] bestSolution)
        {
            solution = bestSolution.ToList();
        }

        if (solution is not null)
        {
            // Sanity check: do not return solution that is much higher or much lower than the target.
            if (solution.Sum() > target * MaxExtraPayment || solution.Sum() < target * MinPaymentThreshold)
            {
                return(false);
            }

            List <SmartCoin> resultCoins = new();
            int i = 0;

            foreach ((SmartCoin smartCoin, long effectiveSatoshis) in inputEffectiveValues)
            {
                // Both arrays are in decreasing order so the first match will be the coin we are looking for.
                if (effectiveSatoshis == solution[i])
                {
                    i++;
                    resultCoins.Add(smartCoin);
                    if (i == solution.Count)
                    {
                        break;
                    }
                }
            }

            selectedCoins = resultCoins;
            return(true);
        }

        return(false);
    }
Beispiel #4
0
        /// <summary>
        /// ## Execute
        ///
        /// Executes the pipeline on a directory with the specified parameters.
        /// </summary>
        /// <param name="directory"></param>
        /// <param name="selectParams"></param>
        /// <param name="filterParams"></param>
        /// <returns></returns>
        public PakuResult Execute(string directory, string selectParams, string filterParams, string pakuParams, bool logToFile = false)
        {
            PakuResult result = new PakuResult();

            // check if directory exists first
            DirectoryInfo di = new DirectoryInfo(directory);

            if (!di.Exists)
            {
                // if not, throw an argument exception
                // we do not want to create a logger in this case, as it will create the directory structure for the log file
                result.Error = new ArgumentException($"Directory does not exist: {directory}");
            }
            else
            {
                // setup the logger
                BootstrapLogger(logToFile, directory);

                try
                {
                    Logger.Info($"Cleaning directory: {directory}");

                    IList <VirtualFileInfo> files = SelectionStrategy.Select(di, selectParams);
                    Logger.Info($"Files selected: {files.Count}");

                    files = FilterStrategy.Filter(files, filterParams);
                    Logger.Info($"Files filtered: {files.Count}");

                    result = PakuStrategy.Eat(di, files, pakuParams);
                    Logger.Info($"Files removed: {result.RemovedFiles.Count}");

                    foreach (VirtualFileInfo fi in result.RemovedFiles)
                    {
                        Logger.Debug($"Removed: {fi.Name}");
                    }
                }
                catch (Exception ex)
                {
                    result.Error = ex;
                }

                if (!result.Success)
                {
                    Logger.Error(result.Error);
                }
            }

            // write any errors to the console
            if (!result.Success)
            {
                Console.WriteLine(result.Error.Message);
            }

            return(result);
        }
Beispiel #5
0
        private static void CreateVectorMachines()
        {
            var kernel = new ChiSquare();

            // Extract training parameters from the interface
            double            complexity = (double)1;
            double            tolerance  = (double)0.01;
            int               cacheSize  = (int)500;
            SelectionStrategy strategy   = SelectionStrategy.Sequential;

            // Create the support vector machine learning algorithm
            var teacher = new MulticlassSupportVectorLearning <IKernel>()
            {
                Kernel  = kernel,
                Learner = (param) =>
                {
                    return(new SequentialMinimalOptimization <IKernel>()
                    {
                        Kernel = kernel,
                        Complexity = complexity,
                        Tolerance = tolerance,
                        CacheSize = cacheSize,
                        Strategy = strategy,
                    });
                }
            };

            // Get the input and output data
            double[][] inputs;
            int[]      outputs;

            var inputsList  = new List <double[]>();
            var outputsList = new List <int>();
            var i           = 0;

            foreach (var trainingImage in trainingImages.Keys)
            {
                var trainingFeature = trainingFeatures[trainingImage];

                inputsList.Add(trainingFeature);
                outputsList.Add(i);

                i++;
            }

            inputs  = inputsList.ToArray();
            outputs = outputsList.ToArray();

            ksvm = teacher.Learn(inputs, outputs);

            double error = new ZeroOneLoss(outputs).Loss(ksvm.Decide(inputs));

            Console.WriteLine("Error was {0}", error);
        }
Beispiel #6
0
        /// <summary>
        /// Perform the search. Note: should set the Solution in the SearchContext and update its Status.
        /// </summary>
        /// <param name="context">The context within which the search happens.</param>
        public override void Search(SearchContext <D, P, A, S, Sol> context)
        {
            var clone     = context.Cloner;
            var rootState = context.Source;
            var apply     = context.Application;

            var endTime = DateTime.Now.AddMilliseconds(Time);
            var it      = 0;

            // Setup for when we might be continuing a search from a specific node.
            var root = (TreeSearchNode <P, A>)context.StartNode;

            if (root == null)
            {
                root = new TreeSearchNode <P, A>(clone.Clone(rootState));
                context.StartNode = root;
            }

            while ((Time == Constants.NO_LIMIT_ON_THINKING_TIME || DateTime.Now < endTime) &&
                   (Iterations == Constants.NO_LIMIT_ON_ITERATIONS || it < Iterations))
            {
                it++;

                var worldState = clone.Clone(rootState);
                var target     = root;

                // Check if we have to expand
                if (!target.IsFullyExpanded())
                {
                    target = ExpansionStrategy.Expand(context, root, worldState);
                }

                // Select a node
                if (target == null || target == root)
                {
                    target = SelectionStrategy.SelectNextNode(context, root);
                }

                // Apply action in selected node
                worldState = apply.Apply(context, worldState, target.Payload);
                // Simulate
                var endState = PlayoutStrategy.Playout(context, worldState);

                // Backpropagation
                BackPropagationStrategy.BackPropagate(context, EvaluationStrategy, target, endState);
            }

            var finalNode = FinalNodeSelectionStrategy.SelectFinalNode(context, root);

            context.Solution    = SolutionStrategy.Solution(context, finalNode);
            context.BudgetSpent = it;
            context.Status      = SearchContext <D, P, A, S, Sol> .SearchStatus.Success;
        }
Beispiel #7
0
    public static async IAsyncEnumerable <IEnumerable <SmartCoin> > GetAllStrategyResultsAsync(
        IEnumerable <SmartCoin> availableCoins,
        FeeRate feeRate,
        TxOut txOut,
        int maxInputCount = int.MaxValue,
        [EnumeratorCancellation] CancellationToken cancellationToken = default)
    {
        // target = target amount + output cost
        long target = txOut.Value.Satoshi + feeRate.GetFee(txOut.ScriptPubKey.EstimateOutputVsize()).Satoshi;

        // Keys are effective values of smart coins in satoshis.
        IOrderedEnumerable <SmartCoin> sortedCoins = availableCoins.OrderByDescending(x => x.EffectiveValue(feeRate).Satoshi);

        // How much it costs to spend each coin.
        long[] inputCosts = sortedCoins.Select(x => feeRate.GetFee(x.ScriptPubKey.EstimateInputVsize()).Satoshi).ToArray();

        Dictionary <SmartCoin, long> inputEffectiveValues = new(sortedCoins.ToDictionary(x => x, x => x.EffectiveValue(feeRate).Satoshi));

        // Pass smart coins' effective values in descending order.
        long[] inputValues = inputEffectiveValues.Values.ToArray();

        StrategyParameters parameters = new(target, inputValues, inputCosts, maxInputCount);

        SelectionStrategy[] strategies = new SelectionStrategy[]
        {
            new MoreSelectionStrategy(parameters),
            new LessSelectionStrategy(parameters)
        };

        var tasks = strategies
                    .Select(strategy => Task.Run(
                                () =>
        {
            if (TryGetCoins(strategy, inputEffectiveValues, out IEnumerable <SmartCoin>?coins, cancellationToken))
            {
                return(coins);
            }

            return(Enumerable.Empty <SmartCoin>());
        },
                                cancellationToken))
                    .ToArray();

        foreach (var task in tasks)
        {
            yield return(await task.ConfigureAwait(false));
        }
    }
Beispiel #8
0
        public bool accordTrainingSVM(double[][] inputs, int[] outputs, string modelName)
        {
            int classes = outputs.Distinct().Count();
            var kernel  = getKernel();

            try
            {
                // Create the Multi-class Support Vector Machine using the selected Kernel
                MulticlassSupportVectorMachine ksvm = new MulticlassSupportVectorMachine(inputs[0].Length, kernel, classes);

                // Create the learning algorithm using the machine and the training data
                MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, outputs);

                //// Extract training parameters from the interface
                // complexity;// = (double)numComplexity.Value;
                // double tolerance; //= (double)numTolerance.Value;
                // cacheSize; //= (int)numCache.Value;
                //SelectionStrategy strategy; // cbStrategy.SelectedItem;
                double            complexity = 100.00000;
                double            tolerance  = 0.001;
                int               cacheSize  = 500;
                SelectionStrategy strategy   = (SelectionStrategy.Sequential);
                // Configure the learning algorithm

                ml.Algorithm = (svm, classInputs, classOutputs, i, j) =>
                {
                    return(new SequentialMinimalOptimization(svm, classInputs, classOutputs)
                    {
                        //complexity,
                        //tolerance ,
                        //cacheSize,
                        //strategy ,
                    });
                };
                double error = ml.Run();
                ksvm.Save(modelName);
                return(true);
            }
            catch (Exception eeAccord)
            {
                return(false);
            }

            //<<<<<<< .mine
            //=======            ksvm.Save(@"..\\..\\..\\..\\libs\\models\\HDR_Models\\BagOfWordsModel.xml");
            //>>>>>>> .theirs
        }
 public MCTS(POGame poGame, Dictionary <string, List <Card> > decksDict, Dictionary <string, double> probsDict,
             int turnDepth = 1, int timeBudget = 2000, SelectionStrategy selectionStrategy = SelectionStrategy.UCT,
             StateRateStrategy stateRateStrategy = StateRateStrategy.Greedy, double explorationConstant = 0.5)
 {
     TurnDepth            = turnDepth;
     COMPUTATIONAL_BUDGET = timeBudget;
     EXPLORATION_CONSTANT = explorationConstant;
     Selection            = selectionStrategy;
     StateRate            = stateRateStrategy;
     player       = poGame.CurrentPlayer;
     Root         = new Node(poGame, player.PlayerId);
     InitialState = poGame;
     InitializeNode(Root, InitialState);
     DecksDict         = decksDict;
     ProbabilitiesDict = probsDict;
     ActionEstimator   = new ActionEstimator(DecksDict, ProbabilitiesDict);
     //poGame.CurrentPlayer.Options().ForEach(task => Console.Write(task + " "));
     //Console.WriteLine();
 }
        private IList <E> GetReduced(
            IList <E> edges,
            IDictionary <string, IList <E> > map,
            SelectionStrategy <E, V, W> selectionStrategy
            )
        {
            IList <E> edgesToReturn = new List <E>();

            foreach (E edge in edges)
            {
                string key = edge.EdgeId;
                if (map.ContainsKey(key))
                {
                    IList <E> list   = map[key];
                    E         reduce = selectionStrategy.Reduce(list);
                    edgesToReturn.Add(reduce);
                    map.Remove(key);
                }
            }
            return(edgesToReturn);
        }
Beispiel #11
0
        /// <summary>
        ///   Creates a Support Vector Machine and estimate
        ///   its parameters using a learning algorithm.
        /// </summary>
        ///
        private void btnRunTraining_Click(object sender, EventArgs e)
        {
            if (dgvTrainingSource.Rows.Count == 0)
            {
                MessageBox.Show("Please load the training data before clicking this button");
                return;
            }

            lbStatus.Text = "Gathering data. This may take a while...";
            Application.DoEvents();



            // Extract inputs and outputs
            int rows = dgvTrainingSource.Rows.Count;

            double[][] input  = new double[rows][];
            int[]      output = new int[rows];
            for (int i = 0; i < rows; i++)
            {
                input[i]  = (double[])dgvTrainingSource.Rows[i].Cells["colTrainingFeatures"].Value;
                output[i] = (int)dgvTrainingSource.Rows[i].Cells["colTrainingLabel"].Value;
            }

            // Create the chosen kernel function
            // using the user interface parameters
            //
            IKernel kernel = createKernel();

            // Extract training parameters from the interface
            double            complexity = (double)numComplexity.Value;
            double            tolerance  = (double)numTolerance.Value;
            int               cacheSize  = (int)numCache.Value;
            SelectionStrategy strategy   = (SelectionStrategy)cbStrategy.SelectedItem;


            // Create the Multi-class Support Vector Machine using the selected Kernel
            ksvm = new MulticlassSupportVectorMachine(1024, kernel, 10);

            // Create the learning algorithm using the machine and the training data
            MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, input, output)
            {
                // Configure the learning algorithm
                Algorithm = (svm, classInputs, classOutputs, i, j) =>

                            // Use Platt's Sequential Minimal Optimization algorithm
                            new SequentialMinimalOptimization(svm, classInputs, classOutputs)
                {
                    Complexity = complexity,
                    Tolerance  = tolerance,
                    CacheSize  = cacheSize,
                    Strategy   = strategy,
                    Compact    = (kernel is Linear)
                }
            };


            lbStatus.Text = "Training the classifiers. This may take a (very) significant amount of time...";
            Application.DoEvents();

            Stopwatch sw = Stopwatch.StartNew();

            // Train the machines. It should take a while.
            double error = ml.Run();

            sw.Stop();


            lbStatus.Text = String.Format(
                "Training complete ({0}ms, {1}er). Click Classify to test the classifiers.",
                sw.ElapsedMilliseconds, error);

            // Update the interface status
            btnClassifyVoting.Enabled      = true;
            btnClassifyElimination.Enabled = true;
            btnCalibration.Enabled         = true;


            // Populate the information tab with the machines
            dgvMachines.Rows.Clear();
            int k = 1;

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < i; j++, k++)
                {
                    var machine = ksvm[i, j];

                    int sv = machine.SupportVectors == null ? 0 : machine.SupportVectors.Length;

                    int c = dgvMachines.Rows.Add(k, i + "-vs-" + j, sv, machine.Threshold);
                    dgvMachines.Rows[c].Tag = machine;
                }
            }

            // approximate size in bytes =
            //   number of support vectors * number of doubles in a support vector * size of double
            int   bytes     = ksvm.SupportVectorUniqueCount * 1024 * sizeof(double);
            float megabytes = bytes / (1024 * 1024);

            lbSize.Text = String.Format("{0} ({1} MB)", ksvm.SupportVectorUniqueCount, megabytes);
        }
Beispiel #12
0
        /// <summary>
        ///   Creates the Support Vector Machines that will identify images based on
        ///   their Bag-of-Visual-Words feature vector representation.
        /// </summary>
        ///
        private void btnCreateVectorMachines_Click(object sender, EventArgs e)
        {
            double[][] inputs;
            int[]      outputs;

            getData(out inputs, out outputs);

            int classes = outputs.Distinct().Count();

            var kernel = getKernel();

            // Create the Multi-class Support Vector Machine using the selected Kernel
            ksvm = new MulticlassSupportVectorMachine(inputs[0].Length, kernel, classes);

            // Create the learning algorithm using the machine and the training data
            MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, outputs);

            // Extract training parameters from the interface
            double            complexity = (double)numComplexity.Value;
            double            tolerance  = (double)numTolerance.Value;
            int               cacheSize  = (int)numCache.Value;
            SelectionStrategy strategy   = (SelectionStrategy)cbStrategy.SelectedItem;

            // Configure the learning algorithm
            ml.Algorithm = (svm, classInputs, classOutputs, i, j) =>
            {
                return(new SequentialMinimalOptimization(svm, classInputs, classOutputs)
                {
                    Complexity = complexity,
                    Tolerance = tolerance,
                    CacheSize = cacheSize,
                    Strategy = strategy,
                });
            };


            lbStatus.Text = "Training the classifiers. This may take a (very) significant amount of time...";
            Application.DoEvents();

            Stopwatch sw = Stopwatch.StartNew();

            // Train the machines. It should take a while.
            double error = ml.Run();

            sw.Stop();

            lbStatus.Text = String.Format(
                "Training complete ({0}ms, {1}er). Click Classify to test the classifiers.",
                sw.ElapsedMilliseconds, error);

            btnClassifyElimination.Enabled = true;

            // Populate the information tab with the machines
            dgvMachines.Rows.Clear();
            int k = 1;

            for (int i = 0; i < classes; i++)
            {
                for (int j = 0; j < i; j++, k++)
                {
                    var machine = ksvm[i, j];

                    int sv = machine.SupportVectors == null ? 0 : machine.SupportVectors.Length;

                    int c = dgvMachines.Rows.Add(k, i + "-vs-" + j, sv, machine.Threshold);
                    dgvMachines.Rows[c].Tag = machine;
                }
            }

            // approximate size in bytes =
            //   number of support vectors *
            //   number of doubles in a support vector *
            //   size of double
            int   bytes     = ksvm.SupportVectorUniqueCount * 1024 * sizeof(double);
            float megabytes = bytes / (1024 * 1024);

            lbSize.Text = String.Format("{0} ({1} MB)", ksvm.SupportVectorUniqueCount, megabytes);
        }
Beispiel #13
0
        /// <summary>
        ///   Creates the Support Vector Machines that will identify images based on
        ///   their Bag-of-Visual-Words feature vector representation.
        /// </summary>
        ///
        private void btnCreateVectorMachines_Click(object sender, EventArgs e)
        {
            // Get the chosen kernel
            IKernel kernel = getKernel();

            // Extract training parameters from the interface
            double            complexity = (double)numComplexity.Value;
            double            tolerance  = (double)numTolerance.Value;
            int               cacheSize  = (int)numCache.Value;
            SelectionStrategy strategy   = (SelectionStrategy)cbStrategy.SelectedItem;

            // Create the support vector machine learning algorithm
            var teacher = new MulticlassSupportVectorLearning <IKernel>()
            {
                Kernel  = kernel,
                Learner = (param) =>
                {
                    return(new SequentialMinimalOptimization <IKernel>()
                    {
                        Kernel = kernel,
                        Complexity = complexity,
                        Tolerance = tolerance,
                        CacheSize = cacheSize,
                        Strategy = strategy,
                    });
                }
            };

            // Get the input and output data
            double[][] inputs;
            int[]      outputs;
            getData(out inputs, out outputs);

            // Prepare to start learning
            lbStatus.Text = "Training the classifiers. This may take a (very) significant amount of time...";
            Application.DoEvents();

            Stopwatch sw = Stopwatch.StartNew();

            // Train the machines. It should take a while.
            this.ksvm = teacher.Learn(inputs, outputs);

            sw.Stop();

            // Compute the training error (accuracy, also known as zero-one-loss)
            double error = new ZeroOneLoss(outputs).Loss(ksvm.Decide(inputs));

            lbStatus.Text = String.Format(
                "Training complete ({0}ms, {1}er). Click Classify to test the classifiers.",
                sw.ElapsedMilliseconds, error);

            btnClassifyElimination.Enabled = true;

            // Populate the information tab with the machines
            dgvMachines.Rows.Clear();
            for (int i = 0, k = 1; i < ksvm.NumberOfOutputs; i++)
            {
                for (int j = 0; j < i; j++, k++)
                {
                    SupportVectorMachine <IKernel> machine = ksvm[i, j];

                    int numberOfSupportVectors = machine.SupportVectors == null ?
                                                 0 : machine.SupportVectors.Length;

                    int rowIndex = dgvMachines.Rows.Add(k, i + "-vs-" + j, numberOfSupportVectors, machine.Threshold);
                    dgvMachines.Rows[rowIndex].Tag = machine;
                }
            }

            // approximate size in bytes =
            //   number of support vectors * number of doubles in a support vector * size of double
            int    bytes     = ksvm.SupportVectorUniqueCount * ksvm.NumberOfInputs * sizeof(double);
            double megabytes = bytes / (1024.0 * 1024.0);

            lbSize.Text = String.Format("{0} ({1} MB)", ksvm.SupportVectorUniqueCount, megabytes);
        }
        /// <summary>
        /// Perform the search. Note: should set the Solution in the SearchContext and update its Status.
        /// </summary>
        /// <param name="context">The context within which the search happens.</param>
        public override void Search(SearchContext <D, P, A, S, Sol> context)
        {
            var clone     = context.Cloner;
            var rootState = context.Source;
            var apply     = context.Application;
            var goal      = context.Goal;

            var endTime = DateTime.Now.AddMilliseconds(Time);
            var it      = 0;

            // Setup for when we might be continuing a search from a specific node.
            var root = (TreeSearchNode <P, A>)context.StartNode;

            if (root == null)
            {
                root = new TreeSearchNode <P, A>(clone.Clone(rootState));
                context.StartNode = root;
            }

            while ((Time == Constants.NO_LIMIT_ON_THINKING_TIME || DateTime.Now < endTime) && (Iterations == Constants.NO_LIMIT_ON_ITERATIONS || it < Iterations))
            {
                it++;

                var worldState = clone.Clone(rootState);

                // Selection
                bool done;
                var  target = root;
                while (!(done = goal.Done(context, worldState)) && target.IsFullyExpanded())
                {
                    target     = SelectionStrategy.SelectNextNode(context, target);
                    worldState = apply.Apply(context, worldState, target.Payload);
                }

                // Expansion
                var endState = worldState;
                if (!done)
                {
                    var result = ExpansionStrategy.Expand(context, target, endState);
                    if (result != target)
                    {
                        endState = apply.Apply(context, endState, result.Payload);
                        target   = result;
                    }

                    // Simulation
                    endState = PlayoutStrategy.Playout(context, endState);
                }

                // Keep track of the maximum depth we reach
                var nodeDepth = target.CalculateDepth();
                if (nodeDepth > MaxDepth)
                {
                    MaxDepth = nodeDepth;
                }

                // Backpropagation
                BackPropagationStrategy.BackPropagate(context, EvaluationStrategy, target, endState);
            }

            var finalNode = FinalNodeSelectionStrategy.SelectFinalNode(context, root);

            context.Solution    = SolutionStrategy.Solution(context, finalNode);
            context.BudgetSpent = it;
            context.Status      = SearchContext <D, P, A, S, Sol> .SearchStatus.Success;
        }
 public static Func <Node, Node, double, double> GetSelectionStrategy(SelectionStrategy strategy)
 {
     return(SelectionStrategiesDict.GetValueOrDefault(strategy, SelectionStrategiesDict[SelectionStrategy.MaxRatioChild]));
 }
Beispiel #16
0
    /// <summary>
    /// Attempts to find a set of values that sum up to the target value.
    /// </summary>
    /// <param name="searchStrategy">Search strategy that affects how the algorithm searches through the options.</param>
    /// <param name="selectedValues">Solution of the search algorithm based on <paramref name="searchStrategy"/> sorted in descending order.</param>
    /// <returns><c>true</c> when a match is found, <c>false</c> otherwise.</returns>
    public bool TryGetMatch(SelectionStrategy searchStrategy, [NotNullWhen(true)] out List <long>?selectedValues, CancellationToken cancellationToken = default)
    {
        selectedValues = null;

        if (TryFindSolution(searchStrategy, out long[]? solution, cancellationToken))