I'm trying to do the following: I have a N size chromosome whose type is IntegerGene. I'm interested in that unique chromosome being evaluated following the crossover operator so I use:
Configuration configuration = new DefaultConfiguration ();
Configuracion.getGeneticOperators (). Clear ();
CrossoverOperator crossover = new CrossoverOperator (configuration);
Configuracion.addGeneticOperator (crossover);
...
Genotype poblacion = Genotype.randomInitialGenotype(configuracion);
For example, I want to use a single chromosome to create the following populations, this chromosome would be | 9 | 5 | 10 | 0 | And therefore I initialize it:
Gene [] genes = new Gene [4];
genes [0] = new IntegerGene (configuration, 9,9);
genes [1] = new IntegerGene (configuration, 5,5);
genes [2] = new IntegerGene (configuration, 10,10);
genes [3] = new IntegerGene (configuration, 0,0);
When I run the program the output (the chromosome solution) is always the same (| 9 | 5 | 10 | 0 |), it does not perform the crossover operator ....
Run:
Population Size: 10
...
9 5 10 0
Punctuation = 12.5
9 5 10 0
Punctuation = 12.5
9 5 10 0
Punctuation = 12.5
9 5 10 0
Punctuation = 12.5
Chromosome Solution [0] = 9
Chromosome Solution [1] = 5
Chromosome Solution [2] = 10
Chromosome Solution [3] = 0
Thank you.
Related
What us the difference between int[][] a = new int[2][3] and int a[][] = new int[][]{{11,12},{13,14,15}}?
I decompiled the .class file and found in the first case, the JVM will use multianwearray to create the array, while in the second case, it will use anewarray.
I think in the first case, JVM will create a continuous space. Am I right?
first case (int[][] a = new int[2][3])
second case (int[][] a = new int[][]{{11,12},{13,14,15}})
I think in the first case, jvm will create a continuous space. Am i right?
No. A multidimensional array is still an array of arrays. And it must be so, because the code that later accesses the array doesn't know how it was created.
Writing
int[][] a = new int[2][3];
allocates the same amount of heap space and the data on the heap has the same structure as if you would write
int[][] a = new int[2][];
a[0] = new int[3];
a[1] = new int[3];
except that the code for the first variant is embedded in the JVM:
https://github.com/openjdk/jdk17u/blob/master/src/hotspot/share/oops/objArrayKlass.cpp#L178 ff
objArrayOop array = allocate(length, CHECK_NULL);
//...
if (length != 0) {
for (int index = 0; index < length; index++) {
ArrayKlass* ak = ArrayKlass::cast(ld_klass);
oop sub_array = ak->multi_allocate(rank-1, &sizes[1], CHECK_NULL);
h_array->obj_at_put(index, sub_array);
}
}
First Case
Creates an array of 2 int arrays, which are of length 3, which are filled with 0s.
| [ ][0] | [ ][1] | [ ][2]
[0][ ] | 0 | 0 | 0
[1][ ] | 0 | 0 | 0
Second Case
Creates a "jagged" array containing { 11, 13 }, and { 13, 14, 15 }. And which contained values are themselves array of ints.
| [ ][0] | [ ][1] | [ ][2]
[0][ ] | 11 | 12 | error
[1][ ] | 13 | 14 | 15
I try to learn BitSet collection in java. I have read that it uses bits inside.
Each * component of the bit set has a {#code boolean} value
I wrote a small application:
BitSet bitSet = new BitSet();
bitSet.set(9);
bitSet.set(5);
bitSet.set(3);
System.out.println(bitSet);
System.out.println(Arrays.toString(bitSet.toByteArray()));
I was wondered that I can put value different from 1 and 0.
Also I don't understand the output:
{3, 5, 9}
[40, 2]
Please explain me the usage of this collection?
You set the bits number 3, 5 and 9:
byte# 1 0
index … 9 8 7 6 5 4 3 2 1 0
value … 1 0 0 0 1 0 1 0 0 0
Binary 10 is decimal 2 (2¹ = 2).
Binary 00101000 is decimal 40 (2³ + 2⁵ = 8 + 32 = 40).
BitSet logically represents a "vector of bits that grows as needed" (javadoc).
When you create it via new BitSet(), you have all bits set to 0 (false).
0 5 10
| | |
000000000000... (virtually infinite sequence)
Using set(x) you set to 1 (true) the bit at position x (where the first position is 0); e.g. in your code you enable bits 3, 5 and 9.
0 5 10
| | |
000101000100...
toString() reports the list of bits set to 1, i.e. 3, 5 and 9 in the example.
toByteArray() converts the contents of the BitSet to a sequence of byte values, each containing the value of 8 contiguous bits, in little-endian order (i.e. starting from least indices in the BitSet). The output {40, 2} in your example comes from:
7 0 15 8 <- position in BitSet
| | | |
{00101000 , 00000010} <- toByteArray(), binary
| |
{ 40 , 2 } <- toByteArray(), decimal
Hope this helps.
BitSet.set(int bitIndex) sets the bit at the specified index to true.
So bitSet.set(9); flips bit number 9 to 1
On output:
System.out.println(bitSet); prints the result of toString which is according to JavaDoc:
for every index for which this BitSet contains a bit in the set state, the decimal representation of that index is included in the result. S
Arrays.toString(bitSet.toByteArray()) prints byte representation of BitSet.
Step by step, it splits your binary set: 1000101000
to bytes: 10 00101000
which is 2 and 40 in decimal.
1 - 1
2 - 2,3
3 - 4,5,6
4 - 7,8,9,10
Given any number from 4 to 6 ,I need the output as 3.
Given any number from 7 to 10 ,I need the output as 4.
I need the fastest solution for the above problem to solve an algorithm.
What I could think of is a brute force algorithm :
Given 7:
n-square + n = 7*2 = 14
1 + 1 = 2 < 14
4 + 2 = 6 < 14
9 + 3 = 12 < 14
16+ 4 = 20 >=14 --> So 4
Is there any better approach to arrive at the solution ? OR My approach to the algorithm itself is flawed ?
Brief explanation of the algo :
A,B,C
After every iteration every element becomes increased by one.
A,A,B,B,C,C
Given 3, C will be returned.
Given 4 or 5, A will be returned.
Given 6 or 7, B will be returned.
Given 8 or 9, C will be returned.
Given 10 or 11 or 12, A will be returned.
Given 13 or 14 or 15, B will be returned.
How the solution to the mathematical problem will help solve the algo :
Total number of elements = 3
Given number = 13 (Output to be B)
Divide and Ceiling = Ceil (13/3) = 5 [So 13 falls under when every element has become * 3] (From Mathematical problem : If given number is 5, 3 is to be used)
Starting index of when every element has become * 3 [IS_EQUAL_TO = ] 3 * 3(summation of previous iteration => 1 + 2) + 1 = 10
To Find the index = Ceil(13-10+1/3 (this 3,comes from the mathematical problem) ) = Ceil (4/3) = 2nd index = B
Given number of rows N, the size of the triangle is N(N+1)/2. You are essentially trying to find the least integer N such that N(N+1)/2 >= M, with M given. If you have a function to compute square roots, you can solve this equation in constant time.
N(N+1)/2 >= M, multiply both sides with 2,
N²+N >= 2M, complete the square, take the square root, blablabla
N >= sqrt(2M+1/4)-1/2
Therefore the answer is N = ceil(sqrt(2*M + .25) - .5)
I'm working on an textbook assignment and am having trouble understanding arrays. I'm looking at a practice question and am confused.
Declare an array of integers containing the first five prime numbers.
This would be int[]primes = {2,3,5,7,11}
Assume the array 'primes' has been initialized. What does it contain after executing the following loop?
for(int i = 0; i < 2; i++)
{
primes[4 - i] = primes[i];
}
The textbook gives the answer {2,3,5,3,2} for this...can anyone explain how this loop works?
I assume you understand practice 1. For practice 2, the loop
for (int i = 0; i < 2; i++)
iterates twice: once at i=0, and once at i=1. At i=2, the condition i<2 is broken, and the loop does not execute.
The actual line of code inside the loop
primes[4-i] = primes[i];
sets the 4-i'th element in the array to be equal to the i'th element in the array.
Initialized, the array primes is {2,3,5,7,11}
After one loop, the array primes is {2,3,5,7,2} (primes[4] = primes[0] has been executed).
After both loops, the array primes is {2,3,5,3,2} (primes[3] = primes[1] has been executed).
Remember that arrays are indexed by zero. Hope this helped.
the for loop exceutes two times, for i=0 and i=1
primes[4-0] = primes[0] = 2 -> primes[4] = 2
primes[4-1] = primes[1] = 3 -> primes[3] = 3
so the first 3 fields in the array are not changed, just the 4th and the 5th
It seems like you don't understand for loops that well at all, since this is a pretty simple example. For loops are basically shortcuts in code that iterate through data structures. You can type out for loops line by line, but they would be way longer. In this example,
for (int i = 0; i < 2; i++)
{
primes[4 - i] = primes[i];
}
becomes:
primes[4 - 0] = primes[0];
primes[4 - 1] = primes[1];
So all this loop is doing is setting the last element of the array to the first, and the second-to-last element to the second.
Starting with
int[]primes = {2,3,5,7,11}
the for-loop works like this
i=0 -> primes[4-0] = primes[0]; //array {2,3,5,7,2}
i=1 -> primes[4-1] = primes[1]; //array {2,3,5,3,2}
Imagine you have a zoo(memory) with a row of cages(array) next to each other. each cage is the exact size needed to hold an animal(data type like int)
Zoo---------------------
[animal][animal][animal][animal][animal]
------------------------
You decide to label each cage with a number(index)
Zoo---------------------
[animal][animal][animal][animal][animal]
0 1 2 3 4
------------------------
You get some initial funding to purchase animals for your cages (initialize the array). So you buy a Zebra, Panda, Owl, Tiger, and a Bear and put them in the cages. The order you purchased the animals is the order you place them in their cages.
Zoo---------------------
[Zebra][Panda][Owl][Tiger][Bear]
0 1 2 3 4
------------------------
Your zoo just perfected cloning so you are able to make copies of the animals. You Decide people are really enjoying Pandas & Zebras but don't care for Tigers and Bears. You delegate your tasks(algorithm) by having a sequential stack of index cards with instructions(loop). Each index card has a page number going from 0 up to less than 2, so your last index card has the page number 1
_________________________ ___________________________
|0 | |1 |
| | | |
| | | |
| | | |
| | | |
|_________________________| |___________________________|
You love the number 4 and believe that it is way better then any number and should be the starting point of counting instead of the number 0. So you give all instructions using the number 4 as a reference point.
_____________________________ _____________________________
|0 | |1 |
|Yo zoo keeper, | |Yo zoo keeper, |
|Copy the animal | |Copy the animal |
|in the cage that has this | |in the cage that has this |
|card number and put it in | |card number and put it in |
|cage 4 minus this card number| |cage 4 minus this card number|
|-The Boss | |-The Boss |
|_____________________________| |_____________________________|
Your zoo keeper reads the first index card(with the number 0). He makes a copy of the Zebra and puts in the cage with the Bear. The Zebra immediately eats the bear and is the only inhabitant of the cage.
Zoo---------------------
[Zebra][Panda][Owl][Tiger][Zebra]
0 1 2 3 4
------------------------
Your zoo keeper reads the second index card(with the number 1). He makes a copy of the Panda and puts in the cage with the Tiger. The Panda immediately eats the Tiger and is the only inhabitant of the cage.
Zoo---------------------
[Zebra][Panda][Owl][Panda][Zebra]
0 1 2 3 4
------------------------
And that is how arrays work for primitives. For Objects instead of cages in your zoo you just have a map that points to where you can find the animals in the zoo. Any animal that is not on the list gets marked with a tag and swept to the wild, freeing up space in your zoo.
int firstPosition(int x, int [] a) {
int lower = 0;
int upper = a.length;
while (lower != upper) {
int mid = (lower + upper) / 2;
**if (x <= a[mid]) {** // the line I don't understand
upper = mid;
} else {
lower = mid + 1;
}
return (lower);
}
If a = {4, 4, 5, 5, 6, 7, 8, 9, 9, 9} what will the algorithm return for the following choices of x?
i) x = 3
ii) x = 4
iii) x = 5
iv) x = 9
v) x = 11
I have tried stepping through this program, for example x = 3, a.length returns 10, so upper is always equal to 10.
while ( 3 ! = 0 ) { // execute line
int mid = lower + upper / 2 - which is (0 + 10)/2 = 5
if ( x <= a[mid]) // I assume that means if 3 is less than or equal to 5? 5 then replace mid with 5 and then...
lower = mid + 1 // 5+1 = 6, return 6 as lower?
This is a basic binary search algorithm, implemented iteratively instead of recursively.
The line you don't understand checks to see if x (the search value) might lie in the lower half or upper half of the array. This works because the array is sorted. We can divide any sorted array into two halves, and look at the value in the middle to determine which half the value we're looking for might be in.
Say that the array looks like this:
+---+---+---+---+---+----+
| 1 | 3 | 5 | 7 | 9 | 11 |
+---+---+---+---+---+----+
^ ^
| |
lower upper
and we're trying to figure out which slot the number 9 is in. Since the array is sorted, we can immediately discard half of the array.
How?
Look at the value in the "center" of the array: it's 5, and 9 is larger than 5, so we know that 9 must be in the upper half of the array. Algorithmically speaking, this would be the else case of the if statement in your code.
So we repeat the same process, but only looking at the upper half of the array this time:
+---+---+---+---+---+----+
| 1 | 3 | 5 | 7 | 9 | 11 |
+---+---+---+---+---+----+
^ ^
| |
lower upper
Looks to me like a binary search algorithm. The choice of x makes sure that the array part that needs to be searched is halved each iteration. Read more about it here
This is a modification of a binary search.
Since the data is ordered, to determine if you can throw away the "upper" or "lower" half of a range of data, look at the middle element. When it is bigger than the number you're looking for, you can safely throw away the numbers past it (by reducing the range). When it is smaller than the number you are looking for, you can safely throw away the numbers before it (by reducing the range).
The key here is that if it is the number you are looking for, you need to crawl back towards the beginning of the range until you detect that the "next" number is not actually the "first" of that possibly repeated value.