Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts
Thursday, August 30, 2012
anti-gray code fragment.
Given some index,
int antigray;
int vp = index >> 1;
antigray = vp ^ (vp >> 1);
antigray <<= 1;
if ((index & 1) == 1) antigray ^= -1;
Produces a unique antigray code. There's likely going to be a simpler solution somewhere. Rather than shifting right, gray coding, shifting left, and inverting due to the original bit. But, it's the code solution to my Anti-Gray Codes and a pretty easy couple lines. And having found out that I needed something else for the question I was answering, I cut this bit, and didn't really want to lose it.
There are other gray codes than index ^ (index >> 1) but that's a really good derivation. My solution works for any gray codes. So this shouldn't be taken as the only optimally distant code. Also, there might be more distance possible between step-2 codes. It's maximally distant hamming distance is only for the very next and just previous code. But, the code after the next code, may end up being very similar to the current code. Solving for a code giving the maximally distant hamming distance beyond absolutely adjacent codes might not even be derivable through my method. Though, assuming one doesn't weight the code requirements such that step-2 codes being very maximally different could trump some slightly closer step-1 codes. The hamming distance pattern would be N, N-1, N, N-1 ... to which the anti-code would have to be a gray code.
The most distant step-2 code segments would require that with a hamming distance of 1, the same bit not be modified until all other bits are modified.
So the gray code to derive this would be something like:
0000
0001 -- bit 1.
0011 -- bit 2.
0111 -- bit 3
1111 -- bit 4
1110 -- bit 1 - 4 distance.
1100 -- bit 2 - 4 distance.
1000 -- bit 3 - 4 distance.
1001 -- bit 1 - 3 distance.
1011 -- bit 2 - 3 distance.
1010 -- bit 1 - 2 distance (this might be an error).
0010 -- bit 4 - 7 distance.
0110 -- bit 3 - 5 distance.
0100 -- bit 2 - 4 distance.
0101 -- bit 1 - 4 distance.
1101 -- bit 4 - 4 distance.
Which would then derive to several different gray codes which should still have 1 hamming distance between step-2 codes, but hamming distances of 2 for step-4 codes. Though, it would have less hamming distance at step-3 codes, which may defeat the entire purpose and depending on how much more you value step codes from one another. But, maximizing step-4 and step-6 codes may well minimize step-3 and step-5 codes. But, should at the very least it should maintain a hamming distance above 2 for step-2, and step-3 and potentially step-4 codes, and maximally distant codes at step-1.
Labels:
Algorithms,
anti-grey,
brilliant stuff.,
code,
computer science,
fragments
Thursday, October 15, 2009
You want me to believe sorting algorithms work?
Statistics is the wrong math to use concerning evolution. Rather algorithmics is correct. Allow an analogy.
If I used a bubble sort to sort 1000 items in a list, the odds of getting the correct answer is going to be 1000! (1000 factorial). That's 1000*999*998*997...*3*2*1. It's absolutely astronomical! It would like having a tornado hit a beach of sand and just happen to build the Empire State Building and the rest of New York.
402,387,260,077,093,773,543,702,433,923,003,985,719,374,864,210,714,632,543,799,910,429,938,512,398,629,020,592,044,208,486,969,404,800,479,988,610,197,196,058,631,666,872,994,808,558,901,323,829,669,944,590,997,424,504,087,073,759,918,823,627,727,188,732,519,779,505,950,995,276,120,874,975,462,497,043,601,418,278,094,646,496,291,056,393,887,437,886,487,337,119,181,045,825,783,647,849,977,012,476,632,889,835,955,735,432,513,185,323,958,463,075,557,409,114,262,417,474,349,347,553,428,646,576,611,667,797,396,668,820,291,207,379,143,853,719,588,249,808,126,867,838,374,559,731,746,136,085,379,534,524,221,586,593,201,928,090,878,297,308,431,392,844,403,281,231,558,611,036,976,801,357,304,216,168,747,609,675,871,348,312,025,478,589,320,767,169,132,448,426,236,131,412,508,780,208,000,261,683,151,027,341,827,977,704,784,635,868,170,164,365,024,153,691,398,281,264,810,213,092,761,244,896,359,928,705,114,964,975,419,909,342,221,566,832,572,080,821,333,186,116,811,553,615,836,546,984,046,708,975,602,900,950,537,616,475,847,728,421,889,679,646,244,945,160,765,353,408,198,901,385,442,487,984,959,953,319,101,723,355,556,602,139,450,399,736,280,750,137,837,615,307,127,761,926,849,034,352,625,200,015,888,535,147,331,611,702,103,968,175,921,510,907,788,019,393,178,114,194,545,257,223,865,541,461,062,892,187,960,223,838,971,476,088,506,276,862,967,146,674,697,562,911,234,082,439,208,160,153,780,889,893,964,518,263,243,671,616,762,179,168,909,779,911,903,754,031,274,622,289,988,005,195,444,414,282,012,187,361,745,992,642,956,581,746,628,302,955,570,299,024,324,153,181,617,210,465,832,036,786,906,117,260,158,783,520,751,516,284,225,540,265,170,483,304,226,143,974,286,933,061,690,897,968,482,590,125,458,327,168,226,458,066,526,769,958,652,682,272,807,075,781,391,858,178,889,652,208,164,348,344,825,993,266,043,367,660,176,999,612,831,860,788,386,150,279,465,955,131,156,552,036,093,988,180,612,138,558,600,301,435,694,527,224,206,344,631,797,460,594,682,573,103,790,084,024,432,438,465,657,245,014,402,821,885,252,470,935,190,620,929,023,136,493,273,497,565,513,958,720,559,654,228,749,774,011,413,346,962,715,422,845,862,377,387,538,230,483,865,688,976,461,927,383,814,900,140,767,310,446,640,259,899,490,222,221,765,904,339,901,886,018,566,526,485,061,799,702,356,193,897,017,860,040,811,889,729,918,311,021,171,229,845,901,641,921,068,884,387,121,855,646,124,960,798,722,908,519,296,819,372,388,642,614,839,657,382,291,123,125,024,186,649,353,143,970,137,428,531,926,649,875,337,218,940,694,281,434,118,520,158,014,123,344,828,015,051,399,694,290,153,483,077,644,569,099,073,152,433,278,288,269,864,602,789,864,321,139,083,506,217,095,002,597,389,863,554,277,196,742,822,248,757,586,765,752,344,220,207,573,630,569,498,825,087,968,928,162,753,848,863,396,909,959,826,280,956,121,450,994,871,701,244,516,461,260,379,029,309,120,889,086,942,028,510,640,182,154,399,457,156,805,941,872,748,998,094,254,742,173,582,401,063,677,404,595,741,785,160,829,230,135,358,081,840,096,996,372,524,230,560,855,903,700,624,271,243,416,909,004,153,690,105,933,983,835,777,939,410,970,027,753,472,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000, 000
That's roughly 4 * 10^2568 it's more than 100,000,000,000,000,000,000 googols all multiplied together! And you're going to tell me that the sorting algorithm just happens to get the correct answer? Any statistician would laugh at you!
You want me to believe sorting algorithms work? You have more faith than religion. THAT'S INSANE!!!
Saturday, January 19, 2008
New study finds that evolution doesn't select for the least fit. / Lying Robots
To settle some non-lingering issues about evolution a team of scientists proved the evolutionary equivalent of 'water is wet'. Apparently traits which are useful are selected towards... I'm astounded!
Wow! That's the least interesting thing I've read since the evolutionary creation of lying robots! And that's only because it was the first thing I read after reading that fairly interesting article about lying robots. Both of stories appeared on slashdot, ergo the close proximity of reading.
Oddly enough, lying to comrades to jump off a cliff leads you with more food. After honesty develops and becomes universal, dishonestly is a successful strategy, then the entire system is dishonest and needs some form of trust. Even with lying and distrust, there's the ability to signal and that's a step forward even it takes some time to develop a more optimal strategy of success (tit-for-tat is good).
When the researchers measured changes in 40 defined characteristics of the nematodes’ sexual organs (including cell division patterns and the formation of specific cells), they found that most were uniform in direction, with the main mechanism for the development favoring a natural selection of successful traits, the researchers said.
Wow! That's the least interesting thing I've read since the evolutionary creation of lying robots! And that's only because it was the first thing I read after reading that fairly interesting article about lying robots. Both of stories appeared on slashdot, ergo the close proximity of reading.
Three colonies of bots in the 50th generation learned to signal to other robots in the group when then found food or poison. But the fourth colony included lying cheats that signaled food when they found poison and then calmly rolled over to the real food while other robots went to their battery-death. Eerily wicked, to say the least. Saving the robots' honor, luckily, there were also a few "hero robots" that signalled danger and then rolled to their death to save the others.
Oddly enough, lying to comrades to jump off a cliff leads you with more food. After honesty develops and becomes universal, dishonestly is a successful strategy, then the entire system is dishonest and needs some form of trust. Even with lying and distrust, there's the ability to signal and that's a step forward even it takes some time to develop a more optimal strategy of success (tit-for-tat is good).
Labels:
Algorithms,
Evolution,
interesting,
lying,
robots,
slashdot,
water is wet
Saturday, January 5, 2008
Evolution, Creationism, and Theistic Evolution.
If I were to show a snippet of code to represent each of these, they would be as follows.
Evolution:
Evolution isn't about chance, if it doesn't get it right, it tries again. And keeps preserving its previous lucky hits until it bootstraps a solution.
Creationism:
Creationism looks a bit like evolution, but it makes some odd assumptions, and at the first sign of a problem bails out declaring that God exists, declaring the odds of this running successfully are 10^100 (a googol).
Theistic Evolution:
See, evolution works exactly like it should! But sometimes God needs to step in and fix things when they go wrong or are a bit too complex or might run forever. Thank God for stepping in there to help. Though if God were false, it would still work perfectly fine. God is just there to cheapen the entire operation.
So the general religious view of evolution is as follows.
Now, applying Occam's razor. Which is probably the better code? The three simple easy to understand lines, or the absurdly long bit of code that wants to know the truth of evolution before we start and makes sure we get the conclusion we assumed?
Evolution:
int q;
for (int i = 0; i < 100;) { //loop until i >= 100
q = rand() % 10; //q is random #[0-9].
if (q == 0) i++; //If q is zero, increment i.
}
Evolution isn't about chance, if it doesn't get it right, it tries again. And keeps preserving its previous lucky hits until it bootstraps a solution.
Creationism:
bool God = true; // God is assumed true.
bool Evolution = false; // Evolution is assumed false.
int q;
for (int i = 0; i < 100;) { //loop until i >= 100
q = rand() % 10; //q is random #[0-9].
if (q == 0) i++; //If q is zero, increment i.
else { // if q is not zero,
Evolution = false; // Evolution is false.
God = true; // God is true.
exit(1); // exit program.
}
}
Creationism looks a bit like evolution, but it makes some odd assumptions, and at the first sign of a problem bails out declaring that God exists, declaring the odds of this running successfully are 10^100 (a googol).
Theistic Evolution:
bool God = true; // God is assumed true.
int q;
for (int i = 0; i < 100;) { //loop until i >= 100
q = rand() % 10; //q is random #[0-9].
if (q == 0) i++; //If q is zero, increment i.
else { // if q is not zero,
if (God) i++; //if God, increment i anyway.
}
}
See, evolution works exactly like it should! But sometimes God needs to step in and fix things when they go wrong or are a bit too complex or might run forever. Thank God for stepping in there to help. Though if God were false, it would still work perfectly fine. God is just there to cheapen the entire operation.
So the general religious view of evolution is as follows.
bool God = true; // God is assumed true.
bool Evolution;
cin >> Evolution; // asks user to set Evolution.
int q;
for (int i = 0; i < 100;) { //loop until i >= 100
q = rand() % 10; //q is random #[0-9].
if (q == 0) i++; //If q is zero, increment i.
else if (God) { // if q is not zero & God true.
if (Evolution) { //if Evolution is true
i++; //God helps evolution along.
}
else { //if Evolution is not true.
Evolution = false; // Evolution is false.
God = true; // God is true.
exit(1); // exit program.
}
}
}
Now, applying Occam's razor. Which is probably the better code? The three simple easy to understand lines, or the absurdly long bit of code that wants to know the truth of evolution before we start and makes sure we get the conclusion we assumed?
Friday, November 23, 2007
Irreducible complexity and computer code.
One of my hobbies is evolutionary algorithms. I try to run them on a few different things, for example solving for the Pythagorean theorem is my latest quest. I basically hand "organisms" two numbers and ask for the result. The organism which comes closest to approximating what the hypotenuse would be if those numbers were sides of a right triangle wins. The organisms take the code, try something and live or die based on their results. Due to rounding errors it works much better if you use smaller numbers such as below 100.
However, I have made them play poker by handing them all the information about the current hand and asking for an action, and they bust out and die and the better players are copied, mutated and put into the empty seat.
One of my earlier bots was simply a linear program. It was a bit slow so I tried some nice intelligent designing on the programming and coded the following.
It would run for a few hours and the resulting code (after it took over the gene pool) was:
Well, I had forgotten that I told the starters to sort, so the higher card was first followed by the lower card. This had the result of making considering only the lower card a viable strategy (one could make sure both starters were good rather than calling with A4 and other fairly rag hands). I was constantly pissed at the bot, I couldn't understand why it destroyed the information of what one of the two cards was. It literally blinded itself to this very important information. How the hell is that evolutionarily beneficial? Seriously, gouging out one of your eyes as an adaptation?
I was describing the problem to my brother and realized why it did what it did. It was irreducibly complex!
Each line in the original program is required for any functionality. If one doesn't consider the information of the first card, comparing the total to 15 results in a bot which always folds (highest card is 13, ace). Not considering the information of the second card has the same result; it can't make the minimal requirements. Adjusting the comparison number down any, made the player play too loosely and was driven extinct.
The bot used scaffolding to avoid this problem and get to the better program.
From this point it can simply adjust the values without dying off, getting low enough to drop the consideration of starter two without resulting in a lethal mutation. Freeing up the overwrite value to be whatever it wants to be.
The result is a program which gouges out its own eye to escape my poor programming. Leaving the destruction as a vestige (as no modification to the program actually needed consider the information) so it qualifies as junk code. It served no purpose, and hurt the future prospects of the organism, and seemed like the worst bit of code it could evolve. I stopped the program and started the damned thing over several times to nudge that bit of code away and tried to guide the evolutionary process... it failed every time. I was really getting upset with it. Herding cats is one thing, try herding genes. A vestige of reducing an irreducibly complex program, and I blessed them unaware!
Orgel's second law: Evolution is cleverer than you are.
However, I have made them play poker by handing them all the information about the current hand and asking for an action, and they bust out and die and the better players are copied, mutated and put into the empty seat.
One of my earlier bots was simply a linear program. It was a bit slow so I tried some nice intelligent designing on the programming and coded the following.
Take value of starter one.
Add value of starter two.
Compare value to 15, fold if less, call if equal, raise if greater.
It would run for a few hours and the resulting code (after it took over the gene pool) was:
Take value of starter one.
Destroy information of what starter two is.
Compare value to 8, fold if less, call if equal, raise if greater.
Well, I had forgotten that I told the starters to sort, so the higher card was first followed by the lower card. This had the result of making considering only the lower card a viable strategy (one could make sure both starters were good rather than calling with A4 and other fairly rag hands). I was constantly pissed at the bot, I couldn't understand why it destroyed the information of what one of the two cards was. It literally blinded itself to this very important information. How the hell is that evolutionarily beneficial? Seriously, gouging out one of your eyes as an adaptation?
I was describing the problem to my brother and realized why it did what it did. It was irreducibly complex!
Each line in the original program is required for any functionality. If one doesn't consider the information of the first card, comparing the total to 15 results in a bot which always folds (highest card is 13, ace). Not considering the information of the second card has the same result; it can't make the minimal requirements. Adjusting the comparison number down any, made the player play too loosely and was driven extinct.
The bot used scaffolding to avoid this problem and get to the better program.
Take value of starter one.
Overwrite value of starter two with 7.
Add value of starter two.
Compare value to 15, fold if less, call if equal, raise if greater.
From this point it can simply adjust the values without dying off, getting low enough to drop the consideration of starter two without resulting in a lethal mutation. Freeing up the overwrite value to be whatever it wants to be.
The result is a program which gouges out its own eye to escape my poor programming. Leaving the destruction as a vestige (as no modification to the program actually needed consider the information) so it qualifies as junk code. It served no purpose, and hurt the future prospects of the organism, and seemed like the worst bit of code it could evolve. I stopped the program and started the damned thing over several times to nudge that bit of code away and tried to guide the evolutionary process... it failed every time. I was really getting upset with it. Herding cats is one thing, try herding genes. A vestige of reducing an irreducibly complex program, and I blessed them unaware!
Orgel's second law: Evolution is cleverer than you are.
Subscribe to:
Posts (Atom)