**General Tianji' Horse Race Strategy from China and eTCL demo example calculator, numerical analysis** This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER in your comment with the same courtesy that I will give you. Its very hard to reply intelligibly without some background of the correspondent. Thanks,[gold] ---- <> **Introduction** [gold] Here is some eTCL starter code for General Tianji' Horse Race Strategy from China in calculator shell. Additional console programs below are used to check or improve subroutines in calculator shell. ---- Both the King and General Tianji have sets of 3 horses. Each set has low grade horse, middle grade horse and best grade horse. General Sun Bin advised that the General Tianji run the General's low grade horse against the king's best grade horse, the General's middle grade horse against the king's low grade horse, and the General's best grade horse against the king's middle grade horse. The result was that all the General Tianji 's horses won that day circa 340 BCE. Each round of the horse race won 200 silver dollars. General Tianji and Sun Bin's share of the winnings was a bag of silver, worth expr(3*$200) or 600 silver dollars. When this bit of strategy was explained later to the king, the king promoted General Tianji to chief of all armies. Not sure an elegant proof can be drummed up, but we have the many random horses in eTCL. This racehorse problem can be loaded into the eTCL calculator shell and each horse of three can be simulated with a random race score. The eTCL calculator from the Random Walk Equation offers some analogies. ---- The tale of the General Tianji and General Sun Bin ( died 316 BCE) was found in the Chinese history book, Shuking. The Shuking history book was literally saved from the flames of the book burning first empire. I have a rare English translation of the Shichih (sic) commentary in my library. Here the particular issue is how the grades of horses are defined in ancient China. The analysis assumes the best grade horse wins 50 percent of his races, the middle grade wins 35 percent, and the low grade wins 15 percent. The Tainji horserace has been featured in ACM student contests with an average of 6000 Chinese entries ( computer programs ). Most of the entries featured bipartite matching algorithms. ---- **Pseudocode Section** ====== # using pseudocode proc test1 { # possible problem instances, low vs high middle vs low high vs middle} proc test2 { initialize algorithm_result = 1. initialize terms of tianji combined probability reported instances of harmonic mean, DFP in winner type problems B. combined work norm equals 1/(1/norm1 +1/norm2 +1/norm3) B. alternate combined work norm equals 3/(1/norm1 +1/norm2 +1/norm3) modern formula for harmonic mean equals n/(1/norm1 +1/norm2 +1/norm3 ... 1/nth term) for n terms} proc test3 { combined probability of winning as tianji_win_prob tianji_win_prob has terms of unequal probability win_prob = prob_1 * prob_2 * prob_3, where prob. are equal. tianji_win_prob = prob_1(tianji_low vs king_high) * prob_2(tianji_middle vs king_low) * prob_3(tianji_high vs king_middle) , where prob. terms are not equal.} proc test4 { use llength(lsearch)/llength winner list to calculate probilities of 1) prob_1(tianji_low vs king_high) 2) prob_2(tianji_middle vs king_low) 3) prob_3(tianji_high vs king_middle)} proc test5 { prob_1 = tianji{ $trial > 0 && $trial < .2 } vs king{ $trial > .5 && $trial < 1. } prob_2 = tianji{ $trial > .2 && $trial < .5 } vs king{ $trial > 0 && $trial < .2 } prob_3 = tianji{ $trial > .5 && $trial < 1. } vs king{ $trial > .2 && $trial < .5 } would need 150 trials for answer to gel} proc test6 { check algorithm f(solution) =? initial product check_sum = a+b+c+d+e set answers and printout with resulting values pseudocode: need test cases > small,medium, giant pseudocode: need testcases within range of expected operation. pseudocode: are there any cases too small or large to be solved? pseudocode: limit to size of eTCL list, ref versions. } #end of file ====== ***Testcases Section*** In planning any software, it is advisable to gather a number of testcases to check the results of the program. The math for the testcases can be checked by pasting statements in the TCL console. Aside from the TCL calculator display, when one presses the report button on the calculator, one will have console show access to the capacity functions (subroutines). **** Testcase 1 **** **** Testcase 2 **** **** Testcase 3 **** ---- ***Screenshots Section*** ****figure 1.**** [General Tianji' Horse Race Strategy from China and eTCL demo example screenshot] ****figure 2.**** [General Tianji' Horse Race Strategy from China and eTCL demo wins of high grade horse] ****figure 3.**** [General Tianji' Horse Race Strategy from China and eTCL demo high grade horse probability] ---- ***References:*** * [Horseracing in Tcl] [RS] * Plugging a Math Gap with Harvard mathematician Shing-Tung Yau, * Forbes Magazine 2007, Institute of Mathematical Sciences * in Hong Kong studies betting on horse races and predicting the weather. * Tian Ji , The Horse Racing, Peking University, Ying Fuchen,Xu Pengcheng,Xie Di, ACM Programmer's Contest * The Ambitious Horse, Ancient Chinese Mathematics Problems, Lawence W. Swienciki, Ph.D. * Game-theoreticanalysis of anancient Chinese horse race problem, Mingming Leng, Mahmut Parlar, * DeGroote School of Business, McMaster University, Hamilton, Ont., Canada L8S 4M4 * 10 December 2004 * [Random Walk Equation Slot Calculator Example] * Plugging a Math Gap with Harvard mathematician Shing-Tung Yau, Forbes Magazine 2007, * Institute of Mathematical Sciences * in Hong Kong studies betting on horse races and predicting the weather * [Sumerian Construction Rates and eTCL Slot Calculator Demo Example] * Chabert, J. "Methods of False Position." Ch. 3 in A History of Algorithms: From the Pebble to the Microchip. , 1999. * John Hannah , False position in Leonardo of Pisa's Liber Abbaci,Historia Mathematica Volume 34 issue 3 2007 * Eugene C. Boman,False Position, Double False Position and Cramer's Rule, College Mathematics Journal * Howard Eves ,On the practicality of the rule of false position, Mathematics Teacher Volume 51 * Joy B. Easton, the rule of double false position, Mathematics Teacher Volume 60 * Vera Sanford, the rule of false position, Mathematics Teacher Volume 44 issue 5 1951 * Karine Chemla, Reflections on the World-wide History of the Rule of False Double Position, or: How a Loop Was Closed * Thomas G. Edwards -- Using the Ancient Method of False Position to Find Solutions, Mathematics Teaching in the Middle School Volume 14 issue 4 2008 * [Oneliner's Pie in the Sky] * [One Liners] * [Category Algorithm] * [Babylonian Number Series and eTCL demo example calculator] * [Brahmagupta Area of Cyclic Quadrilateral and eTCL demo example calculator] * [Gauss Approximate Number of Primes and eTCL demo example calculator] * Taking a Weighted Average, Dr Math, mathforum.org/dr.math, dated 03/20/2003 * google , mathforum.org/dr.math * Using Weighted Criteria to Make Decisions, Dr Math>, mathforum.org/dr.math, dated 07/18/2008 * google * harmonic mean discussed in [Additional math functions] * harmonic mean discussed in [Tcl Normalized Extended Benchmarks] * see arithmetic mean on tcl-wiki [Stats] 2011-05-22, [RLE] * see similar 1/(1/nth) terms method used in * [Babylonian Combined Market Rates and eTCL demo example calculator, numerical analysis ] * Zhang Baichun and Jürgen Renn (eds.) , Transformation and Transmission: * Chinese Mechanical Knowledge and the Jesuit Intervention * Joint Research Group of the MPIWG and its Partner Group at the IHNS of the CAS. * Probability models on horse-race outcomes, * MUKHTAR M. ALI, Department of Economics, University of Kentucky, USA * Probability and Optimization Probability and Optimization Models for Racing Horse Models, * Victor S. Y. Lo University of British Columbia University of British Columbia, Fidelity Investments * Designing competitions between teams of individuals, Pingzhong Tanga,Yoav Shohamb,c, Fangzhen Lina, * Mentions ACM horse race problems relative to teams of computer students. * Sun Bin: Distinguished Military Strategist After Sun Zi, By David Wu, Epoch Times * Lecture 4: Matching Algorithms for Bipartite Graphs * Professor: Clifford Stein * Lecture notes on bipartite matching * 100: “Matchings” Fall 2012 * Matching (graph theory) From Wikipedia, the free encyclopedia * Bayes Theorem,Bayes Theorem in short is a method of analysing data. * Massachusetts Institute of Technology Handout 3 * Combinatorial Optimization February 9th, 2009 Michel X. Goemans **Appendix Code** ***appendix TCL programs and scripts *** ====== # real vaporware here # incomplete, still gathering references & testcases for # General Tianji' Chinese Horse Race # pretty print from autoindent and ased editor # General Tianji' Chinese Horse Race Strategy Calculator # written on Windows XP on eTCL # working under TCL version 8.5.6 and 1.0.1 # gold on TCL WIKI, 25feb2017 package require Tk package require math::numtheory namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory } set tcl_precision 17 frame .frame -relief flat -bg aquamarine4 pack .frame -side top -fill y -anchor center set names {{} { low grade horse handicap (a) :} } lappend names { low grade horse score (a) :} lappend names { middle grade horse handicap (b) : } lappend names { middle grade horse score (b) : } lappend names { high grade horse handicap (c) :} lappend names { high grade horse score (c) : } lappend names { optional: number of horses, usually 3 } lappend names { answers: highest score (max fx) :} foreach i {1 2 3 4 5 6 7 8} { label .frame.label$i -text [lindex $names $i] -anchor e entry .frame.entry$i -width 35 -textvariable side$i grid .frame.label$i .frame.entry$i -sticky ew -pady 2 -padx 1 } proc about {} { set msg "Calculator for General Tianji's Chinese Horse Race Strategy from TCL WIKI, written on eTCL " tk_messageBox -title "About" -message $msg } proc random_horses_3 { xx1 } { global side1 side2 side3 global side4 side5 side6 side7 side8 set term1 0 set term2 0 set term3 0 set current 0 set tothorses $xx1 set chance1 [expr {1./$side2 }] set tothorses [expr {int($side7) }] for {set i 0;} {$i<$tothorses} {incr i} { if { [ expr { rand() } ] <= $side2 } {set term1 1 } if { [ expr { rand() } ] <= $side4 } {set term2 1 } if { [ expr { rand() } ] <= $side6 } {set term3 1 } set current [ expr { ($current*1.) +.01*$side1*$term1+.01*$side3*$term2+.01*$side5*$term3+.00000000001 } ] } set side7 $tothorses set side8 [expr {($current*1.)/$tothorses }] if { [ expr { abs($side8) } ] <= .0001 } {set side8 0 } return $side8 } proc calculate { } { global answer2 global side1 side2 side3 side4 side5 global side6 side7 side8 global a_score b_score c_score average_score global testcase_number incr testcase_number set side1 [* $side1 1. ] set side2 [* $side2 1. ] set side3 [* $side3 1. ] set side4 [* $side4 1. ] set side5 [* $side5 1. ] set side6 [* $side6 1. ] set side7 [* $side7 1. ] set side8 [* $side8 1. ] # initialize placeholder answer set maximum_score_rating 1. set score_rating 0.5 set score_rating $side7 set a_score [+ [* $score_rating $side1 ] [* $score_rating $side2 ] ] set b_score [+ [* $score_rating $side3 ] [* $score_rating $side4 ] ] set c_score [+ [* $score_rating $side5 ] [* $score_rating $side6 ] ] set average_score [/ [+ $a_score $b_score $c_score ] 3. ] set maximum_score_rating [max $a_score $b_score $c_score ] #set answer2 $maximum_score_rating set answer2 [ random_horses_3 3 ] set side8 $answer2 } proc fillup {aa bb cc dd ee ff gg hh} { .frame.entry1 insert 0 "$aa" .frame.entry2 insert 0 "$bb" .frame.entry3 insert 0 "$cc" .frame.entry4 insert 0 "$dd" .frame.entry5 insert 0 "$ee" .frame.entry6 insert 0 "$ff" .frame.entry7 insert 0 "$gg" .frame.entry8 insert 0 "$hh" } proc clearx {} { foreach i {1 2 3 4 5 6 7 8 } { .frame.entry$i delete 0 end } } proc reportx {} { global side1 side2 side3 side4 side5 global side6 side7 side8 global a_score b_score c_score average_score global testcase_number console show; puts "%|table $testcase_number|printed in| tcl wiki format|% " puts "&| quantity| value| comment, if any|& " puts "&| $testcase_number:|testcase_number | |& " puts "&| $side1 :|low grade horse handicap (a) | |&" puts "&| $side2 :|low grade horse score (a)| |& " puts "&| $side3 :|middle grade horse handicap (b) | |& " puts "&| $side4 :|middle grade horse score (b)| |&" puts "&| $side5 :|high grade horse handicap (c) | |&" puts "&| $side6 :|high grade horse score (c) | |&" puts "&| $side7 :|optional: number of horses, usually 3. | |&" puts "&| $a_score :|score (a) | |&" puts "&| $b_score :|score (b) | |&" puts "&| $c_score :|score (c) | |&" puts "&| $average_score :|average score per item (a+b+c)/3 | |&" puts "&| $side8 :|answer: highest score (max fx) | |&" } frame .buttons -bg aquamarine4 ::ttk::button .calculator -text "Solve" -command { calculate } ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 5. .3 6. .3 7. .3 3. .10} ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 5. .3 6. .3 7. .3 3. .20 } ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 5. .3 6. .3 7. .3 3. .15 } ::ttk::button .clearallx -text clear -command {clearx } ::ttk::button .about -text about -command {about} ::ttk::button .cons -text report -command { reportx } ::ttk::button .exit -text exit -command {exit} pack .calculator -in .buttons -side top -padx 10 -pady 5 pack .clearallx .cons .about .exit .test4 .test3 .test2 -side bottom -in .buttons grid .frame .buttons -sticky ns -pady {0 10} . configure -background aquamarine4 -highlightcolor brown -relief raised -border 30 wm title . "General Tianji' Chinese Horse Race Strategy Calculator" ====== ---- *** Pushbutton Operation*** For the push buttons, the recommended procedure is push testcase and fill frame, change first three entries etc, push solve, and then push report. Report allows copy and paste from console. For testcases in a computer session, the eTCL calculator increments a new testcase number internally, eg. TC(1), TC(2) , TC(3) , TC(N). The testcase number is internal to the calculator and will not be printed until the report button is pushed for the current result numbers. The current result numbers will be cleared on the next solve button. The command { calculate; reportx } or { calculate ; reportx; clearx } can be added or changed to report automatically. Another wrinkle would be to print out the current text, delimiters, and numbers in a TCL wiki style table as ====== puts " %| testcase $testcase_number | value| units |comment |%" puts " &| volume| $volume| cubic meters |based on length $side1 and width $side2 |&" ====== ---- ** Console program for harmonic_mean** ====== # pretty print from autoindent and ased editor # harmonic_mean for math::stats? package # working under TCL version 8.5.6 and eTCL 1.0.1 # console program written on Windows XP on eTCL # gold on TCL WIKI, 2Mar2017 package require Tk console show package require math::numtheory namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory } set tcl_precision 17 # adapted from tcl-wiki Stats 2011-05-22, arithmetic mean [RLE] # # ::math::harmonic_mean -- # # Return the harmonic mean of two or more values # # Arguments: # val first value # args other values # # Results: # mean harmonic_mean value proc ::math::harmonic_mean {val args} { set sum [ expr { 1./$val } ] set N [ expr { [ llength $args ] + 1 } ] foreach val $args { set sum [ expr { $sum+1./$val } ] } set harmonic_mean [ expr { $N/$sum } ] set harmonic_mean } puts " ::math::harmonic_mean ( 1 2 3 4 5 6 7 8 9 10 ) answer 3.4141715214740551 " puts " [ ::math::harmonic_mean 1 2 3 4 5 6 7 8 9 10 ] 3.4141715214740551 " ====== ---- ** Console program for race output as CVS spreadsheet, includes > equal probability horses < ** ====== # pretty print from autoindent and ased editor # initial console program, Chinese horse race problem # comma delimited spreadsheet for number of horse races # written on Windows XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI, 10feb2017 package require Tk package require math::numtheory namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory } set tcl_precision 17 console show set i 2000 set horse_race_days 1 puts "horse race number , trial heat, winner, wins of particular horse, cum. probability, loser,loser, horse_a , horse_b , horse_c , last place " while {$i>0} { if { $horse_race_days > 180 } { break } set horse_a [ expr { rand() } ] set horse_b [ expr { rand() } ] set horse_c [ expr { rand() } ] set trial [ expr { rand() } ] if { $trial > 0 && $trial < .2 } { set winner_name "horse_a" ; set loser_name "horse_b,horse_c" } if { $trial > .2 && $trial < .5 } { set winner_name "horse_b" ; set loser_name "horse_a,horse_c" } if { $trial > .5 && $trial < 1. } { set winner_name "horse_c" ; set loser_name "horse_a,horse_b" } set winner [max $horse_a $horse_b $horse_c] set last [min $horse_a $horse_b $horse_c] lappend winner_list $winner_name lappend loser_list $loser_name #set number_wins [ lsearch -all $winner_list $winner_name ] set ee [ llength $winner_list ] set kk [ llength [ lsearch -all $winner_list $winner_name ] ] set prob [ expr { ($kk*1.) / $ee } ] if {$winner_name == "horse_c" } { puts "$horse_race_days ,$trial ,$winner_name,$kk,$prob,$loser_name,$trial, $winner, $horse_a , $horse_b , $horse_c , $last "} incr horse_race_days incr i -1 } #end of deck ====== **cvs output for '''> wins only < '''of horse_c** ---- horse race number , trial heat, winner, wins of particular horse, cum. probability, loser,loser, horse_a , horse_b , horse_c , last place 2 ,0.72403450297379612 ,horse_c,1,0.5,horse_a,horse_b,0.72403450297379612, 0.86678423540051297, 0.13588783151278638 , 0.86678423540051297 , 0.042644376420716001 , 0.042644376420716001 3 ,0.87722261151169545 ,horse_c,2,0.66666666666666663,horse_a,horse_b,0.87722261151169545, 0.84789148059109765, 0.84789148059109765 , 0.51211429457744317 , 0.104948963087494 , 0.104948963087494 4 ,0.86264225927304583 ,horse_c,3,0.75,horse_a,horse_b,0.86264225927304583, 0.61519644810594454, 0.48043167706599071 , 0.61519644810594454 , 0.60670331660970267 , 0.48043167706599071 7 ,0.73498046013292873 ,horse_c,4,0.5714285714285714,horse_a,horse_b,0.73498046013292873, 0.27436126688232704, 0.27436126688232704 , 0.18981249127062619 , 0.17854078541441856 , 0.17854078541441856 8 ,0.96185003824618176 ,horse_c,5,0.625,horse_a,horse_b,0.96185003824618176, 0.81659345413399553, 0.81659345413399553 , 0.48618363006328402 , 0.28827047361446101 , 0.28827047361446101 10 ,0.76152973378148381 ,horse_c,6,0.59999999999999998,horse_a,horse_b,0.76152973378148381, 0.95698579864436095, 0.078298148735565204 , 0.95698579864436095 , 0.060317815775199707 , 0.060317815775199707 ---- ** Console program for tianji horse race and combined unequal probability** ====== # pretty print from autoindent and ased editor # subroutine for tianji horse race and combined unequal probability # working under TCL version 8.5.6 and eTCL 1.0.1 # console program written on Windows XP on eTCL # written on Windows XP on eTCL # working under TCL version 8.5.6 and eTCL 1.0.1 # gold on TCL WIKI, 2mar2017 package require Tk package require math::numtheory namespace path {::tcl::mathop ::tcl::mathfunc math::numtheory } set tcl_precision 17 console show proc tianji_horse_race { xxx1 } { set i 2000 set horse_race 1 set trial 1. set trialx 1. set tianji_low 1. set tianji_middle 1. set tianji_high 1. set king_low 1. set king_middle 1. set king_high 1. set program_result {} set winner_list1 {} set winner_list2 {} while {$i>0} { if { $horse_race > 10 } { break } set trial [ expr { rand() } ] set counter 1 set tianji_low [expr 15.* [expr { rand() } ] ] set king_high [expr 50.* [expr { rand() } ] ] # 15 percent = low_grade # 35 percent = medium_grade # 50 percent = high_grade if { [max $tianji_low $king_high ] == $tianji_low } { lappend winner_list2 " horse_race$horse_race heat$counter " tianji_low } if { [max $tianji_low $king_high ] == $king_high } { lappend winner_list2 " horse_race$horse_race heat$counter " king_high } incr counter set tianji_middle [expr 35.* [expr { rand() } ] ] set king_low [expr 15.* [expr { rand() } ] ] if { [max $tianji_middle $king_low ] == $tianji_middle } { lappend winner_list2 " horse_race$horse_race heat$counter " tianji_middle } if { [max $tianji_middle $king_low ] == $king_low } { lappend winner_list2 " horse_race$horse_race heat$counter " king_low } incr counter set tianji_high [expr 50.* [expr { rand() } ] ] set king_middle [expr 35.* [expr { rand() } ] ] if { [max $tianji_high $king_middle ] == $tianji_high } { lappend winner_list2 " horse_race$horse_race heat$counter " tianji_high \n} if { [max $tianji_high $king_middle ] == $king_middle } { lappend winner_list2 " horse_race$horse_race heat$counter " king_middle \n } incr horse_race incr i -1 } return $winner_list2 } set program_result [ tianji_horse_race 3 ] puts " list of winners = \n $program_result " #end of deck ====== **output follows for list of winners in trial races with three 3 heats, > unequal probability horses <** ====== list of winners = { horse_race1 heat1 } king_high { horse_race1 heat2 } tianji_middle { horse_race1 heat3 } tianji_high { } { horse_race2 heat1 } king_high { horse_race2 heat2 } king_low { horse_race2 heat3 } king_middle { } { horse_race3 heat1 } tianji_low { horse_race3 heat2 } tianji_middle { horse_race3 heat3 } tianji_high { } { horse_race4 heat1 } king_high { horse_race4 heat2 } tianji_middle { horse_race4 heat3 } tianji_high { } { horse_race5 heat1 } king_high { horse_race5 heat2 } tianji_middle { horse_race5 heat3 } tianji_high { } { horse_race6 heat1 } king_high { horse_race6 heat2 } tianji_middle { horse_race6 heat3 } tianji_high { } { horse_race7 heat1 } king_high { horse_race7 heat2 } king_low { horse_race7 heat3 } tianji_high { } { horse_race8 heat1 } tianji_low { horse_race8 heat2 } tianji_middle { horse_race8 heat3 } tianji_high { } { horse_race9 heat1 } king_high { horse_race9 heat2 } king_low { horse_race9 heat3 } king_middle { } { horse_race10 heat1 } king_high { horse_race10 heat2 } tianji_middle { horse_race10 heat3 } king_middle { } ====== [gold] This page is copyrighted under the TCL/TK license terms, [http://tcl.tk/software/tcltk/license.html%|%this license]. **Comments Section** <> Please place any comments here, Thanks. <> Numerical Analysis | Toys | Calculator | Mathematics| Example| Toys and Games | Games | Application | GUI ---- <> Development | Concept| Algorithm | Statistics | Spreadsheet