예제 #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);
        }
예제 #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);
        }
예제 #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);
     }
 }
예제 #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);
        }
예제 #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)]);
        }
예제 #6
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);
        }