Can anyone explain this?
In Mathematica 11.1, I have a piece of code that uses RandomReal. Every so often I will throw up a RandomReal::array
error. This happens seemingly at random.
I have a variable called pop
that contains 50 elements, each element is a set of 6 arrays of varying sizes. The following came from iteration 386, there was no problem in all previous iterations. This has happened as late as iteration 1,000 and as early as iteration 50 in previous runs.
"The array dimensions 3 given in position 2 of
RandomReal[{-1,1},3]
should be a list of non-negative machine-sized integers giving the dimensions for the result."
Below is the expression in question, pop[[10]]
in this case;
{1.99835 (1 - Abs[RandomReal[{-1, 1}, 3]]) +
1.60555 Abs[
RandomReal[{-1, 1}, 3]], -1.86404 (1 -
Abs[RandomReal[{-1, 1}, 3]]) -
1.75705 Abs[
RandomReal[{-1, 1}, 3]], -0.358809 (1 -
Abs[RandomReal[{-1, 1}, 3]]) -
0.354962 Abs[RandomReal[{-1, 1}, 3]]}
This came from something of the form;
f[A_,B_]:=Module[{C=Abs[g[]]}, A C + (1-C) B]
Where g[]
generates a random array of the appropriate size. This then causes problems in the rest of my code and stops everything.
I'm not sure exactly what information is needed to solve this problem so I'll happily provide whatever is necessary.
EDIT: Extra definitions added as per a suggestion of Subho95.
Spec = {6, 6, 6, 3};
newMat[{IL_, L1_, L2_, OL_}] :=
{RandomReal[{-1, 1}, {L1, IL}],
RandomReal[{-1, 1}, L1],
RandomReal[{-1, 1}, {L2, L1}], RandomReal[{-1, 1}, L2],
RandomReal[{-1, 1}, {OL, L2}], RandomReal[{-1, 1}, OL]}
mutat[x_, MUTPR_, MUTSTR_] :=
If[RandomReal[] < MUTPR, x + RandomReal[{-MUTSTR, MUTSTR}], x]
breed[mat1_, mat2_, MUTPR_, MUTSTR_,Spec_] :=
Module[{mask = newMat[Spec] // Abs},
Map[mutat[#, MUTPR, MUTSTR] &, mask mat1 + (1 - mask) mat2, {2}]
]
The problem occurs in breed.
EDIT2: Updated as per suggestion of user42582.
With SeedRandom[1]
added at the beginning of everything the problem occurs at iteration 442.
Regarding the function breed, it works as follows;
1st iteration, pop
= a table of 50 newMat
's
nth iteration, pop = getNewPop[pop,fit[pop],...]
getNewPop[pop_, fit_, BKEEP_, MUTPR_, MUTST_, Spec_] :=
Module[{best = Sort[fit, #1[[2]] > #2[[2]] &][[1 ;; BKEEP, 1]],
newpop, tempsamp},
newpop = Table[pop[[best[[i]]]], {i, 1, BKEEP}];
Do[
tempsamp = RandomSample[fit[[;; , 2]] -> pop, 2];
AppendTo[newpop,
breed[tempsamp[[1]], tempsamp[[2]], MUTPR, MUTST, Spec]];
, {i, 1, Length[pop] - BKEEP}];
newpop]
The function fit takes pop and returns a list of 50 pairs {n,fitness of pop[[n]]}
, I haven't included this function here because it is quite complicated, it involves no randomness though.
The function getNewPop
returns a new population, keeping the best BKEEP
of the population and offspring for the rest.
EDIT3: I ran the program for a third time, the error didn't occur at iteration 442 this time, it came at iteration 525. All the other values of pop
and fit
have been the same as the previous run up to iteration 442.
EDIT4: Corrected a typo
EDIT5: I have updated breed as follows
breed[mat1_, mat2_, MUTPR_, MUTSTR_,Spec_] :=
Module[{mask = newMask[Spec]},
Map[mutat[#, MUTPR, MUTSTR] &, mask mat1 + (1 - mask) mat2, {2}]
]
Where newMask is the same as newMat but with a range of {0,1} rather than {-1,1}. Using this everything appears to be working fine. But why?
g
is and howf
is being called in your progarm $\endgroup$RandomReal
; please consider usingBlockRandom
orSeedRandom
; providing the iteration step the error occurs is not helpful unless there is some sensible way to replicate the error; also please consider providing the inputs tobreed
$\endgroup$