Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dba0efa

Browse files
committed
changes based on TDE feedback
1 parent 2876f9b commit dba0efa

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

‎CCSPiJ/src/chapter2/MCState.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ public MCState(int missionaries, int cannibals, boolean boat) {
4040

4141
@Override
4242
public String toString() {
43-
return String.format("On the west bank there are %d missionaries and %d cannibals."
44-
+ System.lineSeparator()
45-
+ "On the east bank there are %d missionaries and %d cannibals."
46-
+ System.lineSeparator()
47-
+ "The boat is on the %s bank.",
43+
return String.format(
44+
"On the west bank there are %d missionaries and %d cannibals.%n"
45+
+ "On the east bank there are %d missionaries and %d cannibals.%n"
46+
+ "The boat is on the %s bank.",
4847
wm, wc, em, ec,
4948
boat ? "west" : "east");
5049
}

‎CCSPiJ/src/chapter5/ListCompression.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ public static void main(String[] args) {
102102
System.out.println(originalOrder);
103103
ArrayList<ListCompression> initialPopulation = new ArrayList<>();
104104
final int POPULATION_SIZE = 100;
105+
final int GENERATIONS = 100;
106+
final double THRESHOLD = 1.0;
105107
for (int i = 0; i < POPULATION_SIZE; i++) {
106108
initialPopulation.add(ListCompression.randomInstance());
107109
}
108110
GeneticAlgorithm<ListCompression> ga = new GeneticAlgorithm<>(
109111
initialPopulation,
110112
0.2, 0.7, GeneticAlgorithm.SelectionType.TOURNAMENT);
111-
ListCompression result = ga.run(100, 1.0);
113+
ListCompression result = ga.run(GENERATIONS, THRESHOLD);
112114
System.out.println(result);
113115
}
114116
}

‎CCSPiJ/src/chapter5/SendMoreMoney2.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ public String toString() {
102102
public static void main(String[] args) {
103103
ArrayList<SendMoreMoney2> initialPopulation = new ArrayList<>();
104104
final int POPULATION_SIZE = 1000;
105+
final int GENERATIONS = 1000;
106+
final double THRESHOLD = 1.0;
105107
for (int i = 0; i < POPULATION_SIZE; i++) {
106108
initialPopulation.add(SendMoreMoney2.randomInstance());
107109
}
108110
GeneticAlgorithm<SendMoreMoney2> ga = new GeneticAlgorithm<>(
109111
initialPopulation,
110112
0.2, 0.7, GeneticAlgorithm.SelectionType.ROULETTE);
111-
SendMoreMoney2 result = ga.run(1000, 1.0);
113+
SendMoreMoney2 result = ga.run(GENERATIONS, THRESHOLD);
112114
System.out.println(result);
113115
}
114116

‎CCSPiJ/src/chapter5/SimpleEquation.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
public class SimpleEquation extends Chromosome<SimpleEquation> {
2424
private int x, y;
2525

26+
private static final int MAX_START = 100;
27+
2628
public SimpleEquation(int x, int y) {
2729
this.x = x;
2830
this.y = y;
2931
}
3032

3133
public static SimpleEquation randomInstance() {
3234
Random random = new Random();
33-
return new SimpleEquation(random.nextInt(100), random.nextInt(100));
35+
return new SimpleEquation(random.nextInt(MAX_START), random.nextInt(MAX_START));
3436
}
3537

3638
// 6x - x^2 + 4y - y^2
@@ -78,13 +80,15 @@ public String toString() {
7880
public static void main(String[] args) {
7981
ArrayList<SimpleEquation> initialPopulation = new ArrayList<>();
8082
final int POPULATION_SIZE = 20;
83+
final int GENERATIONS = 100;
84+
final double THRESHOLD = 13.0;
8185
for (int i = 0; i < POPULATION_SIZE; i++) {
8286
initialPopulation.add(SimpleEquation.randomInstance());
8387
}
8488
GeneticAlgorithm<SimpleEquation> ga = new GeneticAlgorithm<>(
8589
initialPopulation,
8690
0.1, 0.7, GeneticAlgorithm.SelectionType.TOURNAMENT);
87-
SimpleEquation result = ga.run(100, 13.0);
91+
SimpleEquation result = ga.run(GENERATIONS, THRESHOLD);
8892
System.out.println(result);
8993
}
9094

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /