Implementation of Multiparental Sorting Crossover
$begingroup$
I implemented the Multiparental Sorting Crossover as described here in section 4.2. The (compiled) program can be run as follows:
$ java MPSX input.txt
where input.txt looks like this (the first line is the mask followed by three parents):
3 1 2 1 1 1 1 2 2
8 4 7 3 6 2 5 1 9
9 1 2 3 4 5 6 7 8
4 7 9 3 6 2 5 1 8
As for output, it merely prints the correct result as a string.
import edu.princeton.cs.algs4.In;
import java.io.InputStream;
public class MPSX {
public static void main(String args) {
String filename = args[0];
In in = new In(filename);
String Input = in.readAllLines();
String result = solve(Input);
System.out.println(result);
}
private static void swap(int parent_x, int parent_y, int index) {
for (int i = 0; i < parent_y.length; i++) {
if (parent_y[i] == parent_x[index]) {
int tmp = parent_y[index];
parent_y[index] = parent_y[i];
parent_y[i] = tmp;
}
}
}
private static String solve(String Input) {
String result = "";
String parent1str = Input[1].split(" ");
String parent2str = Input[2].split(" ");
String parent3str = Input[3].split(" ");
int parent1 = new int[parent1str.length];
int parent2 = new int[parent2str.length];
int parent3 = new int[parent3str.length];
for (int i = 0; i < parent1str.length; i++) {
parent1[i] = Integer.parseInt(parent1str[i]);
parent2[i] = Integer.parseInt(parent2str[i]);
parent3[i] = Integer.parseInt(parent3str[i]);
}
String mask = Input[0].replaceAll("\s","");
for (int i = 0; i < mask.length(); i++) {
int mask_element = Character.getNumericValue(mask.charAt(i));;
if (mask_element == 1) {
result += parent1[i];
swap(parent1, parent2,i);
swap(parent1, parent3,i);
}
else if (mask_element == 2) {
result += parent2[i];
swap(parent2, parent1,i);
swap(parent2, parent3,i);
}
else if (mask_element == 3) {
result += parent3[i];
swap(parent3, parent1,i);
swap(parent3, parent2,i);
}
}
return result;
}
}
It is effectively the first program I wrote in Java so I know there is a lot of room for improvement and refactorization.
In particular, I am sure there is a better (more concise) way of processing the input text file. I thought of creating an array of arrays of Strings instead of 3 separate arrays but I wasn't sure how to do it.
java beginner
New contributor
$endgroup$
add a comment |
$begingroup$
I implemented the Multiparental Sorting Crossover as described here in section 4.2. The (compiled) program can be run as follows:
$ java MPSX input.txt
where input.txt looks like this (the first line is the mask followed by three parents):
3 1 2 1 1 1 1 2 2
8 4 7 3 6 2 5 1 9
9 1 2 3 4 5 6 7 8
4 7 9 3 6 2 5 1 8
As for output, it merely prints the correct result as a string.
import edu.princeton.cs.algs4.In;
import java.io.InputStream;
public class MPSX {
public static void main(String args) {
String filename = args[0];
In in = new In(filename);
String Input = in.readAllLines();
String result = solve(Input);
System.out.println(result);
}
private static void swap(int parent_x, int parent_y, int index) {
for (int i = 0; i < parent_y.length; i++) {
if (parent_y[i] == parent_x[index]) {
int tmp = parent_y[index];
parent_y[index] = parent_y[i];
parent_y[i] = tmp;
}
}
}
private static String solve(String Input) {
String result = "";
String parent1str = Input[1].split(" ");
String parent2str = Input[2].split(" ");
String parent3str = Input[3].split(" ");
int parent1 = new int[parent1str.length];
int parent2 = new int[parent2str.length];
int parent3 = new int[parent3str.length];
for (int i = 0; i < parent1str.length; i++) {
parent1[i] = Integer.parseInt(parent1str[i]);
parent2[i] = Integer.parseInt(parent2str[i]);
parent3[i] = Integer.parseInt(parent3str[i]);
}
String mask = Input[0].replaceAll("\s","");
for (int i = 0; i < mask.length(); i++) {
int mask_element = Character.getNumericValue(mask.charAt(i));;
if (mask_element == 1) {
result += parent1[i];
swap(parent1, parent2,i);
swap(parent1, parent3,i);
}
else if (mask_element == 2) {
result += parent2[i];
swap(parent2, parent1,i);
swap(parent2, parent3,i);
}
else if (mask_element == 3) {
result += parent3[i];
swap(parent3, parent1,i);
swap(parent3, parent2,i);
}
}
return result;
}
}
It is effectively the first program I wrote in Java so I know there is a lot of room for improvement and refactorization.
In particular, I am sure there is a better (more concise) way of processing the input text file. I thought of creating an array of arrays of Strings instead of 3 separate arrays but I wasn't sure how to do it.
java beginner
New contributor
$endgroup$
1
$begingroup$
What isedu.princeton.cs.algs4.In
?
$endgroup$
– Carcigenicate
8 hours ago
add a comment |
$begingroup$
I implemented the Multiparental Sorting Crossover as described here in section 4.2. The (compiled) program can be run as follows:
$ java MPSX input.txt
where input.txt looks like this (the first line is the mask followed by three parents):
3 1 2 1 1 1 1 2 2
8 4 7 3 6 2 5 1 9
9 1 2 3 4 5 6 7 8
4 7 9 3 6 2 5 1 8
As for output, it merely prints the correct result as a string.
import edu.princeton.cs.algs4.In;
import java.io.InputStream;
public class MPSX {
public static void main(String args) {
String filename = args[0];
In in = new In(filename);
String Input = in.readAllLines();
String result = solve(Input);
System.out.println(result);
}
private static void swap(int parent_x, int parent_y, int index) {
for (int i = 0; i < parent_y.length; i++) {
if (parent_y[i] == parent_x[index]) {
int tmp = parent_y[index];
parent_y[index] = parent_y[i];
parent_y[i] = tmp;
}
}
}
private static String solve(String Input) {
String result = "";
String parent1str = Input[1].split(" ");
String parent2str = Input[2].split(" ");
String parent3str = Input[3].split(" ");
int parent1 = new int[parent1str.length];
int parent2 = new int[parent2str.length];
int parent3 = new int[parent3str.length];
for (int i = 0; i < parent1str.length; i++) {
parent1[i] = Integer.parseInt(parent1str[i]);
parent2[i] = Integer.parseInt(parent2str[i]);
parent3[i] = Integer.parseInt(parent3str[i]);
}
String mask = Input[0].replaceAll("\s","");
for (int i = 0; i < mask.length(); i++) {
int mask_element = Character.getNumericValue(mask.charAt(i));;
if (mask_element == 1) {
result += parent1[i];
swap(parent1, parent2,i);
swap(parent1, parent3,i);
}
else if (mask_element == 2) {
result += parent2[i];
swap(parent2, parent1,i);
swap(parent2, parent3,i);
}
else if (mask_element == 3) {
result += parent3[i];
swap(parent3, parent1,i);
swap(parent3, parent2,i);
}
}
return result;
}
}
It is effectively the first program I wrote in Java so I know there is a lot of room for improvement and refactorization.
In particular, I am sure there is a better (more concise) way of processing the input text file. I thought of creating an array of arrays of Strings instead of 3 separate arrays but I wasn't sure how to do it.
java beginner
New contributor
$endgroup$
I implemented the Multiparental Sorting Crossover as described here in section 4.2. The (compiled) program can be run as follows:
$ java MPSX input.txt
where input.txt looks like this (the first line is the mask followed by three parents):
3 1 2 1 1 1 1 2 2
8 4 7 3 6 2 5 1 9
9 1 2 3 4 5 6 7 8
4 7 9 3 6 2 5 1 8
As for output, it merely prints the correct result as a string.
import edu.princeton.cs.algs4.In;
import java.io.InputStream;
public class MPSX {
public static void main(String args) {
String filename = args[0];
In in = new In(filename);
String Input = in.readAllLines();
String result = solve(Input);
System.out.println(result);
}
private static void swap(int parent_x, int parent_y, int index) {
for (int i = 0; i < parent_y.length; i++) {
if (parent_y[i] == parent_x[index]) {
int tmp = parent_y[index];
parent_y[index] = parent_y[i];
parent_y[i] = tmp;
}
}
}
private static String solve(String Input) {
String result = "";
String parent1str = Input[1].split(" ");
String parent2str = Input[2].split(" ");
String parent3str = Input[3].split(" ");
int parent1 = new int[parent1str.length];
int parent2 = new int[parent2str.length];
int parent3 = new int[parent3str.length];
for (int i = 0; i < parent1str.length; i++) {
parent1[i] = Integer.parseInt(parent1str[i]);
parent2[i] = Integer.parseInt(parent2str[i]);
parent3[i] = Integer.parseInt(parent3str[i]);
}
String mask = Input[0].replaceAll("\s","");
for (int i = 0; i < mask.length(); i++) {
int mask_element = Character.getNumericValue(mask.charAt(i));;
if (mask_element == 1) {
result += parent1[i];
swap(parent1, parent2,i);
swap(parent1, parent3,i);
}
else if (mask_element == 2) {
result += parent2[i];
swap(parent2, parent1,i);
swap(parent2, parent3,i);
}
else if (mask_element == 3) {
result += parent3[i];
swap(parent3, parent1,i);
swap(parent3, parent2,i);
}
}
return result;
}
}
It is effectively the first program I wrote in Java so I know there is a lot of room for improvement and refactorization.
In particular, I am sure there is a better (more concise) way of processing the input text file. I thought of creating an array of arrays of Strings instead of 3 separate arrays but I wasn't sure how to do it.
java beginner
java beginner
New contributor
New contributor
edited 15 mins ago
Jamal♦
30.3k11119227
30.3k11119227
New contributor
asked 9 hours ago
Jan PislJan Pisl
11
11
New contributor
New contributor
1
$begingroup$
What isedu.princeton.cs.algs4.In
?
$endgroup$
– Carcigenicate
8 hours ago
add a comment |
1
$begingroup$
What isedu.princeton.cs.algs4.In
?
$endgroup$
– Carcigenicate
8 hours ago
1
1
$begingroup$
What is
edu.princeton.cs.algs4.In
?$endgroup$
– Carcigenicate
8 hours ago
$begingroup$
What is
edu.princeton.cs.algs4.In
?$endgroup$
– Carcigenicate
8 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Jan Pisl is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214611%2fimplementation-of-multiparental-sorting-crossover%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Jan Pisl is a new contributor. Be nice, and check out our Code of Conduct.
Jan Pisl is a new contributor. Be nice, and check out our Code of Conduct.
Jan Pisl is a new contributor. Be nice, and check out our Code of Conduct.
Jan Pisl is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214611%2fimplementation-of-multiparental-sorting-crossover%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
What is
edu.princeton.cs.algs4.In
?$endgroup$
– Carcigenicate
8 hours ago