4
$\begingroup$

I often need to explore a large parameter space, e.g. making dozens of plots using a range of parameters. This looks something like:

explore[a, b, c, d]

a = {1, 3, 6, 8, 9}
b = {"a", "b", "c"}
c = {2, 6}
d = {"X", "I", "k", "l"}

I then usually use n nested loops (4 in this case) to explore all combinations of parameters, and in each loop export a plot. I would suppose there is a more efficient, or at least a simpler way to code this. I tried Map, but would need more than one slot, and also Table, but could not fully reproduce exploring the entire parameter set.

$\endgroup$
  • 2
    $\begingroup$ Do you mean something like explore @@@ Tuples[{a, b, c, d}]? $\endgroup$ – aardvark2012 Jun 23 '17 at 11:51
  • $\begingroup$ That is not working. explore is a self written command, something like explore[a_, b_, c_, d_] := Module[{},], where a, b, c, d are parameters that are used in a longer and complex subprogram stored in module and executed using the explore command. $\endgroup$ – Mockup Dungeon Jun 23 '17 at 12:01
  • $\begingroup$ That should give you a list of all the outputs obtained by feeding all possible combinations of those parameters into your explore function, whatever the specifics of that function might be. Do the parameters come from a discrete set, as in your example? $\endgroup$ – aardvark2012 Jun 23 '17 at 12:20
  • $\begingroup$ That is beautiful! Working now. I just had to make some slight adjustments to values appearing only once, i.e. putting them into brackets. Thanks! $\endgroup$ – Mockup Dungeon Jun 23 '17 at 13:40
5
$\begingroup$

Tuples and Subsets and their associates are really handy when you're working with discrete sets. In your case:

a = {1, 3, 6, 8, 9};
b = {"a", "b", "c"};
c = {2, 6};
d = {"X", "I", "k", "l"};
explore @@@ Tuples[{a, b, c, d}]

(* Out= {explore[1, "a", 2, "X"], explore[1, "a", 2, "I"],... , explore[9, "c", 6, "l"]} *)

Depending on your explore function (i.e., if it takes a while and isn't already parallelized) and your actual parameters, you might also be able to speed things up with

Parallelize[explore @@@ Tuples[{a, b, c, d}], Method -> "FinestGrained"]

But probably not worth it if explore is fast already due to the overheads.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thanks – it is worth to parallelise, and I wouldn't have done it without your hint. $\endgroup$ – Mockup Dungeon Jun 23 '17 at 18:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.