3
$\begingroup$

I'm a beginner to Mathematica. I'm writing a code to design a 4-bar linkage mechanism that traces a path in 2D space. I got the modelling done right, now I'm writing my own implementation of a genetic algorithm to solve the optimization problem.

So in creating the population that will be passed later to the fitness evaluation function, things will be much easier if the randomly generated numbers follow the certain pattern that will make one of the linkages rotate fully.

Currently here is my function to generate the population:

popCreate[maxValue_, cromosomeSize_, popSize_] :=
  (* Create the initial population *)
  Module[{m = maxValue, c = cromosomeSize, s = popSize}, 
    RandomSeed[0];
    (* Create random numbers with table over the size and 
       table over the population size *)
    Table[Table[RandomReal[{0, m}], c], s]]

I've come across this code from another question here:

RandomReal[1, {100, 2}] /. {x_, y_} /; y > x :> {y, x}

Quite honestly I don't know how to adapt this to my needs. Here is my needed pattern:

L + S ≤ P + Q 

where

$\qquad$L is the biggest number (longest link)
$\qquad$S is the smallest number (shortest link)
$\qquad$P and Q are the other two links

My question is:

  • How do I create a function that returns a list of randomly generated numbers that are normally distributed and that follow this specific pattern only?

I was thinking of using If to clean up the unusable arrays that function will generate, yet I'm sure there must be an elegant way of doing this.

$\endgroup$
  • $\begingroup$ Your could not have tested your popCreate function, because if you had, you would know that it doesn't work -- it returns Null rather than a population of chromosomes. Try the much simpler, popCreate[maxValue_, cromosomeSize_, popSize_] := (RandomSeed[0]; Table[Table[RandomReal[{0, maxValue}], cromosomeSize], popSize]), which actually returns an array of random Reals. $\endgroup$ – m_goldberg Jul 27 '16 at 23:51
  • 2
    $\begingroup$ Something along the lines of Cases[Sort /@ RandomReal[{0, 10}, {100, 4}], {s_, p_, q_, l_} /; l + s <= p + q]? Note that these come from a uniform distribution, not a normal one. For that you need RandomVariate and NormalDistribution as @m_goldberg mentioned. $\endgroup$ – MarcoB Jul 28 '16 at 0:01
  • 1
    $\begingroup$ The reason for my criticism of your question is that the bad code and discussions of genetic algorithms and of sampling distributions make your real problem unclear, almost invisible. We like questions to be better focused on the main issue than this one is. $\endgroup$ – m_goldberg Jul 28 '16 at 0:07
  • 3
    $\begingroup$ @MahmoudRagab That's correct: generate n 4-tuples of random numbers, sort each one of them so the length of the "shortest" linkage corresponds to the first number, the longest is the last, then select only those lists whose members obey your constraint. $\endgroup$ – MarcoB Jul 28 '16 at 0:12
  • 1
    $\begingroup$ Mahmoud, you can do rejection sampling and modify @Marco's suggestion: generate those numbers within a While[] loop and keep on going until you get 100 of these tuples, perhaps using Sow[]/Reap[] to store them. $\endgroup$ – J. M. is away Jul 28 '16 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.