flipcoins Flip Coins Problem Formulation flipcoins_1FlipCoinsProblemFormulation Probabilistic Conditions flipcoins_1FlipCoinsProbabilistic Ternary Coins flipcoins_1FlipCoinsTernaryCoins We study dynamic control flow of non-determinism using conditional tasking. Non-deterministic control flow is a fundamental building block in many optimization and simulation algorithms that rely on stochastic convergence rules or probabilistic pruning. Problem Formulation We have a fair binary coin and want to simulate its tosses. We flip the coin for five times. Apparently, the probability for the result to be all heads is 1/32. It is equivalently to say the expected number we need to toss for obtaining five heads is 32. Probabilistic Conditions We use condition tasks to simulate the five coin tosses. We create five condition tasks each returning a random binary number. If the return is zero (head toss), the execution moves to the next condition task; or it (tail toss) goes back to the first condition task to start over the simulation. #include<taskflow/taskflow.hpp> intmain(){ tf::Taskflowtaskflow; tf::Executorexecutor; size_trounds=10000; size_ttosses=0; size_ttotal_tosses=0; doubleaverage_tosses=0.0; tf::TaskA=taskflow.emplace([&](){tosses=0;}) .name("init"); tf::TaskB=taskflow.emplace([&](){++tosses;returnstd::rand()%2;}) .name("flip-coin-1"); tf::TaskC=taskflow.emplace([&](){returnstd::rand()%2;}) .name("flip-coin-2"); tf::TaskD=taskflow.emplace([&](){returnstd::rand()%2;}) .name("flip-coin-3"); tf::TaskE=taskflow.emplace([&](){returnstd::rand()%2;}) .name("flip-coin-4"); tf::TaskF=taskflow.emplace([&](){returnstd::rand()%2;}) .name("flip-coin-5"); //reachthetarget;recordthenumberoftosses tf::TaskG=taskflow.emplace([&](){total_tosses+=tosses;}) .name("stop"); A.precede(B); //fiveprobabilisticconditions B.precede(C,B); C.precede(D,B); D.precede(E,B); E.precede(F,B); F.precede(G,B); //repeattheflip-coinsimulationbyroundstimes executor.run_n(taskflow,rounds).wait(); //calculatetheexpectednumberoftosses average_tosses=total_tosses/(double)rounds; assert(std::fabs(average_tosses-32.0)<1.0); return0; } Running the taskflow by a fair number of times, the average tosses we have is close to 32. The taskflow diagram is depicted below. Although the execution of this taskflow is non-deterministic, its control flow can expand to a tree of tasks based on our scheduling rule for conditional tasking (see Conditional Tasking). Each path from the root to a leaf represents a result of five heads, and none of them can overlap at the same time (no task race). You must follow the same rule when creating a probabilistic framework using conditional tasking. Ternary Coins We can extend the binary coin example to a ternary case. Each condition task has one successor going back to the beginning and two successors moving to the next task. The expected number of tosses to reach five identical results is 3*3*3*3*3 = 243. tf::TaskA=taskflow.emplace([&](){tosses=0;}) .name("init"); //startovertheflipagain tf::TaskB=taskflow.emplace([&](){++tosses;returnstd::rand()%3;}) .name("flip-coin-1"); tf::TaskC=taskflow.emplace([&](){returnstd::rand()%3;}) .name("flip-coin-2"); tf::TaskD=taskflow.emplace([&](){returnstd::rand()%3;}) .name("flip-coin-3"); tf::TaskE=taskflow.emplace([&](){returnstd::rand()%3;}) .name("flip-coin-4"); tf::TaskF=taskflow.emplace([&](){returnstd::rand()%3;}) .name("flip-coin-5"); //reachthetarget;recordthenumberoftosses tf::TaskG=taskflow.emplace([&](){total_tosses+=tosses;}) .name("stop"); A.precede(B); //fiveprobabilisticconditions B.precede(C,B,B); C.precede(D,B,B); D.precede(E,B,B); E.precede(F,B,B); F.precede(G,B,B); //repeattheflip-coinsimulationbyroundstimes executor.run_n(taskflow,rounds).wait(); //calculatetheexpectednumberoftosses average_tosses=total_tosses/(double)rounds; assert(std::fabs(average_tosses-243.0)<1.0); Similarly, we can extend the probabilistic condition to any degree.