Click here to Skip to main content
Click here to Skip to main content

Mastermind : An evolutionary computing framework demo

, 16 Sep 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Demontration on how to use the evolutionary computing framework

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)

Share

About the Author

Duncan Edwards Jones
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET
Follow on   Twitter   LinkedIn

Comments and Discussions

 
JokeCheers Pinmembermegaadam27-Apr-08 23:09 
GeneralSource download does not work PinmemberSteven Campbell17-Sep-04 8:29 
GeneralRe: Source download does not work PinmemberMerrion18-Sep-04 1:19 
GeneralRe: Source download does not work PinmemberAbhijitRoy24-Jan-06 22:38 
GeneralRe: Source download does not work PinmemberDuncan Edwards Jones24-Jan-06 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 Sep 2004
Article Copyright 2004 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid