Can someone explain me how Order Crossover works? I will give this example and I want to understand it in a generic way to implement after.
Parent 1 = 1 2 3 | 4 5 6 7 | 8 9
Parent 2 = 4 5 2 | 1 8 7 6 | 9 3
and the solution are two childreen:
Children 1 = 2 1 8 | 4 5 6 7 | 9 3
Children 2 = 3 4 5 | 1 8 7 6 | 9 2
I understand some parts but others not.
Thanks
Basically, a swath of consecutive alleles from parent 1 drops down, and remaining values are placed in the child in the order which they appear in parent 2.
Step 1: Select a random swath of consecutive alleles from parent 1. (underlined)
Step 2: Drop the swath down to Child 1 and mark out these alleles in Parent 2.
Step 3: Starting on the right side of the swath, grab alleles from parent 2 and insert them in Child 1 at the right edge of the swath. Since 8 is in that position in Parent 2, it is inserted into Child 1 first at the right edge of the swath. Notice that alleles 1, 2 and 3 are skipped because they are marked out and 4 is inserted into the 2nd spot in Child 1.
Step 4: If you desire a second child from the two parents, flip Parent 1 and Parent 2 and go back to Step 1.
One such solution for Ordered Crossover is detailed in this post.
This answer provides some sample java code with documentation detailing the processes used for the Ordered Crossover.
Additionally, this paper from Moscato provides a breakdown of the OX Process.
Hope this helps!
Related
This is a largely conceptual question so i dont have any code to show. I'll try to explain this as best i can. I am writing a program that is supposed to find common sequences of numbers found in a large table of random combinations.
So for example take this data:
1 5 3 9 6 3 8 8 3 3
6 7 5 5 5 4 9 2 0 1
6 4 4 3 7 8 3 9 5 6
2 4 2 4 5 5 3 4 7 7
1 5 6 3 4 9 9 3 3 2
0 2 7 9 4 5 3 9 8 3
These are random combinatinos of the numbers 1-9. For every 3 digit (or more) sequence found more than once i need to put that into another database. So the first row contains "5 3 9" and the 6th row also contains "5 3 9". I would put that sequence in a separate table with the number of times it was found.
I'm still working out the algorithm for actually making these comparisons but i figure i'll have to start with "1 5 3", compare that to every single 3 number trio found, then move on to "5 3 9" then "3 9 6" etc....
MY MAIN PROBLEM RIGHT NOW is that i dont know how to do this if these numbers are stored in a database. My database table has 11 columns. One column for each individual number, and one column for the 10 digit sequence as a whole. Columns are called Sequence, 1stNum, 2ndNum, 3rdNum...10thNum.
Visual: first row in my database for the data above would be this :
| 1 5 3 9 6 3 8 8 3 3 | 1 | 5 | 3 | 9 | 6 | 3 | 8 | 8 | 3 | 3 |
("|" divide columns)
How do i make comparisons efficiently with Java? I'm iterating over every row in the table many times. Once for the initial sequence to be compared, and for every one of those sequences i go through each row. Basically a for loop in a for loop. This sounds like its going to take a ton of queries and could take forever if the table gets to be massive (which it will).
Is it more computationally efficient if i iterate through a database using queries or if i dump the database and iterate through a file?
I tried to explain this as best as i could, its a very confusing process for me. I can clarify anything you need me to. I just need guidance on what the best course of action for this would be.
Here is what I would do, assuming you have retrieved the sequences in a list :
List<String> sequences = Arrays.asList("1539638833","6755549201","6443783956","2424553477","1563499332","0279453983");
Map<String,Integer> count = new HashMap<>();
for (String seq : sequences) {
int length = seq.length();
for (int i=0 ; i<length - 2 ; i++) {
String sub = seq.substring(i,i + 3);
count.put(sub,count.containsKey(sub) ? count.get(sub) + 1 : 1);
}
}
System.out.println(count);
Ouput :
{920=1, 783=1, 945=1, 332=1, 963=1, 644=1, 156=1, 983=1, 453=1, 153=1, 388=1, 534=1,
455=1, 245=1, 539=2, 554=1, 242=1, 555=1, 553=1, 437=1, 883=1, 349=1, 755=1, 675=1,
638=1, 395=1, 201=1, 956=1, 933=1, 499=1, 634=1, 839=1, 794=1, 027=1, 477=1, 833=1,
347=1, 492=1, 378=1, 279=1, 993=1, 443=1, 396=1, 398=1, 549=1, 563=1, 424=1}
You can then store these values in the database from the Map.
You can do it in sql with a union clause:
select sum(c), sequence
from
(
select
count(*) as c, concat(col1 ,col2 , col3) as sequence
from t
group by col1, col2, col3
union
select
count(*) as c, concat(col2 ,col3 , col4) as sequence
from t
group by col2, col3, col4
union (... and so on enumerating through the column combinations)
) as tt
group by sequence
I would imagine a pure java implementation would be quicker and have less
memory overhead. But if you already have it in the database it may be quick
enough.
I'm working in Java. Given a matrix NxM, I need to find all possible paths through the array. It is only allowed to go diagonally up or down, or go to the right. An example 4x4 matrix:
3 5 7 9
2 4 6 8
9 3 7 5
6 8 2 4
The numbers in the matrix can be any arbitrary value. I would like to generate all possible routes through the matrix, starting in one of the four numbers in the first column. It is only allowed to move Northeast, East, and Southeast. An example route:
3-5 7 9
\
2 4 6-8
9 3 7 5
6 8 2 4
So all routes consist of M numbers. I'm using a 2D array to hold this NxM matrix. Some more possible paths are:
3 4 6 5
3 5 6 9
9 4 7 4
9 3 6 9
Is there a way to easily generate all paths in Java?
Any help is much appreciated!
Probably you can use three methods for doing this and call them recursively.
goNortEast(); Search for an element to the index [N-1][M+1] if N>0, else there will be no element NOrtEast of the current element, if element exists this will also lead to a new path.
goEast(); Each element can go East by index [N][M+1];
goSouthEast(); Search for element at index [N+1][M+1] if the element exists, this will also lead to a new path,
Call these three methods on each element starting from [0][0], then [1][0] and so on.
for [0][0], it will call goNorthEast[]- will not satisfy condition goEast();-index[0][1] and goSouthEast(); index[1][1] now call the same three methods of these elements.
At the end you will get all possible paths from the element you started.
I'm working on a sudoku solver with recursive backtracking which is pretty much finished except for one thing. If I would put duplicates somewhere within the puzzle (For example 1,1 in the top corner) it can go on forever trying to find a solution even though it's not a solvable puzzle.
Any help is greatly appreciated!
Rob
Well the way you know to backtrack is when your puzzle hits a contradictions, so at every step you should run a "validate" method, and if the puzzle is illegal then the last move that you made was illegal.
When you find that your move is illegal you can recursively backtrack and keep going.
Also, note that this is the rather naive approach, maybe some sudoku experts have a better algorithm, but this brute force should do the trick.
You want to detect an invalid situation, so you should check for it even before calling your solver. Your solver on itself will not create invalid solutions...
Regarding duplicates, i would suggest to keep a list of possible numbers for each cell, and when you are trying to solve a cell, you would compare this list against matching row, column and box, that way you will prevent creating duplicates. With this you can solve easier puzzles without backtracking. And if you get stuck, then use backtracking to continue...
This isn't necessarily the answer but it should help you. I have done this sort of thing before for a macro program and it was the highest rated one available.
A Sudoku solver can be quite a challenge. The only way to tell if a move is right is if it is absolute or if it is proved later on. This can lead to be quite a challenge as the end is based off of the current situation and moves. This means that you can handle this as a permutation. You can go through each square and figure out what possible numbers it has. From there, you could get one or two defined squares. Based off of this, there are many possible ways to get to an end point.
An 'end point' would be defined when the puzzle is solved (no errors - every square filled) or there is a fault.
Based off of this, you can treat each move as a node then build a tree system surrounding the possible moves.
For example:
8 7 1 2 _ _ 6 9 3
2 9 6 3 8 7 1 _ _
This is just a small example, but based off of it, respectively sweeping through each row, then each column we can generate possible numbers:
(5, 1) -> [4, 5]
(6, 1) -> [4, 5]
(8, 2) -> [4, 5]
(9, 2) -> [4, 5]
Based off of this, and the solutions given to us, we can see that there is exactly 4 possible solutions:
8 7 1 2 4 5 6 9 3
2 9 6 3 8 7 1 4 5
-or-
8 7 1 2 5 4 6 9 3
2 9 6 3 8 7 1 4 5
-or-
8 7 1 2 4 5 6 9 3
2 9 6 3 8 7 1 5 4
-or-
8 7 1 2 5 4 6 9 3
2 9 6 3 8 7 1 5 4
Though that is not enough information to solve the whole puzzle and figure out which is 'correct', this can be standardized and used to create a similar system and soon find a solution.
So you could add all 4 of these possibilities to a tree, each branching from the original:
8 7 1 2 _ _ 6 9 3
2 9 6 3 8 7 1 _ _
and then deal with them recursively.
Hope this helps!
To implement the Validate class, couldn't you just write Validate.validate(); inside of your solve method? Hope it helps.
use I can't divide into segads. As for my above example if 5 threads are set, then first segment would take 2 first object, and second 3th and 4th, so they dont find dups, but there are dups if we merge them, its 2th and 3th.
There could be more complex strate take from first threads .. ah nevermind, to hard to explain.
And ofcourse, problelection itself in my plans.
Tha
EDIT:
InChunk, and then continue analyzing that chunk till the end. ;/
I think the process of dividing up the items to be de-duped is going to have to look at the end of the section and move forward to encompass dups past it. For example, if you had:
1 1 2 . 2 4 4 . 5 5 6
And you dividing up into blocks of 3, then the dividing process would take 1 1 2 but see that there was another 2 so it would generate 1 1 2 2 as the first block. It would move forward 3 again and generate 4 4 5 but see that there were dups forward and generate 4 4 5 5. The 3rd thread would just have 6. It would become:
1 1 2 2 . 4 4 5 5 . 6
The size of the blocks are going to be inconsistent but as the number of items in the entire list gets large, these small changes are going to be insignificant. The last thread may have very little to do or be short changed altogether but again, as the number of elements gets large, this should not impact the performance of the algorithm.
I think this method would be better than somehow having one thread handle the overlapping blocks. With that method, if you had a lot of dups, you could see it having to handle a lot more than 2 contiguous blocks if you were unlucky in the positing of the dups. For example:
1 1 2 . 2 4 5 . 5 5 6
One thread would have to handle that entire list because of the 2s and the 5s.
I would use a chunk-based division, a task queue (e.g. ExecutorService) and private hash tables to collect duplicates.
Each thread in the pool will take chunks on demand from the queue and add 1 to the value corresponding to the key of the item in the private hash table. At the end they will merge with the global hash table.
At the end just parse the hash table and see which keys have a value greater than 1.
For example with a chunk size of 3 and the items:
1 2 2 2 3 4 5 5 6 6
Assume to have 2 threads in the pool. Thread 1 will take 1 2 2 and thread 2 will take 2 3 4. The private hash tables will look like:
1 1
2 2
3 0
4 0
5 0
6 0
and
1 0
2 1
3 1
4 1
5 0
6 0
Next, thread 1 will process 5 5 6 and thread 2 will process 6:
1 1
2 2
3 0
4 0
5 2
6 1
and
1 0
2 1
3 1
4 1
5 0
6 1
At the end, the duplicates are 2, 5 and 6:
1 1
2 3
3 1
4 1
5 2
6 2
This may take up some amount of space due to the private tables of each thread, but will allow the threads to operate in parallel until the merge phase at the end.
I am supposed to start for example from point 1B to 5D. how am i supposed to reach? anyone can gv me a hint to get start on this?thanks. not asking for codes but the tips/ hints. thanks.
A B C D E
1 5 1 4 4 1
2 3 4 3 3 4
3 4 3 1 1 3
4 4 3 4 2 5
5 3 4 1 1 3
Start by defining data structures , and apply algorithm with it
You may refer to pseudo-code on wiki: Wiki A Star Search Algorithm