First of all, thank you for taking the time to read this. I'm working on a portion of code related to the genetic algorithm that is not producing the expected results. What the code is supposed to do is take a list of lists with binary bits, in this case c = {{1, 0}, {1, 1}}, and change the elements within the sub-lists to either ones or zeros depending on what's given. So, in the case of the list c, when fed into the code, it should yield {{0,1}, {0,0}}. So I created a code involving two nested For loops. After some testing, I've come to realize it's only changing the first element in c ({1, 0}), but not the second ({1, 1}) and returns {{0, 1}, {0, 1}} instead of {{0, 1}, {0, 0}}. I've been trying for some time now to correct this, but so far I've been unsuccessful. I was hoping I could get some help here. The code is as follows:
c = {{1, 0}, {1, 1}};
Mutation1[c_] := Module[{d = c},
MutatedSet = {}; (*This will be used to Append the new lists later*)
For[i = 1, i <= Length[d], i++,
(*This For Loop should take {1,0} AND
{1,1} the apply the rest of the code, but only does
this for the first element, {1,0}*)
position = d[[i]];
length = Length[position];
For[i = 1, i <= length, i++,
location = position[[i]];
If[location == 0,
Mutated = ReplacePart[position, i -> 1];
,
Mutated = ReplacePart[position, i -> 0];
];
position = Mutated;
];
MutatedSet = Append[MutatedSet, position];
];
MutatedSet = Append[MutatedSet, position];
Print[MutatedSet];
];
Thanks again in advance for the help. It is very much appreciated!
Fordoes not localize the variablei, therefore when the innerForloop runs out, the condition for the outer loop also fails and evaluation ceases. – LLlAMnYP Jun 04 '15 at 18:24