From f1c7ac08ad84645c2f1219a40df500a8f9416a9b Mon Sep 17 00:00:00 2001
From: istkabra <kyrill.abrams@stud.h-da.de>
Date: Tue, 2 Jun 2020 16:14:07 +0200
Subject: [PATCH] * Backtracking overlaps are now impossible while initializing
 and single point mutations (still rarely happens because of crossovers) *
 Closes #1

---
 .../InitialGenerationCreators/RandomDirection.java    | 11 ++++-------
 src/main/java/Mutators/SinglePoint.java               |  9 ++++++++-
 src/main/resources/genetic.properties                 |  6 +++---
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/main/java/InitialGenerationCreators/RandomDirection.java b/src/main/java/InitialGenerationCreators/RandomDirection.java
index ecdc775..552c6ce 100644
--- a/src/main/java/InitialGenerationCreators/RandomDirection.java
+++ b/src/main/java/InitialGenerationCreators/RandomDirection.java
@@ -1,6 +1,5 @@
 package InitialGenerationCreators;
 
-import Enums.DirectionNESW;
 import Interfaces.InitialGenerationCreator;
 import MainClasses.Candidate;
 
@@ -22,16 +21,14 @@ public class  RandomDirection<T extends Enum<?>> implements InitialGenerationCre
 
         for (int i = 0; i < populationSize; i++) {
             int[] candidateDirections = new int[sequenceLength];
-            for (int j = 0; j < sequenceLength; j++) {
-                candidateDirections[j] = this.randomDirection(this.possibleDirections);
+            candidateDirections[0] = this.rand.nextInt(4); // Can start in any direction
+            for (int j = 1; j < sequenceLength; j++) {
+                // Make sure there can never be a backtracking overlap while initializing
+                candidateDirections[j] = ((candidateDirections[j-1] - 1 + this.rand.nextInt(3)) + 4 ) % 4;
             }
             population[i] = new Candidate(candidateDirections);
         }
 
         return population;
     }
-
-    private int randomDirection(Class<T> dirEnum) {
-        return rand.nextInt(dirEnum.getEnumConstants().length);
-    }
 }
diff --git a/src/main/java/Mutators/SinglePoint.java b/src/main/java/Mutators/SinglePoint.java
index 47e24c0..0cdf65f 100644
--- a/src/main/java/Mutators/SinglePoint.java
+++ b/src/main/java/Mutators/SinglePoint.java
@@ -43,7 +43,14 @@ public class SinglePoint<T extends Enum<?>> implements Mutator {
                         if (this.isFRL) {
                             mutatedFolding[mutationPlace] = this.rand.nextInt(3);
                         } else {
-                            mutatedFolding[mutationPlace] = this.rand.nextInt(4);
+                            if (mutationPlace == 0) {
+                                // Allow any direction in the first position
+                                mutatedFolding[mutationPlace] = this.rand.nextInt(4);
+                            } else {
+                                // Make sure there can never be a backtracking overlap while mutating
+                                mutatedFolding[mutationPlace] =
+                                        ((mutatedFolding[mutationPlace-1] - 1 + this.rand.nextInt(3)) + 4 ) % 4;
+                            }
                         }
                     }
                 }
diff --git a/src/main/resources/genetic.properties b/src/main/resources/genetic.properties
index e79358a..6a23d74 100644
--- a/src/main/resources/genetic.properties
+++ b/src/main/resources/genetic.properties
@@ -11,7 +11,7 @@
     # Number of generations to run the algorithm for
         noGenerations = 600
     # Type of population initialization [curl | straight | random]
-        initializationMethod = straight
+        initializationMethod = random
     # Type of selection that should be used [proportional | tournament | onlybest]
         selectionMethod = proportional
     # Number of tournament participants, only relevant when selection is set to tournament
@@ -49,8 +49,8 @@
     # log = Generate tab seperated file with a bit of information about each generation
     # generation = print a short overview about each generation to stdout
         visualizerType = log,generation,image,video
-    # Name of the job, filenames will be based on this name. If left empty a timestamp is used
-        jobName = example1
+    # Name of the job, filenames will be based on this name. If left empty a timestamp is used TODO: leaving it empty should use timestamp
+        jobName =
     # Directory for the image sequence
         imageSequenceDirectory = visualization/series
     # Directory for the resulting video file
-- 
GitLab