Skip to content

Commit

Permalink
Add padding to bitstring output
Browse files Browse the repository at this point in the history
  • Loading branch information
Sub-Xaero committed Oct 27, 2017
1 parent ea79583 commit 639b5f6
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 16 deletions.
7 changes: 4 additions & 3 deletions GeneticAlgorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package ga

import (
"errors"
"fmt"
"math/rand"
"time"
"strconv"
"time"
)

func check(e error) {
Expand Down Expand Up @@ -72,14 +73,14 @@ func (genA *GeneticAlgorithm) Summarise(title string, candidatePool Population)
if len(val.Sequence) <= 10 {
output += val.Sequence.String()
} else {
output += strconv.Itoa(genA.Fitness(val))
output += fmt.Sprintf("%3v", genA.Fitness(val))
}
output += "]"
}
output += "}"
output += " Max : " + strconv.Itoa(genA.MaxFitness(candidatePool))
output += " Average : " + strconv.Itoa(genA.AverageFitness(candidatePool))
output += " Best Candidate : " + genA.MaxFitnessCandidate(candidatePool).String()
output += " Best : " + genA.MaxFitnessCandidate(candidatePool).String()
genA.Output(output)
}

Expand Down
4 changes: 2 additions & 2 deletions bitstrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package ga

import (
"errors"
"fmt"
"math/rand"
"strconv"
"fmt"
)

type Bitstring []string
Expand Down Expand Up @@ -32,7 +32,7 @@ func (genA *GeneticAlgorithm) SetGenerateCandidate(f GenerateCandidateFunction)
func (b Bitstring) String() string {
output := ""
for _, val := range b {
output += fmt.Sprintf("%v", val)
output += fmt.Sprintf("%v", val) + " "
}
return "[" + output + "]"
}
2 changes: 1 addition & 1 deletion bitstrings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestGenerateCandidate(t *testing.T) {
t.Run("CorrectStringOutput", func(t *testing.T) {
t.Parallel()
sequence, _ := setup()
expected := "[123456789]"
expected := "[1 2 3 4 5 6 7 8 9 ]"
if fmt.Sprint(sequence) != expected {
t.Error("String is not correct string.", "Expected:", expected, "Got:", sequence)
} else {
Expand Down
4 changes: 2 additions & 2 deletions crossover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestDefaultCrossover(t *testing.T) {
found := false
foundIndex := 0

expectedString := "{[1001]}"
expectedString := "{[1 0 0 1 ]}"
for i, val := range offspring {
if fmt.Sprint(val) == expectedString {
found = true
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestSetCrossoverFunc(t *testing.T) {
return Population{{Bitstring{"1", "2", "3", "4"}}}, nil
})

expectedString := "[{[1234]}]"
expectedString := "[{[1 2 3 4 ]}]"
crossoverGene, err := genA.Crossover(Genome{}, Genome{}, genA.RandomEngine)

if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions genome_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ func TestGenome_ToString(t *testing.T) {
})

outputString := Genome{Bitstring{"1", "1", "1", "1"}}.String()
expected := "{[1111]}"
expected := "{[1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}

outputString = Genome{Bitstring{"1", "0", "1", "0", "1", "0", "1", "0", "1", "0"}}.String()
expected = "{[1010101010]}"
expected = "{[1 0 1 0 1 0 1 0 1 0 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}

outputString = Genome{Bitstring{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"}}.String()
expected = "{[1111111111]}"
expected = "{[1 1 1 1 1 1 1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}

outputString = Genome{Bitstring{"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"}}.String()
expected = "{[111111111111]}"
expected = "{[1 1 1 1 1 1 1 1 1 1 1 1 ]}"
if outputString != expected {
t.Error("Incorrect string:", outputString, "Expected:", expected)
}
Expand Down
2 changes: 1 addition & 1 deletion mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSetMutateFunc(t *testing.T) {
})

output := fmt.Sprint(genA.Mutate(Genome{}, genA.RandomEngine))
expectedOutput := "{[1234]}"
expectedOutput := "{[1 2 3 4 ]}"
if output != expectedOutput {
t.Error("Mutate function not set. Expected:", expectedOutput, "Got:", output)
} else {
Expand Down
2 changes: 1 addition & 1 deletion output_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ga

import (
"testing"
"fmt"
"testing"
)

func TestDefaultOutputFunc(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRule_String(t *testing.T) {
t.Parallel()
rule1 := Rule{Bitstring{"1", "0"}, "0"}

expected := "[10] 0"
expected := "[1 0 ] 0"
got := rule1.String()
if expected != got {
t.Error("Rule.toString does not match expected. Expected:", expected, "Got:", got)
Expand Down
2 changes: 1 addition & 1 deletion selection_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ga

import (
"testing"
"math/rand"
"testing"
)

func TestSetSelectionFunc(t *testing.T) {
Expand Down

0 comments on commit 639b5f6

Please sign in to comment.