Example #1
0
        public string utterWord(Agent speaker)
        {
            Dictionary <string, double> vocabulary = speaker.getVocabulary().getVocabulary();

            if (EALoop.RandomInt(0, 100) <= 40)
            {
                return(newWord());
            }
            if (vocabulary.Count == 0)
            {
                return(newWord());
            }

            double sum        = 0;
            var    sortedDict = from entry in vocabulary orderby entry.Value descending select entry;

            foreach (var i in sortedDict)
            {
                sum += i.Value;
            }
            double rnd  = EALoop.RandomDouble();
            double prob = 0;

            foreach (var i in sortedDict)
            {
                prob += i.Value / sum;
                if (rnd <= prob)
                {
                    return(i.Key);
                }
            }
            return(vocabulary.ElementAt(EALoop.RandomInt(0, vocabulary.Count)).Key);
        }
Example #2
0
        public Genome()
        {
            genomeNormalised = new List <double>();
            genomeValues     = new List <double>();
            for (int i = 0; i < 10; i++)
            {
                genomeValues.Add(0.0);
            }
            int n = 20;

            genomeValues[0] = EALoop.RandomInt(0, 100);                                                                                      // Random value
            genomeValues[1] = (genomeValues[0] * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100)));                             // Close to value #0
            genomeValues[5] = (100 - genomeValues[0]) * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                       // Opposite of #0
            genomeValues[6] = genomeValues[5] * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                               // Close to value #5
            genomeValues[7] = genomeValues[6] * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                               // Close to value #6

            genomeValues[3] = EALoop.RandomInt(0, 100);                                                                                      // Random value
            genomeValues[4] = genomeValues[3] * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                               // Close to value #3
            genomeValues[8] = (100 - genomeValues[4]) * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                       // Opposite of #4
            genomeValues[9] = genomeValues[8] * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n)) / 100));                               // Close to value 8

            genomeValues[2] = ((genomeValues[1] + genomeValues[3]) / 2) * (1 + (System.Convert.ToDouble(EALoop.RandomInt(0, n / 2)) / 100)); // Close to #3 and #1

            //-- Normalise genome --//
            genomeNormalised = normalise(genomeValues);
        }
Example #3
0
 public void mutate(double p)
 {
     if (EALoop.RandomDouble() <= p)
     {
         //double mutateRate = (EALoop.RandomInt(0,4) - 2) / 10; // Blir et tall mellom -0.2 og 0.2
         //if (mutateRate == 0.0) { mutateRate = 1.01; }
         //int index = EALoop.RandomInt(0,genomeValues.Count);
         genomeValues[EALoop.RandomInt(0, genomeValues.Count)] = EALoop.RandomInt(0, 100);
         genomeNormalised = normalise(genomeValues);
     }
 }
Example #4
0
        private string newWord()
        {
            int    length = EALoop.RandomInt(1, 5);
            string word   = "";

            for (int i = 0; i < length; i++)
            {
                int n = EALoop.RandomInt(0, 26);
                word += newLetter(n);
            }
            return(word);
        }
Example #5
0
        public Agent selectSpeaker(List <Agent> pop)
        {
            double sum = 0;

            foreach (Agent a in pop)
            {
                sum += a.getFitness();
            }
            double rnd = EALoop.RandomDouble();
            double n   = 0;

            foreach (Agent a in pop)
            {
                n += a.getFitness() / sum;
                if (rnd <= n)
                {
                    return(a);
                }
            }
            return(pop[EALoop.RandomInt(0, pop.Count)]);
        }
Example #6
0
        public List <Agent> makeChildren(List <Agent> population, SocialNetwork socialNetwork)
        {
            EALoop        ea       = new EALoop();
            List <Agent>  Children = new List <Agent>();
            List <Thread> ts       = new List <Thread>();

            //Console.WriteLine("making " + (populationSize - n )+ " children.");
            for (int i = population.Count; i < populationSize; i++)
            {
                Thread t = new Thread(new ThreadStart(() => Children.AddRange(ea.breed(population, socialNetwork))));
                t.Name = String.Format("t{0}", i + 1);
                t.Start();
                ts.Add(t);
            }
            foreach (var t in ts)
            {
                t.Join();
            }
            //Console.WriteLine("Number of children: " + Children.Count);
            return(Children);
        }
Example #7
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);
        }
Example #8
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");
        }