Example #1
0
    private AgentEntity.EntityType MostProfitableAgentType()
    {
        float sumOfProfits            = 0;
        float meanProfit              = 0;
        float highestMeanProfit       = 0;
        List <AgentEntity> listOfType = new List <AgentEntity>();

        AgentEntity.EntityType mostProfitable = new AgentEntity.EntityType();

        foreach (AgentEntity.EntityType type in Enum.GetValues(typeof(AgentEntity.EntityType)))
        {
            listOfType = Agents.Where(x => x.Type == type).ToList();
            foreach (AgentEntity agent in listOfType)
            {
                sumOfProfits += agent.ProfitLedger.Skip(Math.Max(0, agent.ProfitLedger.Count() - 15)).ToList().Sum();
            }

            int numberOfType = Agents.Count(x => x.Type == type);

            if (numberOfType > 0)
            {
                meanProfit = sumOfProfits / Agents.Count(x => x.Type == type);
                if (meanProfit > highestMeanProfit)
                {
                    highestMeanProfit = meanProfit;
                    mostProfitable    = type;
                }
            }
        }

        return(mostProfitable);
    }
Example #2
0
 void RightBtnCmdHandler()
 {
     if (CurrentIndex < Agents.Count() - 1)
     {
         ++CurrentIndex;
     }
 }
Example #3
0
        private double CalculateAverageFitness(double totalFitness)
        {
            double averageFitness = 0;

            if (Agents.Count != 0)
            {
                averageFitness = totalFitness / Agents.Count();
            }
            return(averageFitness);
        }
Example #4
0
        public string ToString(int numberOfTabs)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Message: \r\n" + getTabs(numberOfTabs) + "Encrypted: " + Encrypted.ToString());

            if (Nonces.Count != 0)
            {
                sb.Append("\r\n" + getTabs(numberOfTabs) + "Nonce(s): ");
                foreach (Nonce nonce in Nonces)
                {
                    sb.Append(nonce.id);
                }
            }

            if (Keys.Count() != 0)
            {
                sb.Append("\r\n" + getTabs(numberOfTabs) + "Key(s): ");
                foreach (Key key in Keys)
                {
                    sb.Append(key.ToString());
                }
            }

            if (Messages.Count() != 0)
            {
                sb.Append("\r\n" + getTabs(numberOfTabs) + "Message(s): ");
                foreach (Message m in Messages)
                {
                    sb.Append(m.ToString(numberOfTabs + 1));
                }
            }

            if (Agents.Count() != 0)
            {
                sb.Append("\r\n" + getTabs(numberOfTabs) + "Agent(s): ");
                foreach (Agent a in Agents)
                {
                    sb.Append(a.Id);
                }
            }

            return(sb.ToString());
        }
        public void RunOnce()
        {
            var currentAgentIndex = 0;

            foreach (var agent in Agents)
            {
                var randoms      = GetThreeRandomAgentIndexes(Agents.Count() - 1, currentAgentIndex);
                var randomAgent1 = Agents[randoms[0]];
                var randomAgent2 = Agents[randoms[1]];
                var randomAgent3 = Agents[randoms[2]];

                var randomDimension = random.Next(0, dimensions - 1);

                double[] potentialNewPosition = new double[agent.Position.Length];
                for (int d = 0; d < dimensions; d++)
                {
                    if (CrossoverProbablity > random.NextDouble() || randomDimension == d)
                    {
                        potentialNewPosition[d] = randomAgent1.Position[d] + DifferentialWeight * (randomAgent2.Position[d] - randomAgent3.Position[d]);
                    }
                    else
                    {
                        potentialNewPosition[d] = agent.Position[d];
                    }
                }

                double newPositionFitness = FitnessCalculator(potentialNewPosition);
                if (newPositionFitness > FitnessCalculator(agent.Position))
                {
                    agent.Position = potentialNewPosition;
                    if (!BestFitness.HasValue || newPositionFitness > BestFitness)
                    {
                        BestFitness = newPositionFitness;
                    }
                }
                currentAgentIndex++;
            }
        }
Example #6
0
    public void RemoveBankruptAgents()
    {
        AgentEntity   removedAgent;
        List <Market> removedAgentsMarkets = new List <Market>();

        for (int i = 0; i < Agents.Count; i++)
        {
            if (Agents[i].Currency <= 0)
            {
                removedAgent         = Agents[i];
                removedAgentsMarkets = removedAgent.Markets.Values.ToList();
                RemoveAgent(removedAgent);
                if (Agents.Count(x => x.Type == removedAgent.Type) < 1)
                {
                    SpawnAgent(removedAgent.Type);
                }
                else
                {
                    SpawnMostProfitableAgent();
                }
            }
        }
    }
Example #7
0
        public override byte[] Serialize()
        {
            List <byte> s = new List <byte>();

            // serialize Board
            s.Add((byte)((Board == null) ? 0 : 1));
            if (Board != null)
            {
                List <byte> tmp16 = new List <byte>();
                tmp16.AddRange(BitConverter.GetBytes((uint)Board.Count()));
                while (tmp16.Count > 0 && tmp16.Last() == 0)
                {
                    tmp16.RemoveAt(tmp16.Count - 1);
                }
                s.Add((byte)tmp16.Count);
                s.AddRange(tmp16);

                foreach (var tmp17 in Board)
                {
                    s.Add((byte)((tmp17 == null) ? 0 : 1));
                    if (tmp17 != null)
                    {
                        List <byte> tmp18 = new List <byte>();
                        tmp18.AddRange(BitConverter.GetBytes((uint)tmp17.Count()));
                        while (tmp18.Count > 0 && tmp18.Last() == 0)
                        {
                            tmp18.RemoveAt(tmp18.Count - 1);
                        }
                        s.Add((byte)tmp18.Count);
                        s.AddRange(tmp18);

                        foreach (var tmp19 in tmp17)
                        {
                            s.Add((byte)((tmp19 == null) ? 0 : 1));
                            if (tmp19 != null)
                            {
                                s.Add((byte)((sbyte)tmp19));
                            }
                        }
                    }
                }
            }

            // serialize Agents
            s.Add((byte)((Agents == null) ? 0 : 1));
            if (Agents != null)
            {
                List <byte> tmp20 = new List <byte>();
                tmp20.AddRange(BitConverter.GetBytes((uint)Agents.Count()));
                while (tmp20.Count > 0 && tmp20.Last() == 0)
                {
                    tmp20.RemoveAt(tmp20.Count - 1);
                }
                s.Add((byte)tmp20.Count);
                s.AddRange(tmp20);

                foreach (var tmp21 in Agents)
                {
                    s.Add((byte)((tmp21.Key == null) ? 0 : 1));
                    if (tmp21.Key != null)
                    {
                        List <byte> tmp22 = new List <byte>();
                        tmp22.AddRange(BitConverter.GetBytes((uint)tmp21.Key.Count()));
                        while (tmp22.Count > 0 && tmp22.Last() == 0)
                        {
                            tmp22.RemoveAt(tmp22.Count - 1);
                        }
                        s.Add((byte)tmp22.Count);
                        s.AddRange(tmp22);

                        s.AddRange(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(tmp21.Key));
                    }

                    s.Add((byte)((tmp21.Value == null) ? 0 : 1));
                    if (tmp21.Value != null)
                    {
                        s.AddRange(tmp21.Value.Serialize());
                    }
                }
            }

            // serialize Scores
            s.Add((byte)((Scores == null) ? 0 : 1));
            if (Scores != null)
            {
                List <byte> tmp23 = new List <byte>();
                tmp23.AddRange(BitConverter.GetBytes((uint)Scores.Count()));
                while (tmp23.Count > 0 && tmp23.Last() == 0)
                {
                    tmp23.RemoveAt(tmp23.Count - 1);
                }
                s.Add((byte)tmp23.Count);
                s.AddRange(tmp23);

                foreach (var tmp24 in Scores)
                {
                    s.Add((byte)((tmp24.Key == null) ? 0 : 1));
                    if (tmp24.Key != null)
                    {
                        List <byte> tmp25 = new List <byte>();
                        tmp25.AddRange(BitConverter.GetBytes((uint)tmp24.Key.Count()));
                        while (tmp25.Count > 0 && tmp25.Last() == 0)
                        {
                            tmp25.RemoveAt(tmp25.Count - 1);
                        }
                        s.Add((byte)tmp25.Count);
                        s.AddRange(tmp25);

                        s.AddRange(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(tmp24.Key));
                    }

                    s.Add((byte)((tmp24.Value == null) ? 0 : 1));
                    if (tmp24.Value != null)
                    {
                        s.AddRange(BitConverter.GetBytes((int)tmp24.Value));
                    }
                }
            }

            // serialize Constants
            s.Add((byte)((Constants == null) ? 0 : 1));
            if (Constants != null)
            {
                s.AddRange(Constants.Serialize());
            }

            return(s.ToArray());
        }