Ejemplo n.º 1
0
    public void Test(FCM fcm, int generations, string[] args)
    {
        var startTime = DateTime.Now;

        for (int generation = 0; generation < generations; generation++)
        {
            Console.WriteLine("\nTest generation: {0} of {1}", generation, generations);

            LoggerFactory.SetLogLevel(LogLevel.Warning);
            LoggerFactory.DeactivateConsoleLogging();


            SimulationStarter task = SimulationStarter.Start(this.modelDescription, args);

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            SimulationWorkflowState loopResults = task.Run();

            if (loopResults.IsFinished)
            {
                stopWatch.Stop();
                // Console.WriteLine($"Simulation execution finished in {stopWatch.ElapsedMilliseconds / 1000:N2} seconds");

                stopWatch.Restart();
                fcm.Run(false);
                stopWatch.Stop();
                // Console.WriteLine($"FCM finished in {stopWatch.ElapsedMilliseconds / 100:N2} seconds");

                GC.Collect();
            }
        }
    }
Ejemplo n.º 2
0
        public async Task <BaseDto <NotifInput> > Handle(CreateNotificationCommand request, CancellationToken cancellationToken)
        {
            var recieved = BackgroundJob.Enqueue(() => Subscriber.Recieved());

            var Data = request.data.attributes;

            // Add Notification Data
            var notificationData = new Notification_Model
            {
                title   = Data.title,
                message = Data.message
            };

            _context.notifications.Add(notificationData);
            await _context.SaveChangesAsync(cancellationToken);

            // Get Id for notification_id in notification logs
            var Id = await _context.notifications.ToListAsync();

            var userData = await _context.users.FindAsync(Data.from);

            // Add Notification Log Data
            foreach (var logs in Data.targets)
            {
                _context.notificationLogs.Add(new NotificationLogs
                {
                    notification_id   = Id.Last().id,
                    type              = Data.type,
                    from              = Data.from,
                    target            = logs.id,
                    email_destination = logs.email_destination
                });
                await _context.SaveChangesAsync();

                if (logs.email_destination != null)
                {
                    BackgroundJob.Enqueue(() => EmailSender.Send(notificationData.title, notificationData.message, userData.address, userData.name, logs.email_destination));
                }

                BackgroundJob.Enqueue(() => FCM.SendAsync(notificationData.title, notificationData.message));
            }

            return(new BaseDto <NotifInput>
            {
                message = "Success add Notification Data",
                success = true,
                data = Data
            });
        }
Ejemplo n.º 3
0
        public IHttpActionResult kiemtra(string device, string token)
        {
            try
            {
                FCM fcm = db.FCMs.FirstOrDefault(e => e.device.Equals(device) && e.token.Equals(token));

                if (fcm == null)
                {
                    return(StatusCode(HttpStatusCode.NotFound));
                }
                return(Ok(fcm));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 4
0
 public IHttpActionResult Xoa(int id)
 {
     try
     {
         FCM fcm = db.FCMs.FirstOrDefault(e => e.id == id);
         if (fcm == null)
         {
             return(StatusCode(HttpStatusCode.NoContent));
         }
         db.FCMs.DeleteOnSubmit(fcm);
         db.SubmitChanges();
         return(Ok());
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Ejemplo n.º 5
0
    public void Train(FCM fcm, int generations, float targetFitness, Boolean saveGenomes, string[] args)
    {
        var startTime = DateTime.Now;

        for (int generation = 0; generation < generations; generation++)
        {
            Console.WriteLine("\nGeneration: {0} of {1}", generation, generations);

            LoggerFactory.SetLogLevel(LogLevel.Warning);
            LoggerFactory.DeactivateConsoleLogging();

            SimulationStarter task = SimulationStarter.Start(this.modelDescription, args);

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            SimulationWorkflowState loopResults = task.Run();

            if (loopResults.IsFinished)
            {
                stopWatch.Stop();
                Console.WriteLine($"Simulation execution finished in {stopWatch.ElapsedMilliseconds / 1000:N2} seconds");

                stopWatch.Restart();
                fcm.Run(true, targetFitness, saveGenomes);
                stopWatch.Stop();

                Console.WriteLine($"FCM finished in {stopWatch.ElapsedMilliseconds / 100:N2} seconds");

                GC.Collect();
            }
        }

        string filename = FileUtils.CreateTimestampedFilename("Genomes", DateTime.Now, ".csv");

        fcm.WriteGenomes(filename);
        fcm.WriteGenomes("genomes.csv");
    }
Ejemplo n.º 6
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorker.ReportProgress(0, "working...");
            filteredImage = (Bitmap)OriImage.Image.Clone();
            int    numCluster    = (int)numericUpDown1.Value;
            int    maxIterations = (int)numericUpDown2.Value;
            double accuracy      = (double)numericUpDown3.Value;

            List <ClusterPoint> points = new List <ClusterPoint>();

            for (int row = 0; row < originalImage.Width; ++row)
            {
                for (int col = 0; col < originalImage.Height; ++col)
                {
                    Color c2 = originalImage.GetPixel(row, col);
                    points.Add(new ClusterPoint(row, col, c2));
                }
            }

            List <ClusterCentroid> centroids = new List <ClusterCentroid>();

            //Create random points to use a the cluster centroids
            Random random = new Random();

            for (int i = 0; i < numCluster; i++)
            {
                int randomNumber1 = random.Next(sourceImage.Width);
                int randomNumber2 = random.Next(sourceImage.Height);
                centroids.Add(new ClusterCentroid(randomNumber1, randomNumber2, filteredImage.GetPixel(randomNumber1, randomNumber2)));
            }

            FCM alg = new FCM(points, centroids, 2, filteredImage, (int)numericUpDown1.Value);

            int k = 0;

            do
            {
                if ((backgroundWorker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    k++;
                    alg.J = alg.CalculateObjectiveFunction();
                    alg.CalculateClusterCentroids();
                    alg.Step();
                    double Jnew = alg.CalculateObjectiveFunction();
                    Console.WriteLine("Run method i={0} accuracy = {1} delta = {2}", k, alg.J, Math.Abs(alg.J - Jnew));
                    precision.Text = "Precision " + Math.Abs(alg.J - Jnew);

                    //Format and Display the TimeSpan Value;
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds / 10);
                    duration.Text = "Duration: " + elapsedTime;

                    SegmentedImage.Image = (Bitmap)alg.getProcessedImage;
                    backgroundWorker.ReportProgress((100 * k) / maxIterations, "Iteration " + k);
                    if (Math.Abs(alg.J - Jnew) < accuracy)
                    {
                        break;
                    }
                }
            } while (maxIterations > k);
            Console.WriteLine("Done..");

            //Save the Segmented Image
            SegmentedImage.Image = (Bitmap)alg.getProcessedImage.Clone();
            alg.getProcessedImage.Save("SegmentedImage.png");

            //Create a new image for each cluster in order to extratc the feature
            double[,] Matrix = alg.U;
            Bitmap[] bmapArray = new Bitmap[centroids.Count];
            for (int i = 0; i < centroids.Count; i++)
            {
                bmapArray[i] = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppRgb);
            }

            for (int j = 0; j < points.Count; j++)
            {
                for (int i = 0; i < centroids.Count; i++)
                {
                    ClusterPoint p = points[j];
                    if (Matrix[j, i] == p.ClusterIndex)
                    {
                        bmapArray[i].SetPixel((int)p.X, (int)p.Y, p.OriginalPixelColor);
                    }
                }
            }

            //Save the image for each segmented Cluster

            for (int i = 0; i < centroids.Count; i++)
            {
                bmapArray[i].Save("Cluster" + i + ".png");
            }
            imgCluster1     = (Bitmap)bmapArray[0].Clone();
            imgCluster3     = (Bitmap)bmapArray[2].Clone();
            imgCluster2     = (Bitmap)bmapArray[1].Clone();
            pictClus1.Image = imgCluster1;
            pictClus2.Image = imgCluster2;
            pictClus3.Image = imgCluster3;

            // Resource Cleanup..
            backgroundWorker.ReportProgress(100, "Done in " + k + "iteration.");
            //getDataCluster();

            for (int i = 0; i < points.Count; i++)
            {
                points[i] = null;
            }
            for (int i = 0; i < centroids.Count; i++)
            {
                centroids[i] = null;
            }
            alg = null;
        }