Click here to Skip to main content
13,869,083 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 9 Sep 2004
Licenced CPOL

Mastermind : An evolutionary computing framework demo

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


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")
        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))
                GenomeHealth = GetHealth(_Population.Item(TestGenome))
                If GenomeHealth > GetHealth(_HealthiestIndividual) Then
                    _HealthiestIndividual = _Population.Item(TestGenome)
                End If
                TotalHealth = TotalHealth + GenomeHealth
            End If
        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
                MaxIndex = MaxIndex - 1
            End If
        For TestGenome = 0 To _Population.PopulationSize - 2 Step 2
            Dim Parents As New MastermindGuessPopulation()
            Parents.AddGenome(_Population.Item(TestGenome + 1))
    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
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.


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
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

You may also be interested in...

Comments and Discussions

JokeCheers Pin
megaadam27-Apr-08 23:09
professionalmegaadam27-Apr-08 23:09 
GeneralSource download does not work Pin
Steven Campbell17-Sep-04 8:29
memberSteven Campbell17-Sep-04 8:29 
GeneralRe: Source download does not work Pin
Duncan Edwards Jones18-Sep-04 1:19
professionalDuncan Edwards Jones18-Sep-04 1:19 
GeneralRe: Source download does not work Pin
Abhijit.Roy24-Jan-06 22:38
memberAbhijit.Roy24-Jan-06 22:38 
GeneralRe: Source download does not work Pin
Duncan Edwards Jones24-Jan-06 23:15
professionalDuncan Edwards Jones24-Jan-06 23:15 

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

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

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