Exemplo n.º 1
0
        public static Func <int[], int> GetScoreFunction(
            SeatsMap map,
            IEnumerable <Attendee> attendees,
            IEnumerable <Topic> topics)
        {
            var availableSeats = GetAvailableSeats(map);

            return((int[] solution) => {
                var seatsMap = SeatsWithAttendees(map, availableSeats, solution);
                var score = 0;
                availableSeats
                .Where(seat => seatsMap[seat.R, seat.C] > 0)
                .ToList()
                .ForEach(seat => {
                    var targetTopicIds = attendees.First(a => a.IndividualId == seatsMap[seat.R, seat.C]).TopicIds;
                    var neighboors = GetValidAdjacent(seat.R, seat.C, seatsMap);
                    neighboors.ToList().ForEach(neighboor => {
                        var seatValue = seatsMap[neighboor.R, neighboor.C];

                        if (seatValue == 0)
                        {
                            score -= 1;
                        }
                        else
                        {
                            var neigboorTopicIds = attendees.First(a => a.IndividualId == seatValue).TopicIds;
                            var commondIds = targetTopicIds.Intersect(neigboorTopicIds);
                            score += topics.Where(topic => commondIds.Contains(topic.Id)).Sum(t => 1);
                        }
                    });
                });
                return score;
            });
        }
Exemplo n.º 2
0
        public void BuildMapFromArray()
        {
            var map = new SeatsMap(3, 4, new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });

            Assert.Equal(7, map.Map[1, 2]);
            Assert.Equal(12, map.Map[2, 3]);
            Assert.Equal(1, map.Map[0, 0]);
        }
Exemplo n.º 3
0
        private static int[,] SeatsWithAttendees(SeatsMap map, Seat[] availableSeats, int[] attendeeIds)
        {
            var seatsMap = (int [, ])map.Map.Clone();

            for (int i = 0; i < availableSeats.Length; i++)
            {
                var seat = availableSeats[i];
                seatsMap[seat.R, seat.C] = attendeeIds[i];
            }
            return(seatsMap);
        }
Exemplo n.º 4
0
        private static Seat[] GetAvailableSeats(SeatsMap map)
        {
            var seats = new List <Seat>();

            for (int i = 0; i < map.Rows; i++)
            {
                for (int j = 0; j < map.Cols; j++)
                {
                    if (map.Map[i, j] == 1)
                    {
                        seats.Add(new Seat {
                            R = i, C = j
                        });
                    }
                }
            }
            return(seats.ToArray());
        }
        /**** Seats Map
         *     {0,1,1,1,0},
         *     {0,1,0,1,0},
         *     {0,0,0,1,0}
         */
        public Func <int[], int> PrepareScenario()
        {
            var seatsMap = new SeatsMap(
                3, 5,
                new int[] { 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 });


            var topics = new List <Topic>()
            {
                new Topic {
                    Id = "1", Weigth = 5
                },
                new Topic {
                    Id = "2", Weigth = 2
                }
            };

            var attendees = new List <Attendee>()
            {
                new Attendee()
                {
                    IndividualId = 111, TopicIds = new List <string>()
                    {
                        "1"
                    }
                },
                new Attendee()
                {
                    IndividualId = 222, TopicIds = new List <string>()
                    {
                        "2"
                    }
                },
                new Attendee()
                {
                    IndividualId = 333, TopicIds = new List <string>()
                    {
                        "1", "2"
                    }
                }
            };

            return(FitnessFunction.GetScoreFunction(seatsMap, attendees, topics));
        }
Exemplo n.º 6
0
        private IEnumerable <int> GetGenes(SeatsMap map, IEnumerable <Attendee> attendees)
        {
            var attendeesGenes = attendees.Select(attendee => attendee.IndividualId).ToList();

            var availableSeats = 0;

            for (int i = 0; i < map.Rows; i++)
            {
                for (int j = 0; j < map.Cols; j++)
                {
                    if (map.Map[i, j] > 0)
                    {
                        availableSeats++;
                    }
                }
            }

            var unassignedSeatsGenes = Enumerable.Range(1, availableSeats - attendeesGenes.Count).Select(x => x * -1);

            attendeesGenes.AddRange(unassignedSeatsGenes);
            return(attendeesGenes);
        }
        public void Should_Avoid_alone_attendee()
        {
            var seatsMap = new SeatsMap(
                3, 5,
                new int[] { 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0 });


            var topics = new List <Topic>()
            {
                new Topic {
                    Id = "1", Weigth = 1
                },
                new Topic {
                    Id = "2", Weigth = 1
                },
            };

            var attendees = new List <Attendee>()
            {
                new Attendee()
                {
                    IndividualId = 111, TopicIds = new List <string>()
                    {
                        "1"
                    }
                },
                new Attendee()
                {
                    IndividualId = 222, TopicIds = new List <string>()
                    {
                        "1"
                    }
                },
                new Attendee()
                {
                    IndividualId = 333, TopicIds = new List <string>()
                    {
                        "2"
                    }
                }
            };

            var getScore = FitnessFunction.GetScoreFunction(seatsMap, attendees, topics);

            /******Solution
             *  {0, 111, 222, X,  0},
             *  {0,  X,   0,  X,  0},
             *  {0,  0,   0, 333, 0}
             */

            var aloneSolution = new int[] { 111, 222, -1, -1, -1, 333 };

            /******Solution
             *  {0, 111, 222, X, 0},
             *  {0, 333,   0, X, 0},
             *  {0,  0,   0,  X, 0}
             */

            var groupSolution = new int[] { 111, 222, 333, -1, -1, -1 };

            Assert.True(getScore(groupSolution) > getScore(aloneSolution));
        }