Esempio n. 1
0
        public IActionResult RequestCandidate(int algo, int maxLoad, string id)
        {
            IAssignTask assignTask  = default(IAssignTask);
            DateTime    requestTime = DateTime.Now;
            Target      currentUser = _dataBusiness.GetTarget(id).ToLibTarget();

            if (algo == 1)
            {
                assignTask = new NearestNeighborAlgorithm();
            }
            else if (algo == 2)
            {
                assignTask = new RoundRobinAlgorithm();
            }

            var stopWatch = Stopwatch.StartNew();

            (IList <Candidate>, Candidate)assigned = assignTask.AssignTo(_dataBusiness.GetCandidates().ToLibCandidates(), currentUser, maxLoad);
            stopWatch.Stop();

            DateTime assignedTime = DateTime.Now;

            ModifyTable(assigned.Item1, assigned.Item2, currentUser, requestTime, assignedTime, stopWatch.Elapsed, algo, maxLoad);

            if (algo == 1)
            {
                assigned.Item1 = assigned.Item1
                                 .OrderBy(x => x.DistanceToTarget)
                                 .ToList();
            }
            else if (algo == 2)
            {
                assigned.Item1 = assigned.Item1
                                 .OrderBy(x => x.Load)
                                 .ThenBy(x => x.DistanceToTarget)
                                 .ToList();
            }

            return(PartialView("_CandidatesToAssign", assigned.Item1
                               .Select(x => new CandidateViewModel
            {
                Id = x.Id,
                Name = _dataBusiness.GetCandidate(x.Id).Name,
                DistanceToTarget = x.DistanceToTarget,
                Load = x.Load,
                Latitude = x.Location.Latitude,
                Longitude = x.Location.Longitude
            })
                               .ToList()));
        }
Esempio n. 2
0
        public IActionResult Index()
        {
            IAssignTask nn       = new NearestNeighborAlgorithm();
            var         nnResult = nn.AssignTo(Mock.Candidates, Mock.Targets[0]);

            IAssignTask rr       = new RoundRobinAlgorithm();
            var         rrResult = rr.AssignTo(Mock.Candidates, Mock.Targets[1], 3);

            //List<Data.Candidate> mockCandidates =
            //    new List<Data.Candidate>(Mock.Candidates
            //    .Select(c => new Data.Candidate
            //    {
            //        Load = c.Load,
            //        Latitude = c.Location.Latitude,
            //        Longitude = c.Location.Longitude
            //    }));
            //string[] nameCandidates = { "Abdul", "Bambang", "Chairul", "Deni", "Edo" };
            //int[] travelCandidates = { 1520, 300, 610, 1280, 490 };

            //for (int i = 0; i < mockCandidates.Count ; i++)
            //{
            //    mockCandidates[i].Name = nameCandidates[i];
            //    mockCandidates[i].TotalTravel = travelCandidates[i];

            //    var candidate = _dataBusiness.ModifyCandidate(mockCandidates[i]);
            //}

            //List<Data.Target> mockTargets =
            //    new List<Data.Target>(Mock.Targets
            //    .Select(t => new Data.Target
            //    {
            //        Latitude = t.Location.Latitude,
            //        Longitude = t.Location.Longitude,
            //    }));
            //string[] nameTargets = { "Ujang", "Udin" };

            //for (int i = 0; i < mockTargets.Count; i++)
            //{
            //    mockTargets[i].Name = nameTargets[i];

            //    var target = _dataBusiness.ModifyTarget(mockTargets[i]);
            //}

            var candidates = _dataBusiness.GetCandidates();
            var targets    = _dataBusiness.GetTargets();

            return(View());
        }
Esempio n. 3
0
        public async Task InvokeAsync(HttpContext context)
        {
            var balancerAlgorithmSetting = LoadBalancerSettings.Current.BalancingAlgorithm.ToLowerInvariant().Replace(" ", "");

            BalancingAlgorithm balancerAlgorithm = null;

            if (balancerAlgorithmSetting == "roundrobin")
            {
                balancerAlgorithm = new RoundRobinAlgorithm();
            }
            else if (balancerAlgorithmSetting == "random")
            {
                balancerAlgorithm = new RandomAlgorithm();
            }
            else if (balancerAlgorithmSetting == "connectioncount")
            {
                balancerAlgorithm = new ConnectionCountAlgorithm();
            }

            context.Items["destination"] = balancerAlgorithm.GetInstance();

            await _next.Invoke(context);
        }
        public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Matches.Any())
            {
                return;
            }

            for (int i = 0; i < 4; i++)
            {
                var league = dbContext.Leagues.FirstOrDefault(m => m.Matches.Count == 0);
                var teams  = league.Teams.Select(t => t.Id).ToArray();

                var matches = new List <Match>();

                var roundingResult = new RoundRobinAlgorithm().GetCalculatedSchedule(teams.Count());
                var gameweek       = 1;

                foreach (var rounding3rdLevel in roundingResult)
                {
                    foreach (var rounding2ndLevel in rounding3rdLevel)
                    {
                        for (int round = 0; round < rounding2ndLevel.Length; round += 2)
                        {
                            var homeTeamId = teams[rounding2ndLevel[0] - 1];
                            var awayTeamId = teams[rounding2ndLevel[1] - 1];

                            matches.Add(new Match
                            {
                                HomeTeamId = homeTeamId,
                                AwayTeamId = awayTeamId,
                                LeagueId   = league.Id,
                                GameweekId = gameweek,
                            });
                        }
                    }

                    gameweek++;
                }

                foreach (var rounding3rdLevel in roundingResult)
                {
                    foreach (var rounding2ndLevel in rounding3rdLevel)
                    {
                        for (int round = 0; round < rounding2ndLevel.Length; round += 2)
                        {
                            var homeTeamId = teams[rounding2ndLevel[1] - 1];
                            var awayTeamId = teams[rounding2ndLevel[0] - 1];

                            matches.Add(new Match
                            {
                                HomeTeamId = homeTeamId,
                                AwayTeamId = awayTeamId,
                                LeagueId   = league.Id,
                                GameweekId = gameweek,
                            });
                        }
                    }
                    gameweek++;
                }
                await dbContext.Matches.AddRangeAsync(matches);

                await dbContext.SaveChangesAsync();
            }
        }