Ejemplo n.º 1
0
 private void Redraw()
 {
     lock (currentDrawing)
     {
         guiDrawing = currentDrawing.Clone();
     }
     pnlCanvas.Invalidate();
     lastRepaint  = DateTime.Now;
     lastSelected = selected;
 }
Ejemplo n.º 2
0
        private void StartEvolution()
        {
            SetupSourceColorMatrix();// Sets up the Mona lisa using x and y cordinates to create an matrix.
            if (currentDrawing == null)
            {
                currentDrawing = GetNewInitializedDrawing();
            }
            lastSelected = 0;

            while (isRunning)
            {
                DnaDrawing newDrawing;
                lock (currentDrawing)
                {
                    newDrawing = currentDrawing.Clone();
                }
                newDrawing.Mutate();// Multiple levels: With probability x either adds, removes or moves polygons. Next level changes polygons shape, color
                if (newDrawing.IsDirty)
                {
                    generation++;
                    double newErrorLevel = FitnessCalculator.GetDrawingFitness(newDrawing, sourceColors);

                    if (newErrorLevel <= errorLevel)
                    {
                        selected++;
                        lock (currentDrawing)
                        {
                            currentDrawing = newDrawing;
                        }
                        errorLevel = newErrorLevel;
                        if (selected % 500 == 0)
                        {
                            string local_path_name = "C:\\Users\\raunonaksi\\OneDrive\\Advanced Algorithmics - MTAT.03.238\\Project\\Pildid\\";
                            string filename        = "result";
                            string dot_filetype    = ".png";
                            FitnessCalculator.b.Save(local_path_name + filename + generation.ToString() + dot_filetype, System.Drawing.Imaging.ImageFormat.Png);
                            FitnessCalculator.b.Save("C:\\Users\\raunonaksi\\result.png");
                        }
                    }
                    else if (Settings.Activetemperature > 1 && newErrorLevel * Settings.Activeacceptable < errorLevel && Settings.UseSimulatedAnnealing)
                    {
                        Settings.Activetemperature = Settings.Activetemperature - Settings.Activetemperature * Settings.Activecooling;
                        if (Tools.GetRandomNumber(0, 100000) < Settings.Activetemperature)
                        {
                            currentDrawing = newDrawing;
                            errorLevel     = newErrorLevel;
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void tmrRedraw_Tick(object sender, EventArgs e)
        {
            if (currentDrawing == null)
            {
                return;
            }

            int    polygons = currentDrawing.Polygons.Count;
            int    points   = currentDrawing.PointCount;
            double avg      = 0;

            if (polygons != 0)
            {
                avg = points / polygons;
            }

            toolStripStatusLabelFitness.Text    = errorLevel.ToString();
            toolStripStatusLabelGeneration.Text = generation.ToString();
            toolStripStatusLabelSelected.Text   = selected.ToString();
            toolStripStatusLabelPoints.Text     = points.ToString();
            toolStripStatusLabelPolygons.Text   = polygons.ToString();
            toolStripStatusLabelAvgPoints.Text  = avg.ToString();

            bool shouldRepaint = false;

            if (repaintIntervall.Ticks > 0)
            {
                if (lastRepaint < DateTime.Now - repaintIntervall)
                {
                    shouldRepaint = true;
                }
            }

            if (repaintOnSelectedSteps > 0)
            {
                if (lastSelected + repaintOnSelectedSteps < selected)
                {
                    shouldRepaint = true;
                }
            }

            if (shouldRepaint)
            {
                guiDrawing = currentDrawing.Clone();
                pnlCanvas.Invalidate();
                lastRepaint  = DateTime.Now;
                lastSelected = selected;
            }
        }
Ejemplo n.º 4
0
        private void RepaintCanvas()
        {
            if (currentDrawing == null)
            {
                return;
            }

            lock (currentDrawing)
            {
                guiDrawing = currentDrawing.Clone();
            }
            pnlCanvas.Invalidate();
            lastRepaint  = DateTime.Now;
            lastSelected = Project.Selected;
        }
Ejemplo n.º 5
0
        public double GetNextErrorLevel()
        {
            var drawing = new DnaDrawing();

            drawing.SourceImage = sourceImage;
            drawing.Polygons    = new List <DnaPolygon>();
            int i = 0;

            foreach (LayeredWorker worker in workers)
            {
                i++;

                if (i == 1)
                {
                    continue;
                }

                DnaDrawing workerDrawing = worker.GetDrawing();
                drawing.Polygons.AddRange(workerDrawing.Clone().Polygons);
                //drawing = worker.GetDrawing();
            }

            currentDrawing = drawing;

            currentErrorLevel = FitnessCalculator.GetDrawingFitness(drawing, sourceImage);
            return(currentErrorLevel);
        }
Ejemplo n.º 6
0
        private DnaDrawing GetMutatedSeedSyncedDrawing()
        {
            DnaDrawing newDrawing = parentDrawing.Clone();

            newDrawing.Mutate(info);
            return(newDrawing);
        }
Ejemplo n.º 7
0
        private void StartEvolution()
        {
            SetupSourceColorMatrix();
            if (currentDrawing == null)
            {
                currentDrawing = GetNewInitializedDrawing();
            }
            lastSelected = 0;

            using (var fitnessCalculator = new FitnessCalculator())
            {
                while (isRunning)
                {
                    DnaDrawing newDrawing;
                    lock (currentDrawing)
                    {
                        newDrawing = currentDrawing.Clone();
                    }

                    newDrawing.Mutate();

                    if (newDrawing.IsDirty)
                    {
                        generation++;

                        double newErrorLevel = fitnessCalculator.GetDrawingFitness(newDrawing, sourcePixels);

                        if (newErrorLevel <= errorLevel)
                        {
                            selected++;
                            lock (currentDrawing)
                            {
                                currentDrawing = newDrawing;
                            }

                            errorLevel = newErrorLevel;
                        }
                    }

                    //else, discard new drawing
                }
            }
        }
Ejemplo n.º 8
0
        private static void SaveDnaDrawingToFile(string fileName, DnaDrawing drawing)
        {
            DnaDrawing clone;

            clone = drawing.Clone();
            if (clone != null)
            {
                Serializer.Serialize(clone, fileName);
            }
        }
Ejemplo n.º 9
0
        private void StartEvolution()
        {
            if (_currentDrawing == null)
            {
                _currentDrawing = GetNewInitializedDrawing(_sourceBitmap.Width, _sourceBitmap.Height);
            }

            while (IsRunning)
            {
                DnaDrawing newDrawing;
                lock (_currentDrawing)
                {
                    newDrawing = _currentDrawing.Clone();
                }
                newDrawing.Mutate();

                _candidates++;

                if (newDrawing.IsDirty)
                {
                    _generation++;

                    double newErrorLevel = _fitnessCalculator.GetDrawingFitness(newDrawing);

                    if (newErrorLevel <= _errorLevel)
                    {
                        _selected++;
                        lock (_currentDrawing)
                        {
                            _currentDrawing = newDrawing;
                        }
                        _errorLevel = newErrorLevel;

                        Console.WriteLine("{0} {1} {2}",
                                          _selected,
                                          _generation,
                                          _errorLevel);
                    }
                }
                //else, discard new drawing
            }
        }
Ejemplo n.º 10
0
        public DefaultEvolutionJob(DnaDrawing drawing, JobInfo info)
        {
            this.info = info;

            if (drawing == null)
            {
                drawing = GetNewInitializedDrawing(info);
            }
            lock (drawing)
            {
                currentDrawing = drawing.Clone();
            }

            currentErrorLevel = FitnessCalculator.GetDrawingFitness(currentDrawing, info.SourceImage);
        }
Ejemplo n.º 11
0
        private static Tuple <DnaDrawing, double> Work(DnaDrawing drawing, Color[,] originColors)
        {
            var newDrawing = drawing.Clone();

            newDrawing.Mutate();

            if (!newDrawing.IsDirty)
            {
                return(new Tuple <DnaDrawing, double>(null, 0.0));
            }

            var newErrorLevel = FitnessCalculator.GetDrawingFitness(newDrawing, originColors);

            return(new Tuple <DnaDrawing, double>(newDrawing, newErrorLevel));
        }
Ejemplo n.º 12
0
        private void OpenDNA()
        {
            Stop();

            DnaDrawing drawing = Serializer.DeserializeDnaDrawing(FileUtil.GetOpenFileName(FileUtil.DnaExtension));

            if (drawing != null)
            {
                lock (currentDrawing)
                {
                    currentDrawing = drawing;
                    guiDrawing     = currentDrawing.Clone();
                }
                pnlCanvas.Invalidate();
                lastRepaint = DateTime.Now;
            }
        }
Ejemplo n.º 13
0
        private void SaveDNA()
        {
            string fileName = FileUtil.GetSaveFileName(FileUtil.DnaExtension);

            if (string.IsNullOrEmpty(fileName) == false && guiDrawing != null)
            {
                DnaDrawing clone = null;
                lock (guiDrawing)
                {
                    clone = guiDrawing.Clone();
                }
                if (clone != null)
                {
                    Serializer.Serialize(clone, fileName);
                }
            }
        }
Ejemplo n.º 14
0
        private void StartEvolution()
        {
            if (currentDrawing == null)
            {
                currentDrawing = GetNewInitializedDrawing();
            }

            lastSelected = 0;
            NewFitnessCalculator calc = new NewFitnessCalculator();

            readyToBreed = false;

            while (isRunning)
            {
                DnaDrawing newDrawing;
                lock (currentDrawing)
                {
                    newDrawing = currentDrawing.Clone();
                }
                newDrawing.Mutate();

                if (newDrawing.IsDirty)
                {
                    generation++;

                    double newErrorLevel = calc.GetDrawingFitness(newDrawing, sourceColours);

                    if (newErrorLevel <= _errorLevel)
                    {
                        selected++;
                        selectedThisGeneration++;
                        lock (currentDrawing)
                        {
                            currentDrawing = newDrawing;
                        }
                        _errorLevel = newErrorLevel;
                    }
                }

                if (metCompletionCritera)
                {
                    readyToBreed = true;
                }
            }
        }
Ejemplo n.º 15
0
        public double GetNextErrorLevel()
        {
            Generations = 0;
            DnaDrawing newDrawing = currentDrawing.Clone();

            while (newDrawing.IsDirty == false)
            {
                newDrawing.Mutate(info);
                Generations++;
            }

            double newErrorLevel = FitnessCalculator.GetDrawingFitness(newDrawing, info.SourceImage);

            if (newErrorLevel <= currentErrorLevel)
            {
                currentDrawing    = newDrawing;
                currentErrorLevel = newErrorLevel;
            }

            return(newErrorLevel);
        }
Ejemplo n.º 16
0
        private void OpenDNA()
        {
            Stop();

            DnaDrawing drawing = Serializer.DeserializeDnaDrawing(FileUtil.GetOpenFileName(FileUtil.DnaExtension));

            if (drawing != null)
            {
                if (currentDrawing == null)
                {
                    currentDrawing = new DnaDrawing();
                }
                lock (currentDrawing)
                {
                    currentDrawing  = drawing;
                    guiDrawing      = currentDrawing.Clone();
                    Project.Drawing = currentDrawing;
                }
                ResetProjectLevels();
                RepaintCanvas();
            }
        }
Ejemplo n.º 17
0
        private void OpenProject()
        {
            Stop();

            string     fileName = FileUtil.GetOpenFileName(FileUtil.ProjectExtension);
            DnaProject project  = Serializer.DeserializeDnaProject(fileName);

            if (project != null)
            {
                Project = project;

                if (!string.IsNullOrEmpty(Project.ImagePath))
                {
                    OpenImage(Project.ImagePath);
                }

                if (Project.Drawing != null)
                {
                    if (currentDrawing == null)
                    {
                        currentDrawing = new DnaDrawing();
                    }

                    lock (currentDrawing)
                    {
                        currentDrawing = Project.Drawing;
                        guiDrawing     = currentDrawing.Clone();
                    }
                }
                ActivateProjectSettings();

                ResetProjectLevels();
                RepaintCanvas();

                projectFileName = fileName;
            }

            SetTitleBar();
        }
Ejemplo n.º 18
0
        private void StartEvolution()
        {
            var sourceImage = new SourceImage
            {
                Pixels = SetupSourceColorMatrix(picPattern.Image as Bitmap),
                Width  = picPattern.Width,
                Height = picPattern.Height
            };

            var info = new JobInfo
            {
                Settings    = Project.Settings,
                SourceImage = sourceImage,
            };
            //IEvolutionJob job = new LayeredEvolutionJob(sourceImage, 4);

            //DefaultEvolutionJob job = new DefaultEvolutionJob(sourceImage, currentDrawing);
            //IEvolutionJob job = new DefaultEvolutionJob(info);
            IEvolutionJob job = new ClusteredEvolutionJob(info);

            while (Project.IsRunning)
            {
                double newErrorLevel = job.GetNextErrorLevel();
                var    defJob        = job as DefaultEvolutionJob;
                if (defJob != null)
                {
                    Project.Generations += defJob.Generations;
                }

                Project.Mutations++;

                if (newErrorLevel <= Project.ErrorLevel)
                {
                    Project.Selected++;

                    if (newErrorLevel < Project.ErrorLevel)
                    {
                        Project.Positive++;
                    }
                    else
                    {
                        Project.Neutral++;
                    }

                    DnaDrawing newDrawing = job.GetDrawing();
                    if (currentDrawing == null) // to make always lockable...
                    {
                        currentDrawing = new DnaDrawing();
                    }

                    lock (currentDrawing)
                    {
                        currentDrawing  = newDrawing;
                        Project.Drawing = currentDrawing.Clone();
                    }

                    Project.ErrorLevel = newErrorLevel;

                    SaveAnimationImage(newDrawing);
                }
            }
        }