Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic

Mastermind: An Evolutionary Computing Framework Demo

Rate me:
Please Sign up or sign in to vote.
4.29/5 (8 votes)
16 Sep 2004CPOL2 min read 51.2K   161   24   5
Demonstration 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 its 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.

VB.NET
'\\ --[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:

VB
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.

History

  • 16th September, 2004: Initial version

License

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


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
JokeCheers Pin
megaadam27-Apr-08 22:09
professionalmegaadam27-Apr-08 22:09 
GeneralSource download does not work Pin
Steven Campbell17-Sep-04 7:29
Steven Campbell17-Sep-04 7:29 
File not Found

The file '/useritems/Mastermind/Mastermind_src.zip' doesn't exist.


my blog
GeneralRe: Source download does not work Pin
Duncan Edwards Jones18-Sep-04 0:19
professionalDuncan Edwards Jones18-Sep-04 0:19 
GeneralRe: Source download does not work Pin
Abhijit.Roy24-Jan-06 21:38
Abhijit.Roy24-Jan-06 21:38 
GeneralRe: Source download does not work Pin
Duncan Edwards Jones24-Jan-06 22:15
professionalDuncan Edwards Jones24-Jan-06 22: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.