Example #1
0
 private void Redraw()
 {
     lock (currentDrawing)
     {
         guiDrawing = currentDrawing.Clone();
     }
     pnlCanvas.Invalidate();
     lastRepaint  = DateTime.Now;
     lastSelected = selected;
 }
        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;
                        }
                    }
                }
            }
        }
Example #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;
            }
        }
Example #4
0
        private void RepaintCanvas()
        {
            if (currentDrawing == null)
            {
                return;
            }

            lock (currentDrawing)
            {
                guiDrawing = currentDrawing.Clone();
            }
            pnlCanvas.Invalidate();
            lastRepaint  = DateTime.Now;
            lastSelected = Project.Selected;
        }
        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);
        }
Example #6
0
        private DnaDrawing GetMutatedSeedSyncedDrawing()
        {
            DnaDrawing newDrawing = parentDrawing.Clone();

            newDrawing.Mutate(info);
            return(newDrawing);
        }
Example #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
                }
            }
        }
Example #8
0
        private static void SaveDnaDrawingToFile(string fileName, DnaDrawing drawing)
        {
            DnaDrawing clone;

            clone = drawing.Clone();
            if (clone != null)
            {
                Serializer.Serialize(clone, fileName);
            }
        }
Example #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
            }
        }
Example #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);
        }
Example #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));
        }
Example #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;
            }
        }
Example #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);
                }
            }
        }
Example #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;
                }
            }
        }
Example #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);
        }
Example #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();
            }
        }
Example #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();
        }
Example #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);
                }
            }
        }