5,692,513 members and growing! (17,110 online)
Email Password   helpLost your password?
Development Lifecycle » Design and Architecture » Design Patterns     Intermediate License: The Code Project Open License (CPOL)

Mastermind : An evolutionary computing framework demo

By Duncan Edwards Jones

Demontration on how to use the evolutionary computing framework
VB, Windows, .NETVS.NET2002, VS.NET2003, VS2005, Visual Studio, Dev

Posted: 9 Sep 2004
Updated: 16 Sep 2004
Views: 30,514
Bookmarked: 15 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.86 Rating: 4.09 out of 5
2 votes, 40.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
2 votes, 40.0%
5

Introduction

The mastermind application is a demonstration project to illustrate how you can use the Evolutionary Computing Framework to build a real program and to test how changing the different parameters of the evolutionary setup can affect it's problem solving ability. Essentially the application attempts to solve the standard "Mastermind" problem which is to discover the colour and position of a number of hidden pegs by guessing based on the feedback of the correctness of the guesses that precede it.

MastermindEnvironment : IEnvironment

The MasterMind environment class is an implementation of the IEnvironment that defines the correctness of each of the population of current guesses. To do this it has a copy of the correct peg layout and it compares each guess in the guess population against this assigning 5 points if the guess has the right colour peg in the wrong position and 50 points if the guess has the right colour peg in the right position. It then culls those that have less than the average number of points and breeds the new guess population from the remainder. Thus the population gets healthier each turn and nearer to the correct guess.

'\\ --[NextGeneration]-------------------------------------------------

'\\ Evaluates the health of each individual in the current population, 

'\\ killing off the least healthy and breeding from the rest

'\\ -------------------------------------------------------------------

Public Sub NextGeneration()
 
    If _Population.PopulationSize = 0 Then
        Throw New Exception("The population is extinct")
    Else
        Dim GenomeHealth As Integer
        Dim TotalHealth As Integer
        _HealthiestIndividual = Nothing
        Dim TestGenome As Integer
        For TestGenome = 0 To _Population.PopulationSize - 1
            If _HealthiestIndividual Is Nothing Then
                _HealthiestIndividual = _Population.Item(TestGenome)
                TotalHealth = GetHealth(_Population.Item(TestGenome))
            Else
                GenomeHealth = GetHealth(_Population.Item(TestGenome))
                If GenomeHealth > GetHealth(_HealthiestIndividual) Then
                    _HealthiestIndividual = _Population.Item(TestGenome)
                End If
                TotalHealth = TotalHealth + GenomeHealth
            End If
        Next
        Dim Averagehealth As Integer = TotalHealth/_Population.PopulationSize
        Dim MaxIndex As Integer = _Population.PopulationSize - 1
        For TestGenome = 0 To MaxIndex
            If TestGenome > MaxIndex Then
                Exit For
            End If
            GenomeHealth = GetHealth(_Population.Item(TestGenome))
            If GenomeHealth < Averagehealth OrElse GenomeHealth = 0 Then
                _Population.Kill(TestGenome)
                MaxIndex = MaxIndex - 1
            End If
        Next
        For TestGenome = 0 To _Population.PopulationSize - 2 Step 2
            Dim Parents As New MastermindGuessPopulation()
            Parents.AddGenome(_Population.Item(TestGenome))
            Parents.AddGenome(_Population.Item(TestGenome + 1))
            _Population.AddGenome(Breed(Parents))
        Next
    End If

End Sub

MastermindGuessPopulation : IPopulation

The mastermind guess population is all the current guesses at the peg layout. It is not much more than a type safe collection of MastermindGuessGenomes

MastermindGuessGenome : IGenome

The MastermindGuessGenome represents a single guess at the hidden peg set. It has one MastermindGuessGene for each of the available peg holes.

MastermindGuessGene : IGene

The mastermind guess gene is a single peg colour. The gene holds one of 8 possible colour values according to the enumerated type Peg_Colours

    Public Enum Peg_Colours
        White_Peg
        Black_Peg
        Green_Peg
        Blue_Peg
        Yellow_Peg
        Red_Peg
        Orange_Peg
        Brown_Peg
    End Enum

Other stuff

There is also a form that shows the current state of the game and another to allow you to alter the environment settings for each game. This allows you to investigate the effect of changing the population size or the number of peg holes on the number of generations needed to solve the problem

Comparison to an intelligent player

There are a couple of things that an intelligent player would do that this demonstration doesn't do. Firstly the intelligent player would never submit the same guess twice because this is obviously wrong. Also the intelligent player would swap the positions of the pegs if they were getting a number of "right colour in the wrong hole" feedback. In this implementation each peg has to evolve towards the right colour in situ. I will address this in a later version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones


Microsoft MVP 2006, 2007
Visual Basic .NET
Occupation: Software Developer (Senior)
Location: Ireland Ireland

Other popular Design and Architecture articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
JokeCheersmembermegaadam23:09 27 Apr '08  
GeneralSource download does not workmemberSteven Campbell8:29 17 Sep '04  
GeneralRe: Source download does not workmemberMerrion1:19 18 Sep '04  
GeneralRe: Source download does not workmemberAbhijitRoy22:38 24 Jan '06  
GeneralRe: Source download does not workmemberDuncan Edwards Jones23:15 24 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Sep 2004
Editor: Chris Maunder
Copyright 2004 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project