public ActionResult <MissionCreateDto> CreateMission(MissionCreateDto missionCreateDto)
        {
            var mission = _mapper.Map <Mission>(missionCreateDto);

            mission.OperationId = _operationRepository.GetActiveOperationId(UserId);
            _missionRepository.Create(mission);
            _missionRepository.SaveChanges();
            var missionReadDto = _mapper.Map <Mission>(mission);

            _notificationHub.Clients.All.SendAsync("SendMessage", "MissionCreated");
            _notificationHub.Clients.All.SendAsync("Notification", "Вам назначена миссия #" + mission.Id + ".");
            return(CreatedAtRoute(nameof(GetMissionById), new { Id = missionReadDto.Id }, missionReadDto)); //Return 201
        }
Example #2
0
        public ActionResult Clusters(IEnumerable <UserDto> userDtos)
        {
            if (userDtos.Count() < 1)
            {
                return(Ok());
            }
            int operationId  = _operationRepository.GetActiveOperationId(UserId);
            int num_clasters = userDtos.ToList().Count();
            var users        = new List <User> {
            };
            var objs         = _detectedObjectRepository.GetVacantObjects(operationId);

            if (userDtos.Count() == 0 || objs.Count() == 0 || objs.Count() < userDtos.Count())
            {
                return(Ok());
            }


            float[]               scores                = new float[3];
            List <PointF>[]       CentroidsList         = new List <PointF> [3];
            List <ClusterPoint>[] clusteredObjectsLists = new List <ClusterPoint> [3];

            int i = 0;

            foreach (var clusteredObjects in clusteredObjectsLists)
            {
                clusteredObjectsLists[i] = _clusteringService.GetClusters(num_clasters, objs, out scores[i], out CentroidsList[i++]).ToList();
            }

            int indexOfMinScore = Array.IndexOf(scores, scores.Min());

            foreach (var user in userDtos)
            {
                users.Add(_userRepository.GetById(user.Id));
            }

            double[,] distances = new double[num_clasters, num_clasters];
            i = 0;
            int j = 0;

            foreach (var centriods in CentroidsList[indexOfMinScore])
            {
                j = 0;
                foreach (var user in users)
                {
                    distances[i, j] = _clusteringService.Distance2(
                        new PointF
                    {
                        X = float.Parse(user.UserPositions.LastOrDefault(u => u.UserId == user.Id).X.Replace('.', ',')),
                        Y = float.Parse(user.UserPositions.LastOrDefault(u => u.UserId == user.Id).Y.Replace('.', ','))
                    },
                        new PointF
                    {
                        X = centriods.X,
                        Y = centriods.Y
                    }
                        );
                    j++;
                }
                i++;
            }

            Dictionary <int, (int, double)> bestDistances = new Dictionary <int, (int, double)>();

            //i = 0;
            //foreach (var user in userDtos)
            //{
            //    double[] arr = new double[num_clasters];
            //    for (j = 0; j < num_clasters; j++)
            //    {
            //        arr[j] = distances[i, j];
            //    }

            //    bestDistances.Add(i++, (user.Id, arr.Min())); //0 - 1 кластер, 1 - 2 кластер ...
            //}

            i = 0;
            List <UserDto> usersCopy = userDtos.ToList();

            for (i = 0; i < num_clasters; i++)
            {
                j = 0;
                Dictionary <int, double> arr = new Dictionary <int, double>();
                foreach (var user in usersCopy.ToList())
                {
                    arr.Add(user.Id, distances[i, j]);
                    j++;
                }
                var bestKeys = bestDistances.Keys.ToList();
                if (bestKeys.Count != 0)
                {
                    foreach (var item in bestKeys)
                    {
                        arr.Remove(item);
                    }
                }


                var minUser = arr.Aggregate((l, r) => l.Value < r.Value ? l : r).Key;

                var minDist = arr.Values.Min();

                bestDistances.Add(i, (minUser, minDist)); //0 - 1 кластер, 1 - 2 кластер ...
            }



            //var bestDistancesSort = bestDistances;
            var bestDistancesSort = bestDistances.OrderBy(r => r.Value.Item2);

            List <DetectedObject>[] detectedObjects = new List <DetectedObject> [num_clasters];
            i = 0;
            for (int q = 0; q < num_clasters; q++)
            {
                detectedObjects[i] = new List <DetectedObject> {
                };
                foreach (var clusterPoint in clusteredObjectsLists[indexOfMinScore].Where(p => p.ClusterNum == q))
                {
                    detectedObjects[i].Add(_detectedObjectRepository.GetById(clusterPoint.ObjectId));
                }
                i++;
            }


            //#region DEBUG
            ////DEBUG: добавляет обьекты соответствующие центроидам кластеров
            //i = 0;
            //foreach (var centroid in CentroidsList[indexOfMinScore])
            //{
            //    _detectedObjectRepository.Create(new DetectedObject
            //    {
            //        Description = "centroid_" + i,
            //        Title = "Centroid",
            //        X = centroid.X.ToString().Replace(',', '.'),
            //        Y = centroid.Y.ToString().Replace(',', '.'),
            //        OperationId = operationId,
            //        IsDesired = false
            //    });
            //    i++;
            //}
            //#endregion



            foreach (var user in bestDistancesSort)
            {
                _missionRepository.Create(
                    new Mission
                {
                    OperationId     = operationId,
                    UserId          = user.Value.Item1,
                    DetectedObjects = detectedObjects[user.Key]
                });
            }

            _detectedObjectRepository.SaveChanges();
            _notificationHub.Clients.All.SendAsync("Notification", "Миссии обновлены.");

            return(Ok());
        }
 public void Create(MissionEntity entity)
 {
     entity.IsDone = false;
     missionRepository.Create(entity.GetDalEntity());
     uow.Commit();
 }