this tutorial will start by running the prebuilt

genetic algorithm simple robotics java 8 application and 2nd i will use the generated output in

order to quickly explain what this application does and 3rd i will step by step write and

explain the code using an eclipse ide this is the prebuilt application so let me go ahead

and run it so given those route coordinates we are using genetic algorithm in order to

evolve a solution that lets a robot autonomously navigate from BEGIN and go through ROUTE the

different ROUTE until it reaches the END and for simplicity the robot here has only 4 detectors

so backward right left and forward and each one of those can be either 0 or 1 depending

on what it detects meaning if it detects a route than it is 0 and if it detects a wall

than it is 1 and the robot can perform one of four actions it can do nothing and we represent

that as 00 and it can move forward and we represent that as 01 so here it is moving

forward from position [0,0] to position [1,0] and it can turn clockwise and we represent

that as 10 so here the robot position [1,0] turns clockwise and it can turn counter-clockwise

and we represent that as 11 and the combination of detectors we represent that as an integer

so for example this is 13 as an integer this is 14 as an integer and this is 03 as an integer

so this is 03 here and for this combination of detectors this is the action that is taken

10 meaning turn clockwise and for example here for 14 we have 01 which is move forward

and the robot direction is from the point of view of the robot itself so if we are in

this direction so BACKWARD RIGHT LEFT and FORWARD and if the robot turns clockwise that

it goes into this direction and here if it turns clockwise than it goes into this direction

and same thing if it turns clockwise it goes into this direction and this if it turns counter

clockwise it goes into this and this counter clockwise it goes into this and (2) counter

clockwise it goes into (1) and (1) counter clockwise becomes (4) so initially the robot

is in this position so forward has a WALL_ so to overcome that it turns counter-clockwise

and now it is in position (4) so FORWARD is this way so it goes ahead and moves forward

and here in position [1,0] it turns clockwise so FORWARD is downward now we’re in this position

so it can move to position [1,1] so [1,0] move forward we go into [1,1] and we keep

going [1,1] we move forward go here etc. so this Fittest Solution we evolved it from here

from generation 0 and this is the population of potential solutions and this is the fitness

for each solution and they are sorted by fitness so we start from this generation

and after 250 generations we end up with this fittest solution that we are printing over

here and this is what allows the robot to go from BEGIN to END__ so from position [0,0]

to position [0,5] i will start by creating a new project

and this will be running on JAVA 8 and here i will have a Driver class with a main method

and it will be in this package and this will be the Driver for the whole application and

a Solution class and this represents the action that the robot

would take when faced with each set of detectors and a Population class and this represents

a population of potential solutions and a GeneticAlgorithm class where selection crossover

mutation and elitism will be implemented and next i’ll have a Route class that will be

in this package and this represents the path that an autonomous robot navigator would be

traversing and i will refer to the beginning of the path so this is 1 the end of the path

is 3 and a wall is represented by a 0 and a route that to be traversed is represented

by 2 and let’s take this as a potential route coordinates start from BEGIN and we end with

END__ here and the robot would be traversing all those routes and this class will only

have 1 method getRoutePositionValue and since we’re usion Position here let’s go ahead and

define that class so it represents an [x,y] position in the route so we have and y and

they are initialized in the constructor and will have another constructor that instantiates

a new Position instance given a pre-existing position and let’s have get and set methods

for those and y coordinates and a toString method that returns those coordinates in this

format as a String in this format so let’s go back to the Route class and let’s have

a return value that will be initially returning 0 a WALL_ and return that return value and

if the x and y coordinates are valid here we’re checking for that

than the return value will be the coordinates so Route.coordinates and this is position.getY

and position.getX so we either return a WALL_ meaning 0 or we return BEGIN or END__ or ROUTE

given the position in the coordinates so BEGIN here is [0,0] coordinates ROUTE is [1,0] etc.

and the last class we will be creating here is the Navigator class which represents and

autonomous robot navigator and the directions from the point of view of that robot would

be either BACKWARD or LEFT or FORWARD or RIGHT and the moves that it can take are either

it can DO_NOTHING this is represented by a 0 or MOVE_FORWARD represented by a 1 or TURN_CLOCKWISE

or TURN_COUNTER_CLOCKWISE and next let’s go to the Solution class and i’ll define this

NUMB_OF_DIGITS to be 32 and the reason this is is that we have 4 detectors so we have

16 possible set of detectors so we represent those here from 0 to 15 we represent them

as integers and we have here 4 actions that are possible the DO_NOTHING MOVE_FORWARD TURN_CLOCKWISE

and TURN_COUNTER_CLOCKWISE and we represent those as 00 for 0 and 01 for 1 and 10 for

2 and 11 for 3 so we need 2 digits to represent those 4 possibilities so here we have 2 digits

for each possible set of detectors and we have 16*2 gives us 32 so this is the number

of digits being 32 and let me define this ArrayList that would have those 32 digits

so each one of those and the solution will have a fitness so let me initially define

this fitness and set it to -1 and i’ll define a boolean fitnessFlag and set it initially

to false and i need a getDigits method that would return this ArrayList of digits and

i will have a constructor that will instantiate this ArrayList given the NUMB_OF_DIGITS and

initialize it initially with nulls and than populate it with 0s or 1 randomly and i need

a method that calculates the fitness for a solution and for now let’s return 0 and the

getFitness method will check the fitnessFlag that we defined over here and if it is false

than we recalculate the fitness otherwise we return the fitness that we already have

and here each 2 digits represents an action so we need a method that would return those

actions so i’ll have this getActions method and the number of actions is essentially the

digits size over 2 since each 2 would make an action and let’s instantiate the ArrayList

to be returned given the number of actions and return it but before doing that let’s

add the various inter actions here so we end up with 16 possible actions in the

ArrayList that is returned and i need a toString method that would print this as a for each

solution instance so we are adding this between each 2 digits that represent an action and

returning this String and before writing the calculateFitness method let’s go to the Navigator

and so the Navigator will have one public method which would be activate

and let’s return null for now so given an ArrayList of actions of 16 actions we return

an ArrayList of Positions that this autonomous robot Navigator will be taking will be moving

though so let me go back to the Solution class and here in calculateFitness we will be calling

Navigator so a new instance of Navigator will be calling activate method on it and passing

in the actions calling the getActions method that would return the actions for this Solution

instance and before recalculating the fitness i will set the existing fitness to 0 and instantiate

an isVisited ArrayList that keeps track of the visited positions and let me here return

the fitness the calculated fitness and before doing that i’ll set the fitness flag to true

and for each position here i’ll check that it’s not already visited and if that position

is a ROUTE position than i’ll add 1 to the fitness and make it as a visited position

so this should do it for this class and next we’ll be doing the Population class here we

will have an ArrayList of potential solutions and a get method that would return those solutions

and a sort method that would sort those solutions by fitness and it’s using java 8 and it would

return a sorted ArrayList of solutions and i will have a constructor that takes in a

population size and based on that we will instantiate an ArrayList of solutions

and populate it with new random solutions so this should do it for this class and an

example population is here let’s say this is the generation that we are in and those

are the detectors as integers and this is the sorted ArrayList of potential solutions

and they are sorted by fitness so notice the highest fitness is first next let’s go to

the GeneticAlgorithm class and here i will have a private method that does population

crossover and for now i’ll return null and another private method that does population

mutation and for now i’ll also return null and the only public method that we will have

in this class is this evolve method that takes in a population and ends up calling crossoverPopulation

and the population that is returned from that it calls mutatePopulation on it and returns

the evolved populaion and crossover here is using tournament selection so we will end

up using this method and for now let’s just return null and we will have this private

crossoverSolution method that does crossover on those two solutions and for now let’s just

return null and mutatePopulation will end up making calls on this mutateSolution method

and let’s return the mutated solution here and before proceeding here let’s go to the

Driver class and define those constants so the maximum number of generations let’s say

it’s 250 and the population size let’s say it’s 10 potential solutions and the mutation

rate and the crossover rate and the number of elite solutions and the tournament selection

size let’s say it’s 4 and we only have 1 solution that get passed as is from one generation

to the next and now let’s go back to the GeneticAlgorithm

class and in crossoverPopulation the first thing we want to do is make sure the population

passed in is sorted so sortByFitness and let’s instantiate a new population that we want

to return so crossoverPopulation based on the solution so population.getSolutions().size()

of the passed in population and we will be returning this crossover population but before

doing that we will pick the elite solutions and set them

as is in this new crossover population to be returned and as we just mentioned we only

have 1 solution that is elite here so this is 1 the number of elite solutions as for

the remaining solutions to be populated in this crossover population we start from 1

here from the number of elite solutions and we go to the size of how many solutions we

have in the passed in population so we pick solution1 is we do population.getSolutions().get

a solution from there and than for solution2 we do selectTournamentPopulation and we pick

the fittest solution from that population that is returned here and than we call crossoverPopulation

or actually i meant tosay we call crossoverSolution on solution1 and solution2 and we set what

is returned here in crossoverPopulation now in mutatePopulation so initially we do pretty

much the same as we did in crossoverPopulation so we sort the passed in population and than

we instantiate a new population we’ll call it mutatePopulation and this mutated population

will be the one that we will be returning and we pass the elite solutions as is tothe

new mutated population as for the remaining solutions we populate this mutatePopulation

we start from the number of elite solutions so from 1 here and we go to the size of the

solutions that we have and we end up calling mutateSolution on each one of those solutions

and we set the returned solution in this mutatePopulation that we end up returning now for crossoverSolution

we will instantiate this crossoverSolution and end up returning it but before doing that

we’ll check the CROOSOVER_RATE if it is bigger than a random number than we will perform

the crossover otherwise crossoverSolution is going to equal to solution1 passed in here

so here i’ll pickup a random swap position i’m using math.random here and depending on

where we are with respect to that random swap position we’ll pickup from solution1 or from

solution2 and for mutateSolution than we will go through each digit in the digits ArrayList

and check if the MUTATION_RATE is bigger than a random number and we would flip bits here

and return the mutated solution and what remains here is this method so we will have this tournamentPopulation

that we would instantiate based on the TOURNAMENT_SELECTION_SIZE which here we have it to be 4 in this case

so we will be returning a sorted tournament population sorted by fitness and before doing

the return i will randomly populatethis tournamentPopulation with 4 random solutions from the passed in

population so this should do it for this class next let’s write the code for the autonomous

robot or the Navigator class so here we only have 1 public method activate and we will

have several private methods so one of them is stepForward when the navigator needs to

step forward and changeDirection when it needs to turn clockwise or counter-clockwise and

we need a method that would return a set of detectors as an integer solet’s for now just

return 0 and this method will end up calling this other private method calculateDetectorsAsInteger

so lt’s define the MAX_NUMB_OF_MOVES that this robot can take and let’s say it is 50

and this counter keeps track of the number of moves that the robot have already taken

and in the activate method let’s say we have a continue flag that we start with true and

we will pickup the solutionAction so here we’re passing the actions for a solution so

we have a specific solutionAction based on the sate of the detectors and we pickup those

as an integer and i would increase the number of moves of the robot and let’s have here

an instance variable a route instance variable and a currentRoutePosition and we start from

position (0,0) so if we are not at the end of the route and if we haven’t exceeded the

maximum number of moves than we do a switch statement on the solutionAction and this can

be one of those 4 things do nothing move forward turn clockwise and turn counter clockwise

and let me define a routeString as an ArrayList and a routePositions so with the routePositions

we are keeping track of the positions on the route but the routeString is for display purposes

so initially i will add these to the routeString the Position the Detectors and the Detectors

As Integer and the Action and let me define here the direction and it will be initially

Direction.FORWARD from here and detectorAsInteger instance variable and i’ll set it initially

to -1 so now here in this switch statement i’ll have the case where the action is do

nothing meaning 00 or move forward 01 or turn clockwise 10 or turn couter clockwise 11 and

in case it is do nothing i’ll set the continueFlag to false incase it is move forward i’ll call

stepForward passing in the current route position and in case it is turn clockwise i’ll call

changeDirection and pass it the turn direction and same for turn counter clockwise and i

am adding here to the routeString what is to be displayed and i’ll reset the detectorsAsInteger

to -1 and if we have reached the route end or if we have exceeded the max number of moves

i’ll go ahead and set the continueFlag to false and this will be returning the routePositions

ArrayList that we defined over here and initially in the constructor i added the current route

position which was 0 position 00 and for the stepForward method this would be the code

and here we stay in the same position if we hit a wall otherwise we add a new position

so currentPosition to the routePositions ArrayList so if we are stepping forward and the direction

is BACKWARD than we do currentPosition.getY() -1 if it is FORWARD than we do currentPosition.getY()

+1 and if that direction is LEFT we do currentPosition.setX currentPosition.getX()+1 and if it is RIGHT

we do currentPosition.setX currentPosition.getX()-1 now in case we need to change direction this

is the code that would do that so if the current direction is BACKWARD and if the turnDirection

is TURN_CLOCKWISE than the new direction will be Direction.LEFT otherwise if the turnDirection

is TURN_COUNTER_CLOCKWISE than the new direction will be Direction.Right and same concept for

FORWARD LEFT and RIGHT and for getRouteCurrentPositionDetectorsAsInteger this will be the code that does that and this

will call calculateDetectorsAsInteger so this method which will have this code and the logic

of how we came up with here 1 and 2 and 4 and 8 is over here so for example we ahve

8 here so for the next 8 we start 0 we add 8 1 we add 8 and 2 we add 8 and than 3 we

add 8 etc 7 we add 8 and so we end up adding those which give us this which would be 15

and here which would be 14 13 etc and same concept here we have the 4 so for the next

4 we start from 0 we add 4 1 we add 4 2 we add 4 3 we add 4 so this + 4 and + 4 +4 and

+ 4 gives us this which would be 7 6 5 and 4 and same for 2 so we have those 2 and the

next 2 we add 2 0 we add 2 1 we add 2 so this is how we come up with a number form 0 to

15 given 4 different detectors and the detector is detecting if there is a wall or there is

no wall 0 or 1 actually if there is a wall it is 1 and otherwise it is 0 and here the

string here is for display purposes the routeString so here we pickup the frontPosition leftPosition

rightPosition and backPosition depending on what the direction is and we pass it to the

calculateDetectorsAsInteger which would return or actually it would set the detectorsAsInteger

that we defined over here so this should do it for this class next let’s do the Driver

class so let’s start by picking up a new instance of Driver and we will start from generation

0 and i will instantiate a GeneticAlgorithm and a Population given the population size

which is 10 and i will calculate the fitness for each solution in the population and than

sort the population by fitness and i will have this method that just printout the population

and we’ll call it from here for generation 0 so let’s test run this application before

finishing up the code so generation 0 and those are the setectors as integers so 16

possibilities and this is each one of the potential solutions with the fitness for each

so this one has the highest fitness next let’s go ahead and evolve the population so i’ll

have a while loop while we’re smaller or equal to the max number of generations which we

defined here to be 250 generations i will first go ahead and evolve the population and

than calculate the fitness for each solution in the population of the solutions and than

sort the population by fitness and printout that population so let’s go ahead and test

run it again before finishing up the remaining code so here we go we went to generation 250

and we started from generation 0 so this was the fittest solution here and this is the

fittest solution here and let me add another method that does printing so printFittestSolution

and here i’ll pickup the fittest solution for

the last generation and than print it so let’s run it again before adding more code so here

we go this is the fittest solution so we printed it out here so those are the detectors as

integers and those are the actions for each set of detectors

and if we come back here i’ll add 3 methods that also does printing so printRouteMapDetails

all what it does is prints out this route coordinates that we defined here and this

is just for display purposes and this will print what the robot is doing and this would

print the direction basically when the robot changes direction so from the point of view

of the robot if we are in this position and we turn clockwise than we end up in this position

etc (2)+turn clockwise end up in (3) (3) turn clockwise we end up with (4) and (4)+turn

clockwise we end up with (1) and (4)+turn counter clockwise we end up with (3) (3) turn

counter clockwise we end up with (2) and (2) turn counter clockwise we end up with (1)

and (1) turn counter clockwise we end up with (4) so i will be calling those 3 methods from

here and now let’s go ahead and test run the application the finished application so here

we go this is the fittest solution and those are the route coordinates and this is the

robot navigate route coordinates so here we’re in position (0,0) (0,1)(1,0) so this is (0,0)

and this is (0,1) and this is (1,0) and actually this is (1,1) and this is (1,0) so here the

different positions and what the robot is doing here it is turning counter clockwise

and than it is moving forward so it is position (0,0) and than (0,0) it moves forward and

than it turns clockwise move forward etc until it reaches position (0,5) which is here the

END__ position

how can i get gui for it/

I can not find the code ;( from where I can see it

Genetic Algorithms w/ Java – Tutorial 01 @ https://youtu.be/UcVJsV-tqlo

Genetic Algorithms w/ JAVA + JavaFX – Tutorial 02 @ https://youtu.be/0VijcOA5ZAY

TSP By Genetic Algorithms w/ JAVA @ https://youtu.be/Z3668A0zLCM

Genetic Algorithms Tutorial 04 – Class Scheduling JAVA Application @ https://youtu.be/cn1JyZvV5YA

Genetic Algorithms Tutorial 06 – data mining + JAVA 8 + logical operators @ https://youtu.be/Ha9xddd7xTY

Genetic Algorithms Tutorial 07 – data mining + arithmetic operators + JAVA @ https://youtu.be/azdlGExeXKc

Class Scheduling w/ Genetic Algorithm + SQLite DB + JAVA @ https://youtu.be/PQPs-vJuHC0

Genetic Algorithms w/ Python – Tutorial 01 @ https://youtu.be/zumC_C0C25c

Genetic Algorithms Class Scheduling w/ Python Tutorial @ https://youtu.be/8NrNX_jCkjw

Genetic Algorithm + SQLite DB + Python + Class Scheduling @ https://youtu.be/6SDQdx5VLO8

Genetic Algorithms w/ Scala – Tutorial 01 @ https://youtu.be/6ngju74tHbI