I have a large simulation (1500 lines of code in different sections) and I would like to run the file using different combinations of input parameters without manually changing the parameters.
Say my simulation is a function of alpha, beta and gamma and I want to iterate all three variables from 1 to 10. Then with only little code that fits in one cell, the iteration could be easily done using
Do[someFunction[alpha,beta,gamma],{alpha,1,10},{beta,1,10},{gamma,1,10}]
This evaluates the function for all 1000 combinations of input parameters. However, I do not see how I can use Do
for an entire Notebook, is there a way without merging it all into one cell and thereby losing the Notebook's structure?
An approach I have now tried for days is to open the Notebook from another Notebook and writing the parameters created by Do
within that notebook into the main Notebook using NotebookWrite
and then evaluating it using SelectionMove[nb,All,Notebook]
and SelectionEvaluate[nb]
(within that Do
-Loop). However I encountered problems:
a) I think the problem might be that I need the other Notebook to write a list of value assignments such as HoldForm[{alpha=5,beta=5...}
into the main Notebook, and NotebookWrite
does not permit a list of assignments as its argument to be exported; converting it to Strings seems to be difficult to combine with the Do
-Iteration. I tried Öskå's workaround solution but it didn't work for my Problem.
b) A main problem with evaluating it from another workbook seems to be that the Do
-Iteration does not seem to wait until the rest of the Do
-commands are evaluated. Means: It keeps entering new parameters before the evaluation of the main Notebook has finished (but I might be wrong here?).
I have tried many different ways to do it but I didn't find a solution, I'd really appreciate your help. Maybe there's a better and completely different solution or even a solution within the main Notebook.
Edit: More information on the problem
@Jens @Jagra Thank you big time for replying this fast.
My code would surely fit within a Do
-Loop if it wasn't just this much code (with a structure). A Do
-Loop must fit in one single cell; it may reference to values and functions defined outside the ´Do´-Command, but every line of code that is supposed be evaluated during the Loop must also be mentioned within the Do
(at least by some symbol that represents a sequence of commands).
I have approx. 1500 lines of code and if possible, I don't want to name all of them in the Do
even if it would work this way. Merging everything into one cell to put the Do
around the entire NB is not my preferred option because my (many) section titles then get messy if they are marked as input.
My code is basically just transformations of simulated numbers and many different plots of the results:
μ=10;
ϕ=5;
θ=5;
κ=0.1;
γ=0.3;
iterationcount=20000;
lineardepreciation=10;
cashflow1=RandomVariate[NormalDistribution[μ,ϕ],iterationcount];
cashflow2=cashflow1(1+δ)+RandomVariate[NormalDistribution[0,θ],iterationcount];
cashflow3=cashflow2(1+δ)+RandomVariate[NormalDistribution[0,θ],iterationcount];
npv=-100+cashflow1/(1+κ)+cashflow2/(1+κ)^2+cashflow3/(1+κ)^3
earnings1=cashflow1-lineardepreciation;
earnings2=cashflow2-lineardepreciation;
earnings3=cashflow3-lineardepreciation;
PVE1=earnings1/(1+γ);
PVE2=earnings1/(1+γ)+earnings2/(1+γ)^2;
PVE3=earnings1/(1+γ)+earnings2/(1+γ)^2+earnings3/(1+γ)^3;
earningsPVmatrix=Transpose [{npv,PVE1,PVE2,PVE3}];
Hundreds of lines of code similar to this, altering these distributions, all depending on μ,ϕ,0,κ,γ and so on. Based on the results, 40 different plots are created and exported. This would surely fit within the Do
, but then I lose my structure by different levels of Styles (title, section, subsection..which structure the problem). I now want to run the file with different combinations of parameters for my input variables μ,ϕ,0,κ,γ etc.
Should I fit all of this in one cell by referencing all the 1500 commands within the Do
or is there another way?
Do
loop evaluation? That's the sane way to proceed under any normal circumstances. You could also look upImport
. $\endgroup$Do[]
command can be replaced by aTable[]
, which is the preferred method. $\endgroup$Package
$\endgroup$