コード例 #1
0
        public static void Paths(Bee bees)
        {
            string path = "p: ";

            foreach (UInt32 vertex in bees.path)
            {
                path += vertex.ToString() + " ";
            }
            Console.WriteLine(path);
            Console.WriteLine();
        }
コード例 #2
0
        public static void CheckBestBee(Bee bee)
        {
            List <UInt32> p = bee.path;

            p.Sort();
            string lista = "p: ";

            foreach (UInt32 node in bee.path)
            {
                lista += node.ToString() + " ";
            }
            Console.WriteLine(lista);
        }
コード例 #3
0
        public static void Solve(Graph graph, UInt32 bees, UInt32 iterations, int steps)
        {
            /*List<UInt32> bestPath = new List<UInt32>();
             * double newPath = 0;
             *
             *
             *
             * for (int k = 0; k < graph.dimension; k++)
             * {
             *  bestPath.Add((UInt32)k);
             * }
             * double bestPathLength = graph.CalculateRouteLength(bestPath);
             * Console.WriteLine("Best path is: " + bestPathLength);
             * //Values.toFile.Add("-First best-" + 0 + "-" + bestPathLength);
             * Values.AddNewValues(0, bestPathLength);*/
            double bestPath;

            for (int i = 0; i < iterations; i++)
            {
                Bee[] hive    = new Bee[bees];
                Bee   bestBee = new Bee(graph);

                for (int b = 0; b < hive.Length; b++)
                {
                    hive[b] = new Bee(graph);
                }

                int epochs = 0;
                while (!bestBee.IsComplete(graph))
                {
                    //forward
                    int h = 0;
                    foreach (Bee bee in hive)
                    {
                        bee.ChooseNextNode(steps, graph, rand);
                        //Console.WriteLine("Bee nr {0} path count {1}", h, bee.path.Count);
                        h++;
                    }

                    //backward
                    hive    = hive.OrderBy(c => c.pathValue).ToArray();
                    bestBee = hive[0];

                    double cMax = hive[0].pathValue;
                    double cMin = hive[hive.Length - 1].pathValue;

                    List <Bee> recruiters = new List <Bee>();
                    foreach (Bee bee in hive)
                    {
                        double ob    = (cMax - bee.pathValue) / (cMax - cMin);
                        double probs = Math.Pow(Math.E, (-(1 - ob) / (bee.path.Count * 0.01)));
                        double r     = rand.NextDouble();
                        if (r > probs)
                        {
                            bee.ChangeStatus(true);
                            recruiters.Add(bee);
                        }
                        else
                        {
                            bee.ChangeStatus(false);
                        }
                    }
                    if (recruiters.Count == 0)
                    {
                        //int l = hive.Length / 3;
                        for (int l = 0; l < hive.Length; l++)
                        {
                            hive[l].ChangeStatus(true);
                            recruiters.Add(hive[l]);
                        }
                    }

                    double        divider = 0;
                    List <double> pr      = new List <double>();
                    foreach (Bee bee in recruiters)
                    {
                        divider += (cMax - bee.pathValue) / (cMax - cMin);
                    }

                    foreach (Bee bee in recruiters)
                    {
                        pr.Add((cMax - bee.pathValue) / (cMax - cMin) / divider);
                    }

                    List <double> cumulativeProbs = new List <double>();
                    for (int x = 0; x < pr.Count; x++)
                    {
                        double cumPr = 0;
                        for (int y = x + 1; y < pr.Count; y++)
                        {
                            cumPr += pr[y];
                            cumulativeProbs.Add(cumPr);
                        }
                    }

                    foreach (Bee bee in hive)
                    {
                        if (!bee.status)
                        {
                            double rndm        = rand.NextDouble();
                            Bee    selectedBee = new Bee(graph);
                            selectedBee = recruiters[0];
                            for (int k = 0; k < cumulativeProbs.Count; k++)
                            {
                                if (rndm < cumulativeProbs[k])
                                {
                                    selectedBee = recruiters[k];
                                    break;
                                }
                            }
                            //bee.path.Clear();
                            bee.SetPath(selectedBee.path);
                            bee.ChangeStatus(true);
                        }
                    }
                    //Console.WriteLine("-----{0}", bestBee.path.Count);

                    epochs++;
                    if (bestBee.IsComplete(graph))
                    {
                        //CheckBestBee(bestBee);
                        //Console.WriteLine("Solve in {0} epochs", epochs);
                        Console.WriteLine("--Best path in iteration {0} is: {1}", i, bestBee.pathValue);
                        Values.AddNewValues(i, bestBee.pathValue);
                        //Console.WriteLine();
                    }
                }

                bestBee = new Bee(graph);
                for (int l = 0; l < hive.Length; l++)
                {
                    hive[l] = new Bee(graph);
                }
            }
        }