Student Support Forum: 'Monte Carlo Optimization' topicStudent Support Forum > General > "Monte Carlo Optimization"

 < Previous Comment | Next Comment > Help | Reply To Comment | Reply To Topic
 Author Comment/Response Bill Simpson 05/02/13 9:08pm In Response To 'Re: Re: Re: Re: Monte Carlo Optimization'---------I believe I understood what your intent was. My using Sow and Reap was an attempt to avoid the time consumed with many AppendTo operations. It would have helped if I had not made several silly mistakes, like choosing to use the name v without looking to see that you had already used v for another purpose and assigned large expressions to it and a couple of other silly errors. Hopefully this fixes those problems. (tbl = Reap[Table[ (* Open Main For Loop *) ka[[i]] = ka[[i - 1]] + weight*ka[[i - 1]]*RandomReal[{-1.0, 1.0}]; (* Other distributions are possible *) kb[[i]] = kb[[i - 1]] + weight*kb[[i - 1]]*RandomReal[{-1.0, 1.0}];(*************************************************) (* This is where the evaluation of the parameter sets begins *)(*************************************************) decayscore[[i]] = chi2[ka[[i]], kb[[i]]];(* This is the function evaluated for each parameter set *) deltaE[[i]] = decayscore[[i]] - decayscore[[i - 1]]; (* The change in energy for each step *) freeE[[i]] = Exp[-beta*deltaE[[i]]]; (* This is a free energy associated with each step *) v [[i]] = Min[1, freeE[[i]]]; u[[i]] = RandomReal[{0, 1}]; (**************************************************) (******** Metropolis Selection Algorithm ********)(**************************************************) If[u[[i]] < v[[i]], (* If u < v always accept that parameter set, else accept with probability u < v *) ka[[i + 1]] = ka[[i]]; kb[[i + 1]] = kb[[i]]; Sow[{True, decayscore[[i]], v[[i]], ka[[i]], kb[[i]], i}](*bestParamSets*), ka[[i]] = ka[[i - 1]]; kb[[i]] = kb[[i - 1]]; Sow[{False, decayscore[[i]], v[[i]], ka[[i]], kb[[i]], i}]; ] (* Close Main For Loop // How long does this loop take to run? *) ,{i, 1, runlength - 1}]][[2, 1]]; bestParamSets = Cases[tbl, {True, data__} -> {data}]; thrownsets = Cases[tbl, {False, data__} -> {data}];)//AbsoluteTiming You can inspect the contents of tbl and compare that to the contents of bestParamSets and thrownsets. Case extracts the items from tbl depending on whether the first element is True or False, and discards that before storing the result in your list. This still isn't a particularly "functional solution" because of all the assignments to array elements, but changing that would take more of a rewrite of the code. I hope I have corrected this now. My apologies for my mistakes and for not seeing those more quickly. URL: ,

 Subject (listing for 'Monte Carlo Optimization') Author Date Posted Monte Carlo Optimization Patrick 04/19/13 10:11am Re: Monte Carlo Optimization Bill Simpson 04/21/13 11:00pm Re: Re: Monte Carlo Optimization Patrick 04/24/13 7:33pm Re: Re: Re: Monte Carlo Optimization Bill Simpson 04/25/13 4:38pm Re: Re: Re: Re: Monte Carlo Optimization Patrick 05/01/13 09:23am Re: Re: Re: Re: Re: Monte Carlo Optimization Bill Simpson 05/02/13 9:08pm Re: Re: Re: Re: Re: Re: Monte Carlo Optimization Patrick 05/03/13 1:12pm
 < Previous Comment | Next Comment > Help | Reply To Comment | Reply To Topic