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
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 was two wins and a bag of silver, worth expr(2*$200) or 400 silver dollars. When this bit of strategy was explained later to the king, the king promoted General Tianji to chief of all armies and promoted Sun Bin to the military strategy advisor of General Tianji. Not sure an elegant proof can be drummed up, but there are many random horses in eTCL. The Tianji racehorse problem can be loaded into the eTCL calculator shell and each horse of three can be simulated with a random horse race probability model. 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, Shujing. The Shujing 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 was how the grades of horses were defined in ancient China. The analysis assumed the best grade horse won 50 percent of his races, the middle grade won 35 percent, and the low grade won 15 percent. The eTCL calculator has entries for the grade probabilities to be changed so the effects or rewards of better or lesser random models can be studied.
An alternate method for selecting winners for random horses is somewhat like the sieve method or throwing different colored apples into separate buckets for each horse. The output of the random generator is a decimal fraction between 0.0 and 1.0. Each random horse is assigned a range or band between 0.0 and 1.0, or between the equivalent 0 percent and 100 percent probability. Meaning, the developed formula is 100 percent times the probability of the variable 1.0 from the generator. For example, random horse_a could be assigned the band 0 to 20 percent, random horse_b could be assigned 20 to 30 percent, and random horse_c could be assigned 50 to 100 percent. In different expressions, the probability bands for the horses are 0. > horse_a < 20. , 20> horse_b < 50., and 50. > horse_c < 100.
Some trial code for racehorse accidents or racehorse troubles can be developed for random horses. For the example below, there is a 15 percent chance of hoof infection. If the statement on hoof infection fires, there is a 0.1 factor or 10 percent loss as short_fall of the finish line or 10 percent reduction in normal horse speed. Also, horse speed may not be constant, so a different exponential curve or exponential constant could be selected, reduced, or changed for the horse speed. Notice the potential loss for the horse is calculated independent of whether the hoof infection flag is yes or no (0 or 1). The potential for trouble is there whether or not the accident actually occurs. The potential for trouble can be calculated, so strategy or insurance can be adjusted for the horse race.
The Tainji horserace as a computer contest problem is featured in ACM student contests with an average of 6000 Chinese entries ( computer programs ). Most of the entries featured bipartite matching algorithms. For the eTCL calculator, the calculator is returning the results of random models so the random results are not always the same with each solve. Over a number of cases, the cumulative or average results should approach the results or winnings of General Tianji.
# 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 proc test3 { combined probability of winning as tianji_win_prob tianji_win_prob has terms of unequal probability proc test4 { use llength(lsearch)/llength winner list to calculate probilities of 1) prob_1(tianji_low vs king_high) horses 2) prob_2(tianji_middle vs king_low) horses 3) prob_3(tianji_high vs king_middle)} horses proc test5 { prob_1 = tianji high probability * [expr { rand() } ] prob_2 = tianji medium probability * [expr { rand() } ] prob_3 = tianji low probability * [expr { rand() } ] would need ~100 trials for answer to gel, in iterative solution } 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
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).
table 1 | printed in | tcl wiki format |
---|---|---|
quantity | value | comment, if any |
1: | testcase_number | |
50.0 : | high grade horse probability percent (a) , usually 50 | |
35.0 : | medium grade horse probability percent (b) , usually 35 | |
15.0 : | low grade horse probability percent (c) , usually 15 | |
1.0 : | optional: | |
1.0 : | optional: | |
1.0 : | optional: | |
3.0 : | optional: number Tianji wins, 0 to max 3 | random results |
600.0 : | answer: total winnings silver dollars | random results |
table 1 | printed in | tcl wiki format |
---|---|---|
quantity | value | comment, if any |
2 : | testcase_number | |
50.0 : | high grade horse probability percent (a) , usually 50 | |
30.0 : | medium grade horse probability percent (b) , usually 35 | |
20.0 : | low grade horse probability percent (c) , usually 15 | |
1.0 : | optional: | |
1.0 : | optional: | |
1.0 : | optional: | |
1.0 : | optional: number Tianji wins, 0 to max 3 | random results |
200.0 : | answer: total winnings silver dollars | random results |
table 3 | printed in | tcl wiki format |
---|---|---|
quantity | value | comment, if any |
3 : | testcase_number | |
50.0 : | high grade horse probability percent (a) , usually 50 | |
40.0 : | medium grade horse probability percent (b) , usually 35 | |
10.0 : | low grade horse probability percent (c) , usually 15 | |
1.0 : | optional: | |
1.0 : | optional: | |
1.0 : | optional: | |
1.0 : | optional: number Tianji wins, 0 to max 3 | random results |
200.0 : | answer: total winnings silver dollars | random results |
# 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 {{} { high grade horse probability percent (a) , usually 50 :} } lappend names { medium grade horse probability percent (b) , usually 35:} lappend names { low grade horse probability percent (c) , usually 15: } lappend names { optional: : } lappend names { answers: check sum all wins =? 3 } lappend names { number of King wins, 0 to max 3 } lappend names { number of Tianji wins, 0 to max 3 : } lappend names { total winnings for Tianji silver dollars :} 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 tianji_horse_race { xxx1 } { global side1 side2 side3 side4 side5 global side6 side7 side8 set high_grade_prob 50. set medium_grade_prob 35. set low_grade_prob 15. set tianji_winnings 0. set king_winnings 0. # normal 15 percent = low_grade_prob # normal 35 percent = medium_grade_prob # normal 50 percent = high_grade_prob set high_grade_prob $side1 set medium_grade_prob $side2 set low_grade_prob $side3 set tianji_low [expr $low_grade_prob * [expr { rand() } ] ] set king_high [expr $high_grade_prob * [expr { rand() } ] ] set tianji_low 1.0 set tianji_medium 1.0 set tianji_high 1.0 set king_low 1.0 set king_medium 1.0 set king_high 1.0 set check_sum 0.0 # 15 percent = low_grade # 35 percent = medium_grade # 50 percent = high_grade set tianji_low [expr $low_grade_prob * [expr { rand() } ] ] set king_high [expr $high_grade_prob * [expr { rand() } ] ] if { [max $tianji_low $king_high ] == $tianji_low } { set tianji_winnings 1. } if { [max $tianji_low $king_high ] == $king_high } { set king_winnings 1. } set tianji_medium [expr $medium_grade_prob * [expr { rand() } ] ] set king_low [expr $low_grade_prob * [expr { rand() } ] ] if { [max $tianji_medium $king_low ] == $tianji_medium } { set tianji_winnings [+ $tianji_winnings 1.] } if { [max $tianji_medium $king_low ] == $king_low } { set king_winnings [+ $king_winnings 1. ] } set tianji_high [expr $high_grade_prob * [expr { rand() } ] ] set king_medium [expr $medium_grade_prob * [expr { rand() } ] ] if { [max $tianji_high $king_medium ] == $tianji_high } { set tianji_winnings [+ $tianji_winnings 1.] } if { [max $tianji_high $king_medium ] == $king_medium } { set king_winnings [+ $king_winnings 1. ] } set check_sum [+ $king_winnings $tianji_winnings ] set side5 $check_sum set side6 $king_winnings return $tianji_winnings } proc calculate { } { global answer2 global side1 side2 side3 side4 side5 global side6 side7 side8 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 answer2 [tianji_horse_race 3 ] set side7 $answer2 set side8 [* 200. $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 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 :|high grade horse probability percent (a) , usually 50 | |&" puts "&| $side2 :|medium grade horse probability percent (b) , usually 35 | |& " puts "&| $side3 :|low grade horse probability percent (c) , usually 15 | |& " puts "&| $side4 :|optional:| |&" puts "&| $side5 :|answers: check sum all wins =? 3 : | |& " puts "&| $side6 :|number king wins, 0 to max 3 | random results |& " puts "&| $side7 :|number Tianji wins, 0 to max 3 | random results |& " puts "&| $side8 :|total winnings for Tianji silver dollars | random results |&" } frame .buttons -bg aquamarine4 ::ttk::button .calculator -text "Solve" -command { calculate } ::ttk::button .test2 -text "Testcase1" -command {clearx;fillup 50. 35. 15. 1.0 0. 0. 0. 0.} ::ttk::button .test3 -text "Testcase2" -command {clearx;fillup 50. 30. 20. 1.0 0. 0. 0. 0. } ::ttk::button .test4 -text "Testcase3" -command {clearx;fillup 50. 40. 10. 1.0 0. 0. 0. 0. } ::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" # end of deck
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 |&"
# 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
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
# 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
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 { }
# random horses in a race 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]
if { [ expr { rand() } ] <= .15 } {set hoof_infection_this_year 1 } set race_meters_shortfall [ expr { $meters*.1*rand() } ] set race_meters_shortfall [ expr { int($race_meters_shortfall ) } ] (...many...) set total_meters [ expr { $meters - $race_meters_shortfall*.1*$hoof_infection_this_year # or decrement speed of horse if more effective in program set total_meters_speed [ expr { $normal_meters_speed - $race_speed_shortfall*.1*$hoof_infection_this_year set total_meters_speed_exponent [ expr { $normal_meters_speed_exponent*.1*$hoof_infection_this_year
gold This page is copyrighted under the TCL/TK license terms, this license .
Please place any comments here, Thanks.
Category Numerical Analysis | Category Toys | Category Calculator | Category Mathematics | Category Example | Toys and Games | Category Games | Category Application | Category GUI |