public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            Parallel.For(0, parents.Length, i =>
            {
                AdjacencyGraphIndividual p = (AdjacencyGraphIndividual)parents[i];
                AdjacencyGraphIndividual o = (AdjacencyGraphIndividual)p.Clone();

                if (Rnd.NextDouble() < MutationProbability)
                {
                    var candidates = from n in o.StartingRoom.Neighbors where n.IsFree select n;
                    int size       = candidates.Count();
                    o.StartingRoom.IsStartingRoom = false;

                    if (candidates.Any())
                    {
                        GraphNode neo      = candidates.ElementAt(Rnd.Next(size));
                        neo.IsStartingRoom = true;
                        o.StartingRoom     = neo;
                    }
                }

                offspring[i] = o;
            });

            return(offspring);
        }
        public Individual[] Operate(Individual[] parents)
        {
            Individual[] offspring = new Individual[parents.Length];

            Parallel.For(0, parents.Length, i =>
            {
                AdjacencyGraphIndividual p = (AdjacencyGraphIndividual)parents[i];
                AdjacencyGraphIndividual o = (AdjacencyGraphIndividual)p.Clone();

                if (Rnd.NextDouble() < MutationProbability)
                {
                    byte branchId = o.BranchStarters.Keys.ElementAt(Rnd.Next(o.BranchStarters.Count));

                    GraphNode next = o.BranchStarters[branchId];
                    GraphNode last = null;

                    do
                    {
                        GraphNode tmp = (from n in next.Neighbors where n.BranchId == next.BranchId && n != last select n).First();
                        last          = next;
                        next          = tmp;
                    }while (next != null);

                    GraphNode newLast = (from n in last.Neighbors where n.BranchId == branchId select n).First();
                    last.BranchId     = 0;
                    last.Status       = GraphNode.BranchRoomStatus.None;
                    newLast.Status    = GraphNode.BranchRoomStatus.Ending;
                }

                offspring[i] = o;
            });

            return(offspring);
        }
Esempio n. 3
0
        /*class PathNode
         * {
         *  public GraphNode Node { get; set; }
         *  public int Cost { get; set; }
         *
         *  public PathNode(GraphNode node, int cost)
         *  {
         *      Node = node;
         *      Cost = cost;
         *  }
         *
         *  public override int GetHashCode()
         *  {
         *      return Node.GetHashCode();
         *  }
         *
         *  public override bool Equals(object obj)
         *  {
         *      PathNode other = (PathNode)obj;
         *      return Node == other.Node;
         *  }
         * }*/

        public double Evaluate(Individual ind)
        {
            AdjacencyGraphIndividual graph = (AdjacencyGraphIndividual)ind;

            HashSet <GraphNode>                  closed   = new HashSet <GraphNode>();
            Dictionary <GraphNode, int>          distance = new Dictionary <GraphNode, int>();
            HashedFibonacciHeap <GraphNode, int> open     = new HashedFibonacciHeap <GraphNode, int>(0);

            open.Insert(graph.StartingRoom, 0);

            while (!open.IsEmpty())
            {
                GraphNode current = open.RemoveMin();
                closed.Add(current);

                foreach (GraphNode neighbor in current.Neighbors)
                {
                    if (closed.Contains(neighbor))
                    {
                        continue;
                    }

                    int alt = distance[current] + 1;

                    if (distance.ContainsKey(neighbor) && alt >= distance[neighbor])
                    {
                        continue;
                    }

                    distance[neighbor] = alt;

                    if (open.Contains(neighbor))
                    {
                        open.DecreasePriority(neighbor, distance[neighbor]);
                    }
                    else
                    {
                        open.Insert(neighbor, distance[neighbor]);
                    }
                }
            }

            double fitness = 0;

            foreach (GraphNode starter in graph.BranchStarters.Values)
            {
                fitness += distance[starter];
            }

            // f/C ^ -1
            return(graph.BranchStarters.Count / fitness);
        }
Esempio n. 4
0
        static void Phase2()
        {
            EvolutionaryAlgorithm  eva   = new EvolutionaryAlgorithm();
            MapBlueprintIndividual input = (MapBlueprintIndividual)Individual.FromFile(@"C:\Users\Jan\Source\Repos\RoguelikeEva\Results\door test\_RESULT 9.bin");
            var graph = ExtractRoomAdjacencyGraph(input.Rooms.First().Key, input);

            eva.Operators.Add(new ExtendBranchMutation(1));

            /*eva.Operators.Add(new CompressBranchMutation(1));
             * eva.Operators.Add(new ReplaceBranchMutation(1));
             * eva.Operators.Add(new ReverseBranchMutation(1));
             * eva.Operators.Add(new ShiftStartingRoomMutation(1));*/

            eva.MultiObjective.Add(new BranchRatioFitness(3.0 / 2));
            eva.MultiObjective.Add(new BranchEntryPointsFitness());

            eva.SampleIndividual = new AdjacencyGraphIndividual(new List <byte>(input.Rooms.Keys), 3, 3);
            eva.HvIndicator      = new BiobjectiveHvIndicator();

            using (StreamWriter file = new StreamWriter(@"C:\Users\Jan\Source\Repos\RoguelikeEva\Results\_out.txt"))
            {
                file.WriteLine("Gen\tHv");
                int solutionNo = 0;

                IEnumerable <Individual> result = eva.Run(e => e.Population.No == 10, 100, file, (writer, gen, hv) =>
                {
                    writer.WriteLine("{0}\t{1}", gen.No, hv);

                    Console.Clear();
                    Console.WriteLine("Current generation: " + gen.No);
                });

                foreach (Individual ind in result)
                {
                    AdjacencyGraphIndividual map = (AdjacencyGraphIndividual)ind;

                    // TODO:
                    // transform "map" to correct MapBlueprint

                    // "correct MapBlueprint".SaveImageToFile(@"C:\Users\Jan\Source\Repos\RoguelikeEva\Results\_RESULT " + solutionNo + ".png", 1536, 1024);
                    map.SaveToFile(@"C:\Users\Jan\Source\Repos\RoguelikeEva\Results\_RESULT " + (solutionNo++) + ".bin");
                }
            }
        }
Esempio n. 5
0
        public double Evaluate(Individual ind)
        {
            AdjacencyGraphIndividual graph = (AdjacencyGraphIndividual)ind;
            int @in = 0, @out = 0;

            foreach (GraphNode node in graph.Gene.Values)
            {
                if (node.BranchId > 0)
                {
                    @in++;
                }

                else
                {
                    @out++;
                }
            }

            return(1.0 / Math.Abs((double)@in / @out - Ratio + 1));
        }