Exiting a Nested operation...
- To: mathgroup at smc.vnet.net
- Subject: [mg127901] Exiting a Nested operation...
- From: Earl Mitchell <earl.j.mitchell at gmail.com>
- Date: Fri, 31 Aug 2012 03:57:05 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
Hi all, I'm trying to work out how best to use Nest or NestWhile to perform this iterative task. It's not all that important what's going on inside the functions themselves, only that at each step all the cases that meet some condition are being sown into the Reap call outside the nest, and deleted from testing. The remaining cases are then iteratively processed further until they too meet that condition (let's refer to it as the FinalBranch condition). This is all working well using Nest except that I am having to set the number of iterations and have it spit out a Finsihed! message when finished, rather than ending the nested operation when all cases have met the FinalBranch condition (that is the non-Reaped output == {}). Here is the code that works now - where the second argument to Nest (50) is a hard coded value which I'd like instead to be a test for when to end the iteration: footest = DeleteCases[ Flatten[ Reap[ Nest[ ((*Print[{"Master",#}];*) If[MatchQ[#, {}], Print["Finished!"];, If[ MatchQ[#, _StartingSample], (*Print["1"];*) SingleBranch@(#[[1]]), (*Print["2"];*) With[{branches = Cases[#, _Branch, Infinity]}, With[{newbranches = Parallelize[SingleBranch /@ (branches[[All, 1]])]}, With[{finished = Cases[#, a_Branch /; MatchQ[a[[1, -1, -1]], {"FinalBranch"}]] &@ newbranches}, If[finished != {}, Sow[finished]]; DeleteCases[#, a_Branch /; MatchQ[a[[1, -1, -1]], {"FinalBranch"}]] &@ newbranches ] ] ] ] ]) &, StartingSample[{subtrain[[2 ;; 300]], {}}], 50]]], Null]; // AbsoluteTiming I have tried NestWhile without any luck - my implementation went something like this: footest = DeleteCases[ Flatten[ Reap[ NestWhile[ ((*Print[{"Master",#}];*) If[MatchQ[#, {}], {}, If[ MatchQ[#, _StartingSample], (*Print["1"];*) SingleBranch@(#[[1]]), (*Print["2"];*) With[{branches = Cases[#, _Branch, Infinity]}, With[{newbranches = Parallelize[SingleBranch /@ (branches[[All, 1]])]}, With[{finished = Cases[#, a_Branch /; MatchQ[a[[1, -1, -1]], {"FinalBranch"}]] &@ newbranches}, If[finished != {}, Sow[finished]]; DeleteCases[#, a_Branch /; MatchQ[a[[1, -1, -1]], {"FinalBranch"}]] &@ newbranches ] ] ] ] ]) &, StartingSample[{subtrain[[2 ;; 300]], {}}], # != {} &, 1, 50]]], Null]; // AbsoluteTiming Sorry the code is confusing - this thing is in an expansive phase. Once this problem is solved I will collapse and clean things... any and all help is greatly appreciated! Thanks, Mitch