コード例 #1
0
        static void Main(string[] args)
        {
            //PARAMETERS
            int numberOfIterations = 50;
            int p = 3;
            int numberOfRandomStartingPoints = 3;

            //EXAMPLES

            //Quantum Santa (http://quantumalgorithmzoo.org/traveling_santa/)
            double[]        dtx          = { 0.619193, 0.742566, 0.060035, -1.568955, 0.045490 };
            double[]        dtz          = { 3.182203, -1.139045, 0.221082, 0.537753, -0.417222 };
            double[]        segmentCosts = { 4.70, 9.09, 9.03, 5.70, 8.02, 1.71 };
            double[]        dh           = { 4 * 20 - 0.5 * 4.7, 4 * 20 - 0.5 * 9.09, 4 * 20 - 0.5 * 9.03, 4 * 20 - 0.5 * 5.70, 4 * 20 - 0.5 * 8.02, 4 * 20 - 0.5 * 1.71 };
            double[]        dJ           = { 40.0, 40.0, 20.0, 40.0, 40.0, 40.0,
                                             40.0,                  40.0, 40.0, 20.0, 40.0, 40.0,
                                             40.0,                  40.0, 40.0, 40.0, 40.0, 40.0,
                                             40.0,                  40.0, 40.0, 40.0, 40.0, 40.0,
                                             40.0,                  40.0, 40.0, 40.0, 40.0, 20.0,
                                             40.0,                  40.0, 40.0, 40.0, 40.0, 40.0 };
            ProblemInstance quantumSanta = new ProblemInstance(dh, dJ);


            //MaxCut (medium.com/mdr-inc/qaoa-maxcut-using-blueqat-aaf33038f46e)
            dh = new Double[] { 0, 0, 0, 0, 0 };
            dJ = new Double[] { 0, 1, 0, 1, 0,
                                0, 0, 1, 0, 0,
                                0, 0, 0, 1, 1,
                                0, 0, 0, 0, 1,
                                0, 0, 0, 0, 0 };
            ProblemInstance maxCut1 = new ProblemInstance(dh, dJ);


            //Rigetti MaxCut unit tests
            dh = new Double[] { -0.5, 0, -1, 0.5 };
            dJ = new Double[] { 0, 1, 2, 0,
                                0, 0, 0.5, 0,
                                0, 0, 0, 2.5,
                                0, 0, 0, 0 };
            ProblemInstance maxCut2 = new ProblemInstance(dh, dJ);


            dh = new Double[] { 0.8, -0.5 };
            dJ = new Double[] { 0, -1,
                                0, 0 };
            ProblemInstance maxCut3 = new ProblemInstance(dh, dJ);

            dh = new Double[] { 0, 0 };
            dJ = new Double[] { 0, 1,
                                0, 0 };
            ProblemInstance maxCut4 = new ProblemInstance(dh, dJ);

            //END EXAMPLES

            HybridQaoa cop = new HybridQaoa(numberOfIterations, p, quantumSanta, numberOfRandomStartingPoints);

            OptimalSolution res = cop.RunOptimization();

            Console.WriteLine(res.optimalVector);
        }
コード例 #2
0
        public void RunHybridQaoaTest()
        {
            double[] dh = new Double[] { 0, 0 };
            double[] dJ = new Double[] { 0, 1,
                                         0, 0 };

            int numberOfIterations = 50;
            int p = 2;
            int numberOfRandomStartingPoints = 2;

            ProblemInstance simpleMaxCut = new ProblemInstance(dh, dJ);

            HybridQaoa      classicalOptimization = new HybridQaoa(numberOfIterations, p, simpleMaxCut, numberOfRandomStartingPoints);
            OptimalSolution optimalSolution       = classicalOptimization.RunOptimization();

            string optimizationResult1 = "01";
            string optimizationResult2 = "10";

            string result = optimalSolution.optimalVector;

            Console.WriteLine(result);

            Assert.IsTrue(result.Equals(optimizationResult1) || result.Equals(optimizationResult2), "Hybrid QAOA produced incorrect result.");
        }
コード例 #3
0
        /*
         *    //Bottom up Dynamic Programming approach to Knapsack
         * Description / Algorithm from here:
         * https://github.com/ayzahmt/Knapsack-Problem
         *   Solution of knapsack problem using dynamic programming
         *
         *   Purpose
         *   To get as much value into the knapsack as possible given the weight constraint of the knapsack.
         *
         *   Solution Approach
         *   Evaluate the values of the items iteratively.
         *   For example, put the first item and select second item. Then evaluate first and second item. Make the most appropriate choice according to the value and weight of the items. And then evaluate the all item.
         *   In order to get rid of the recalculation, the calculation for the items are kept on a table at each step.
         *   After the items have been evaluated, the value of V[ItemCount,MaximumWeight] shows the maximum value we can get to the knapsack.
         *   Example
         *   Item Count = 4
         *   Max Weight = 5
         *   Item	1	2	3	4
         *   Value	100	20	60	40
         *   Weight	3	2	4	1
         *   Value Matrix
         *
         *   V[i,w]	w=0	1	2	3	4	5
         *   i=0	0	0	0	0	0	0
         *   1	0	0	0	100	100	100
         *   2	0	0	20	100	100	120
         *   3	0	0	20	100	100	120
         *   4	0	40	40	100	140	140
         *   Maximum value we can put the knapsack is V[4,5] = 140
         *
         *  // Processing requirements
         *   O(N*W) ** Requires int array of size [N,M] to store previous results
         */

        private void DetermineOptimalSolution(KSItem[] items)
        {
            int  n             = items.Length;
            bool includeVolume = (TM.MaxVolume != null);

            //Hard Constraints
            int maxWeight = TM.MaxWeight;
            int maxVolume = TM.MaxVolume.GetValueOrDefault(0);

            int curWeight, curVolume;

            //Value array is multidimensional (Number of Items, maxWeight, maxVolume)
            int[,,] valueArray = new int[n + 1, maxWeight + 1, maxVolume + 1];

            for (int i = 0; i <= n; i++)
            {
                for (curWeight = 0; curWeight <= maxWeight; curWeight++)
                {
                    for (curVolume = 0; curVolume <= maxVolume; curVolume++)
                    {
                        //Base case
                        if (i == 0 || curWeight == 0 || (includeVolume && curVolume == 0))
                        {
                            valueArray[i, curWeight, curVolume] = 0;
                        }

                        //If the item Can be added
                        else if (items[i - 1].Weight <= curWeight && (!includeVolume || (includeVolume && (items[i - 1].Volume <= curVolume))))
                        {
                            int nextItem  = items[i - 1].Value;
                            int prevState = valueArray[i - 1, curWeight - items[i - 1].Weight, includeVolume ? curVolume - items[i - 1].Volume : 0];

                            //Value of adding the next item VS Value of NOT adding the item
                            valueArray[i, curWeight, curVolume] = Math.Max((nextItem + prevState), valueArray[i - 1, curWeight, curVolume]);
                        }
                        //If the next item cannot be added set it to the previous state
                        else
                        {
                            valueArray[i, curWeight, curVolume] = valueArray[i - 1, curWeight, curVolume];
                        }
                    }
                }
            }

            //int maxValue = valueArray[n, maxWeight, maxVolume];
            //Next we need to "Trace Back" to get the actual solution
            //Start at the max K[n,W]
            int remWeight = maxWeight;
            int remVolume = maxVolume;

            for (int i = n; i > 0; i--)
            {
                //IF moving from i[index-1] -> i[index-2] is the same than
                // i[index-1] is NOT part of the solution
                // however if they are different than it is.
                if (valueArray[i, remWeight, remVolume] != valueArray[(i - 1), remWeight, remVolume])
                {
                    KSItem choosenItem = items[i - 1];
                    OptimalSolution.TryAddItem(choosenItem);
                    remWeight -= choosenItem.Weight;

                    if (includeVolume)
                    {
                        remVolume -= choosenItem.Volume;
                    }
                }
            }

            //If we are including Volume than use it else use 1
            SolutionCount = n * maxWeight * (includeVolume ? maxVolume : 1);
        }