예제 #1
0
        public void addDiscreteGraph(List <Agent> pop, SocialNetwork n, int generation)
        {
            string        s          = "";
            string        sep        = ",";
            List <string> addedEdges = new List <string>();

            foreach (var a in pop)
            {
                s += a.getID().ToString() + sep;
                if (n.getAgentsConnections(a) != null)
                {
                    foreach (var b in n.getAgentsConnections(a))
                    {
                        if (b.Value >= 0.0)  //&& !(addedEdges.Contains(a.getID().ToString() + b.Key.getID().ToString())) )
                        {
                            s += b.Key.getID().ToString() + sep;
                            addedEdges.Add(a.getID().ToString() + b.Key.getID().ToString());
                        }
                    }
                    s += "\n";
                }
            }
            string filename = "C:/Users/andrl/Desktop/masterStuff/MasterData/Figures/Experiment 8/Graph" + generation.ToString() + ".txt";

            System.IO.File.WriteAllText(@filename, s);
        }
예제 #2
0
        private void addFitnessDegreeData(List <Agent> population, SocialNetwork socialNetwork, DataCollector d)
        {
            double C = 0.5;
            double allConnections     = 0;
            double AvgWeight          = 0;
            double learnRateSum       = 0;
            double totalVocLen        = 0;
            double speakToParentsGene = 0;
            double extrovertProb      = 0;

            foreach (Agent a in population)
            {
                double agentsAvgWeight   = 0;
                double agentsConnections = 0;
                speakToParentsGene += a.getGenome().getNormalisedGenome()[5] * speakToParentsConstant;
                List <double> genome = a.getGenome().getValuesGenome();
                extrovertProb += ((genome[3] + (100 - genome[6]) / 200) * C) / 100;
                totalVocLen   += a.getVocabulary().getVocabulary().Count;
                List <double> normGenome = a.getGenome().getNormalisedGenome();
                learnRateSum += (normGenome[0] + normGenome[8] + normGenome[9] - normGenome[4] - normGenome[1] - normGenome[7]);
                if (socialNetwork.getAgentsConnections(a) != null && socialNetwork.getAgentsConnections(a).Count > 0)
                {
                    foreach (var i in socialNetwork.getAgentsConnections(a))
                    {
                        if (i.Value > 0.0)
                        {
                            agentsConnections++;
                            agentsAvgWeight += i.Value;
                        }
                    }
                    if (agentsConnections > 0)
                    {
                        AvgWeight      += (agentsAvgWeight / agentsConnections);
                        allConnections += agentsConnections;
                    }
                }
            }
            Console.WriteLine("Speak to parents prob: " + (speakToParentsGene / population.Count));
            Console.WriteLine("Extrovert probability: " + (extrovertProb / population.Count));
            Console.WriteLine("average vocabulary length: " + (totalVocLen / population.Count));
            Console.WriteLine("Fittest agent: " + population[0].getFitness());
            Console.WriteLine("Average connections: " + (allConnections / population.Count));
            Console.WriteLine("Agents average weight: " + (AvgWeight / population.Count));
            Console.WriteLine("Average learn rate: " + (learnRateSum / population.Count) + "\n\n");
            d.addExtrovertData((extrovertProb / population.Count));
            d.addSpeakToParentsGenome((speakToParentsGene / population.Count));
            d.addAvgVocLen((totalVocLen / population.Count));
            d.addFittest(population[0].getFitness());
            d.setDegree(allConnections / population.Count);
            d.addLearnRate((learnRateSum / population.Count));
        }
예제 #3
0
 private void fitnessOfPopulation(List <Agent> population, SocialNetwork socialNetwork)
 {
     foreach (Agent a in population)
     {
         if (a != null && socialNetwork.getAgentsConnections(a) != null)
         {
             double fitness = a.calculateFitness(socialNetwork.getAgentsConnections(a));
             a.setFitness(fitness);
         }
         else
         {
             a.setFitness(0);
         }
         //Console.WriteLine("Vocabulary size: "+ a.getVocabulary().getVocabulary().Count + "\nFitness: "+ fitness+"\n");
     }
     population = population.OrderBy(agent => agent.getFitness()).ToList();
     population.Reverse();
 }
예제 #4
0
        public Agent selectListener(Agent agent, SocialNetwork net, List <Agent> population)
        {
            var    genome      = agent.getGenome().getValuesGenome();
            double P_extrovert = ((genome[3] + (100 - genome[6]) / 200) * C) / 100;
            Dictionary <Agent, double> connections = net.getAgentsConnections(agent);

            //if ((EALoop.RandomDouble() <= P_extrovert && agent.getZ() < maxExtroConv) || net.getAgentsConnections(agent) == null)
            if (EALoop.RandomDouble() <= P_extrovert)
            {
                // Extrovert
                //System.Console.WriteLine("EXTROVERT");
                Agent listener = population[EALoop.RandomInt(0, population.Count)];
                while (listener == agent)
                {
                    listener = population[EALoop.RandomInt(0, population.Count)];
                }
                agent.incrementZ();
                return(listener);
            }
            // Introvert
            double sum = 0;

            foreach (var friend in connections)
            {
                sum += friend.Value;
            }
            double random = EALoop.RandomDouble();
            double to     = 0;

            foreach (var friend in connections)
            {
                to += friend.Value / sum;
                if (random <= to)
                {
                    return(friend.Key);
                }
            }
            return(null);
        }
예제 #5
0
        static void Main(string[] args)
        {
            EALoop ea = new EALoop();

            Console.WriteLine("STARTING");
            SocialNetwork socialNetwork = new SocialNetwork();
            DataCollector data          = new DataCollector();
            List <Agent>  population    = new List <Agent>();
            Dialogue      dialogue      = new Dialogue();
            int           generations   = 1;

            for (int i = 0; i < populationSize; i++)
            {
                population.Add(new Agent(ea.AgentIdCounter));
                ea.AgentIdCounter++;
            }
            List <Agent> speakerPool = new List <Agent>();

            foreach (Agent a in population)
            {
                if (socialNetwork.getAgentsConnections(a) != null)
                {
                    speakerPool.Add(a);
                }
            }
            Console.WriteLine("Speaker pool: " + speakerPool.Count);
            ea.dialogueThreads(socialNetwork, speakerPool, population);

            ea.fitnessOfPopulation(population, socialNetwork);
            population = ea.survivalSelection(population, socialNetwork);
            data.addDiscreteGraph(population, socialNetwork, generations);
            data.addFitnessData(population);
            data.setDialogues(population);
            ea.addFitnessDegreeData(population, socialNetwork, data);
            data.setUniqueWords(population);

            ea.updateAges(population);


            while (generations < Totalgenerations)
            {
                generations++;
                Console.WriteLine("\nGeneration number: " + generations);
                //Console.WriteLine("Nodes in the socialnetwork: " + socialNetwork.socialNetwork.Count);

                //--   MAKE CHILDREN   --//
                //Console.WriteLine("Size of population before children is made: " + population.Count);
                population.AddRange(ea.makeChildren(population, socialNetwork));
                //Console.WriteLine("Size of population after children was made: " + population.Count);

                //--    PERFORM DIALOGUES   --//
                //Console.WriteLine("socialnetwork count  "+socialNetwork.socialNetwork.Count);
                speakerPool = new List <Agent>();
                for (int q = 0; q < population.Count; q++)
                {
                    //Console.WriteLine("index: " + q);
                    if (population[q] == null)
                    {
                        population.RemoveAt(q);
                    }
                    else if (socialNetwork.getAgentsConnections(population[q]) != null && socialNetwork.getAgentsConnections(population[q]).Count > 0)
                    {
                        speakerPool.Add(population[q]);
                    }
                }
                Console.WriteLine("Speaker pool: " + speakerPool.Count);
                ea.dialogueThreads(socialNetwork, speakerPool, population);

                //ea.succcessfullDialogues = 0;
                //Console.WriteLine("Dialogues performed");

                //--    CALCULATE FITNESS   --//
                ea.fitnessOfPopulation(population, socialNetwork);


                //--    SURVIVAL SELECTION   --//
                population = ea.survivalSelection(population, socialNetwork);

                //--   DATA GATHERING   --//
                data.setDialogues(population);
                data.setUniqueWords(population);
                data.addFitnessData(population);
                if (generations % 5 == 0)
                {
                    data.addDiscreteGraph(population, socialNetwork, generations);
                }
                ea.addFitnessDegreeData(population, socialNetwork, data);

                ea.updateAges(population);

                //Console.WriteLine("Size of population after survival selection: " + population.Count);
            }
            Console.WriteLine("fitness data: " + data.getFitnessdata().Count + "\ndialogue data: " + data.getDialogues().Count + "\nUnique words data: " + data.getUniqueWords().Count);
            data.writeToFiles();
            data.addDiscreteGraph(population, socialNetwork, generations);
            Console.Write("Press any button to end");
        }