{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":42674,"title":"Cody meets Xiangqi: foresee the unseen (Part 1)","description":"This is the first part of the Xiangqi series. The second part in this series is: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2 Cody meets Xiangqi: foresee the unseen (Part 2)\u003e\r\n\r\n\u003chttps://en.wikipedia.org/wiki/Xiangqi Xiangqi\u003e, also known as *Chinese Chess* (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \"Chu River–Han border\", in reference to the Chu–Han Contention between \u003chttps://en.wikipedia.org/wiki/Xiang_Yu Xiang Yu\u003e and \u003chttps://de.wikipedia.org/wiki/Han_Gaozu Liu Bang\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to \u003chttps://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention here\u003e.\r\n\r\nFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question: _Who is the stronger player of Xiangqi between Xiang Yu and Liu Bang_? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner: \r\n\r\n_In a succession of Xiangqi games, once Xiang Yu wins Na games *consecutively*, whereas Liu Bang has not won Nb games *consecutively*, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times *consecutively*, whereas Xiang Yu has not won Na times *consecutively*, Liu Bang becomes the winner._ \r\n\r\nCody suggests that Na \u003e 1 and Nb \u003e 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\r\n\r\n                                sol = Xiangqi(p, Na, Nb)\r\n\r\nwith input: 0 \u003c= p \u003c= 1, Na \u003e 1, Nb \u003e 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\r\n\r\n                      abs(sol - Q) \u003c= max(AbsTol, RelTol*abs(sol))\r\n\r\nwhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the *speed* of your code. \r\n\r\nHave fun!\r\n","description_html":"\u003cp\u003eThis is the first part of the Xiangqi series. The second part in this series is: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2\"\u003eCody meets Xiangqi: foresee the unseen (Part 2)\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Xiangqi\"\u003eXiangqi\u003c/a\u003e, also known as \u003cb\u003eChinese Chess\u003c/b\u003e (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \"Chu River–Han border\", in reference to the Chu–Han Contention between \u003ca href = \"https://en.wikipedia.org/wiki/Xiang_Yu\"\u003eXiang Yu\u003c/a\u003e and \u003ca href = \"https://de.wikipedia.org/wiki/Han_Gaozu\"\u003eLiu Bang\u003c/a\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to \u003ca href = \"https://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention\"\u003ehere\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question: \u003ci\u003eWho is the stronger player of Xiangqi between Xiang Yu and Liu Bang\u003c/i\u003e? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner:\u003c/p\u003e\u003cp\u003e\u003ci\u003eIn a succession of Xiangqi games, once Xiang Yu wins Na games \u003cb\u003econsecutively\u003c/b\u003e, whereas Liu Bang has not won Nb games \u003cb\u003econsecutively\u003c/b\u003e, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times \u003cb\u003econsecutively\u003c/b\u003e, whereas Xiang Yu has not won Na times \u003cb\u003econsecutively\u003c/b\u003e, Liu Bang becomes the winner.\u003c/i\u003e\u003c/p\u003e\u003cp\u003eCody suggests that Na \u0026gt; 1 and Nb \u0026gt; 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\u003c/p\u003e\u003cpre\u003e                                sol = Xiangqi(p, Na, Nb)\u003c/pre\u003e\u003cp\u003ewith input: 0 \u0026lt;= p \u0026lt;= 1, Na \u0026gt; 1, Nb \u0026gt; 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\u003c/p\u003e\u003cpre\u003e                      abs(sol - Q) \u0026lt;= max(AbsTol, RelTol*abs(sol))\u003c/pre\u003e\u003cp\u003ewhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the \u003cb\u003espeed\u003c/b\u003e of your code.\u003c/p\u003e\u003cp\u003eHave fun!\u003c/p\u003e","function_template":"function sol = Xiangqi(p, Na, Nb)\r\n  sol = p;\r\nend","test_suite":"%%\r\n% By courtesy of Alfonso Nieto-Castanon\r\nurlwrite('https://sites.google.com/a/alfnie.com/alfnie/software/SetSolutionScore.p?attredirects=0\u0026amp;d=1','SetSolutionScore.p');\r\nrehash path;\r\n\r\n%%\r\nfh = fopen('EvaluateSolution.p','wb');\r\nfwrite(fh, hex2dec(reshape('7630312E30307630302E3030000E601C0AF25FB100000056000000A4000000D6820EB5B30514117A9E6E5DB36898AFFFCC5086DFAF59C2910AEB07B88523DABE546868AC2BDAC3795467A7BCD91A89E2F578F2EDE92D63472A3B8FCA3F216CB3B66B010B5B924A5F514E19B90225B0978A54DA881119917D211CB055361918CAA0670F6D0E8ED17B319492619F4361BFB4C3C31D68E11F4BA084C6456783C358296B3E63E16C78EF2B0279074BCB707265EB4C044BFF7F25BA0A9678B75D36B9ACEE6853',2,[]).')); rehash path; fclose(fh); \r\n\r\n%%\r\np = 0; Na = 2; Nb = 3;\r\nAbsTol = 1e-6; RelTol = 1e-5;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 1; Na = 3; Nb = 2;\r\nAbsTol = 1e-6; RelTol = 1e-5;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.4; Na = 2; Nb = 3;\r\nAbsTol = 5e-4; RelTol = 5e-4;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.7; Na = 4; Nb = 2;\r\nAbsTol = 5e-4; RelTol = 5e-4;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.15; Na = 4; Nb = 2;\r\nAbsTol = 5e-5; RelTol = 1e-6;\r\nt = builtin('tic');\r\nsol = Xiangqi(p, Na, Nb);\r\nscore = builtin('toc',t);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\nSetSolutionScore(round(500*score));","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2015-10-30T08:18:09.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-10-30T05:02:43.000Z","updated_at":"2025-11-30T16:38:45.000Z","published_at":"2015-10-30T05:45:36.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the first part of the Xiangqi series. The second part in this series is:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody meets Xiangqi: foresee the unseen (Part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiangqi\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiangqi\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, also known as\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChinese Chess\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \\\"Chu River–Han border\\\", in reference to the Chu–Han Contention between\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiang_Yu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiang Yu\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://de.wikipedia.org/wiki/Han_Gaozu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLiu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehere\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eWho is the stronger player of Xiangqi between Xiang Yu and Liu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eIn a succession of Xiangqi games, once Xiang Yu wins Na games\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, whereas Liu Bang has not won Nb games\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, whereas Xiang Yu has not won Na times\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, Liu Bang becomes the winner.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCody suggests that Na \u0026gt; 1 and Nb \u0026gt; 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                                sol = Xiangqi(p, Na, Nb)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewith input: 0 \u0026lt;= p \u0026lt;= 1, Na \u0026gt; 1, Nb \u0026gt; 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                      abs(sol - Q) \u003c= max(AbsTol, RelTol*abs(sol))]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003espeed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of your code.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHave fun!\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":42685,"title":"Cody meets Xiangqi: foresee the unseen (Part 2)","description":"This is the second part of the Xiangqi series. The first part in this series is: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen Cody meets Xiangqi: foresee the unseen (Part 1)\u003e\r\n\r\nBeing increasingly interested in \u003chttps://en.wikipedia.org/wiki/Xiangqi Xiangqi\u003e (a.k.a., *Chinese Chess*), Mr. Cody has designed a new Xiangqi match for \u003chttps://en.wikipedia.org/wiki/Xiang_Yu Xiang Yu\u003e and \u003chttps://de.wikipedia.org/wiki/Han_Gaozu Liu Bang\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\r\n\r\nOnce\r\n\r\n   1) Xiang Yu wins Na games consecutively,\r\n   2) Liu Bang wins Nb games consecutively, \r\n   3) No ties occur consecutively, \r\n\r\n*whichever comes first*, Mr. Cody announces the outcome accordingly as follows:\r\n\r\n   1) Xiang Yu is the final winner,\r\n   2) Liu Bang is the final winner, \r\n   3) They end up with a final draw.\r\n\r\nAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\r\n\r\n                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\r\n\r\nwhere \r\n\r\n* a: the probability that Xiang Yu wins one individual game\r\n* b: the probability that Liu Bang wins one individual game\r\n* Na: # of consecutive wins required for Xiang Yu to become the final winner\r\n* Nb: # of consecutive wins required for Liu Bang to become the final winner\r\n* Nc: # of consecutive ties required to result in a final draw\r\n* Pa: the probability that Xiang Yu wins the match\r\n* Pb: the probability that Liu Bang wins the match\r\n* Pc: the probability of a final draw\r\n\r\nThe main focus of this problem is on *Monte Carlo simulations*, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\r\n\r\n1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u003c tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected. \r\n\r\n2) Check if your solution is based on *pure Monte Carlo simulations* or *analytical approaches*. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations. \r\n\r\n3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get. \r\n\r\nIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks. \r\n\r\n ","description_html":"\u003cp\u003eThis is the second part of the Xiangqi series. The first part in this series is: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen\"\u003eCody meets Xiangqi: foresee the unseen (Part 1)\u003c/a\u003e\u003c/p\u003e\u003cp\u003eBeing increasingly interested in \u003ca href = \"https://en.wikipedia.org/wiki/Xiangqi\"\u003eXiangqi\u003c/a\u003e (a.k.a., \u003cb\u003eChinese Chess\u003c/b\u003e), Mr. Cody has designed a new Xiangqi match for \u003ca href = \"https://en.wikipedia.org/wiki/Xiang_Yu\"\u003eXiang Yu\u003c/a\u003e and \u003ca href = \"https://de.wikipedia.org/wiki/Han_Gaozu\"\u003eLiu Bang\u003c/a\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\u003c/p\u003e\u003cp\u003eOnce\u003c/p\u003e\u003cpre\u003e   1) Xiang Yu wins Na games consecutively,\r\n   2) Liu Bang wins Nb games consecutively, \r\n   3) No ties occur consecutively, \u003c/pre\u003e\u003cp\u003e\u003cb\u003ewhichever comes first\u003c/b\u003e, Mr. Cody announces the outcome accordingly as follows:\u003c/p\u003e\u003cpre\u003e   1) Xiang Yu is the final winner,\r\n   2) Liu Bang is the final winner, \r\n   3) They end up with a final draw.\u003c/pre\u003e\u003cp\u003eAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\u003c/p\u003e\u003cpre\u003e                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\u003c/pre\u003e\u003cp\u003ewhere\u003c/p\u003e\u003cul\u003e\u003cli\u003ea: the probability that Xiang Yu wins one individual game\u003c/li\u003e\u003cli\u003eb: the probability that Liu Bang wins one individual game\u003c/li\u003e\u003cli\u003eNa: # of consecutive wins required for Xiang Yu to become the final winner\u003c/li\u003e\u003cli\u003eNb: # of consecutive wins required for Liu Bang to become the final winner\u003c/li\u003e\u003cli\u003eNc: # of consecutive ties required to result in a final draw\u003c/li\u003e\u003cli\u003ePa: the probability that Xiang Yu wins the match\u003c/li\u003e\u003cli\u003ePb: the probability that Liu Bang wins the match\u003c/li\u003e\u003cli\u003ePc: the probability of a final draw\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eThe main focus of this problem is on \u003cb\u003eMonte Carlo simulations\u003c/b\u003e, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\u003c/p\u003e\u003cp\u003e1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u0026lt; tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected.\u003c/p\u003e\u003cp\u003e2) Check if your solution is based on \u003cb\u003epure Monte Carlo simulations\u003c/b\u003e or \u003cb\u003eanalytical approaches\u003c/b\u003e. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations.\u003c/p\u003e\u003cp\u003e3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get.\u003c/p\u003e\u003cp\u003eIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks.\u003c/p\u003e","function_template":"function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\r\n% a: the probability that Xiang Yu wins one individual game\r\n% b: the probability that Liu Bang wins one individual game\r\n% Na: # of consecutive wins required for Xiang Yu to become the final winner\r\n% Nb: # of consecutive wins required for Liu Bang to become the final winner\r\n% Nc: # of consecutive ties required to result in a final draw\r\n% Pa: the probability that Xiang Yu wins the match\r\n% Pb: the probability that Liu Bang wins the match\r\n% Pc: the probability of a final draw\r\n    Pa = ;\r\n    Pb = ;\r\n    Pc = ;\r\nend","test_suite":"%%\r\n% Thanks to Alfonso Nieto-Castanon\r\nurlwrite('https://sites.google.com/a/alfnie.com/alfnie/software/SetSolutionScore.p?attredirects=0\u0026amp;d=1','SetSolutionScore.p');\r\nrehash path;\r\n\r\n%%\r\nfh = fopen('EvaluateSolution.p','wb');\r\nfwrite(fh, hex2dec(reshape('7630312E30307630302E3030000C701C97F61FB1000002D5000001CF000004E73DD68930A391F7C60A534B45A03EAF72EB08941F39EE01BF25BAE04DF43CF342FC1A763DF6B8F26BBED0BD4F2ABBB5927B1EEBAE8795E487F6E4EF2737CBB6646BC4DF145E14664B3A4DACCD7CB01C4EC2328AD76F196231D2CA02CDC2B15466FBA5BDDF9E6C0E5DE12CF07B2AAA50BD2F04FFB92E9BECBE232E01031340A8EDCA5C10DAC01BBE43685ED0AB79D9C6F2A090FB4E0E75CAA236D3D73AD659E0705C42792BC77D85951B2FC49DE856FB97AFD74C1DB66C874EDF5517BCFA14C6706CA5E61DE60F2771B64F6D634B858A1A30AD7C49778534CCCE7551C637DC53846B02140046F729C5EB2DC9C65F16C2FC4F34EA9F03A2056B8218ACAB9A9A8BC5DC8F2F3312740F86626ABA38E00903CE76846DEE175BEE04DC0815E050E4CD95BA8E5BD27A0B57F2413B71A0E4837FB0F86328AA82732C584F1F55C6CCD79CBC69D052011BA93357AAE3568E0086F159C083D665645A38584955283925F900254A6562F0C755323C40805328D04F27FD863E8B774B52E27FC3AA30CE689AC57DEFEE274DBBC2A4D9D320CF19AD873AA0AE806721EE78496F7ADA99EA48060F26FDEE7ED5E9F85B27F79AE176A6E8EAC4DD5127299122FF88139BC4B56A2ED3C5338A72676C4C99AD6DEBCD8BB5EB09',2,[]).')); rehash path; fclose(fh);\r\n\r\n%%\r\nfid = fopen('Xiangqi2.m');\r\ndelim = {' ', '\\n', ',', '.', ';', '''', '@', '+', '-', '*', '/', '\\', '^', '\u003e', '\u003c', '=', '\u0026', '|', '~', '{', '}', '[', ']', '(', ')'};\r\nfile = textscan(fid, '%s', 'CommentStyle', '%', 'MultipleDelimsAsOne', 1, 'Delimiter', delim); fclose(fid); \r\nassert(~any(ismember({'rng','RandStream','seed','state','twister','shufle','default'},file{1})));\r\n\r\n%%\r\na = 0; b = 0; Na = 2; Nb = 3; Nc = 2; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 0; b = 1; Na = 1; Nb = 2; Nc = 1; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 1; b = 0; Na = 3; Nb = 2; Nc = 1; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 0.15; b = 0.85; Na = 4; Nb = 2; Nc = 1; tol = 1e-4;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\na = 0.9; b = 0; Na = 3; Nb = 1; Nc = 2; tol = 1e-3;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\na = 0.65; b = 0.3; Na = 3; Nb = 2; Nc = 2; tol = 1e-3;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\nNa = 3; Nb = 2; Nc = 1; tol = 2e-3; \r\np = sort(rand(2,30)); \r\np = sort([p(1,:);diff(p);1-p(2,:)]);\r\nfor k = size(p,2):-1:1\r\n    a = p(3,k); b = p(2,k);\r\n    score(k) = EvaluateSolution(a, b, Na, Nb, Nc, tol);    \r\nend\r\nSetSolutionScore(round(mean(score)));","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":"2015-11-12T00:41:35.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2015-11-08T20:51:55.000Z","updated_at":"2015-11-12T03:39:15.000Z","published_at":"2015-11-10T00:22:37.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the second part of the Xiangqi series. The first part in this series is:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody meets Xiangqi: foresee the unseen (Part 1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBeing increasingly interested in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiangqi\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiangqi\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (a.k.a.,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChinese Chess\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), Mr. Cody has designed a new Xiangqi match for\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiang_Yu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiang Yu\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://de.wikipedia.org/wiki/Han_Gaozu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLiu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOnce\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   1) Xiang Yu wins Na games consecutively,\\n   2) Liu Bang wins Nb games consecutively, \\n   3) No ties occur consecutively,]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewhichever comes first\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, Mr. Cody announces the outcome accordingly as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   1) Xiang Yu is the final winner,\\n   2) Liu Bang is the final winner, \\n   3) They end up with a final draw.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ea: the probability that Xiang Yu wins one individual game\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb: the probability that Liu Bang wins one individual game\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNa: # of consecutive wins required for Xiang Yu to become the final winner\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNb: # of consecutive wins required for Liu Bang to become the final winner\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNc: # of consecutive ties required to result in a final draw\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePa: the probability that Xiang Yu wins the match\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePb: the probability that Liu Bang wins the match\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePc: the probability of a final draw\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe main focus of this problem is on\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMonte Carlo simulations\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u0026lt; tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2) Check if your solution is based on\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003epure Monte Carlo simulations\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eanalytical approaches\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":42674,"title":"Cody meets Xiangqi: foresee the unseen (Part 1)","description":"This is the first part of the Xiangqi series. The second part in this series is: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2 Cody meets Xiangqi: foresee the unseen (Part 2)\u003e\r\n\r\n\u003chttps://en.wikipedia.org/wiki/Xiangqi Xiangqi\u003e, also known as *Chinese Chess* (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \"Chu River–Han border\", in reference to the Chu–Han Contention between \u003chttps://en.wikipedia.org/wiki/Xiang_Yu Xiang Yu\u003e and \u003chttps://de.wikipedia.org/wiki/Han_Gaozu Liu Bang\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to \u003chttps://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention here\u003e.\r\n\r\nFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question: _Who is the stronger player of Xiangqi between Xiang Yu and Liu Bang_? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner: \r\n\r\n_In a succession of Xiangqi games, once Xiang Yu wins Na games *consecutively*, whereas Liu Bang has not won Nb games *consecutively*, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times *consecutively*, whereas Xiang Yu has not won Na times *consecutively*, Liu Bang becomes the winner._ \r\n\r\nCody suggests that Na \u003e 1 and Nb \u003e 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\r\n\r\n                                sol = Xiangqi(p, Na, Nb)\r\n\r\nwith input: 0 \u003c= p \u003c= 1, Na \u003e 1, Nb \u003e 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\r\n\r\n                      abs(sol - Q) \u003c= max(AbsTol, RelTol*abs(sol))\r\n\r\nwhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the *speed* of your code. \r\n\r\nHave fun!\r\n","description_html":"\u003cp\u003eThis is the first part of the Xiangqi series. The second part in this series is: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2\"\u003eCody meets Xiangqi: foresee the unseen (Part 2)\u003c/a\u003e\u003c/p\u003e\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Xiangqi\"\u003eXiangqi\u003c/a\u003e, also known as \u003cb\u003eChinese Chess\u003c/b\u003e (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \"Chu River–Han border\", in reference to the Chu–Han Contention between \u003ca href = \"https://en.wikipedia.org/wiki/Xiang_Yu\"\u003eXiang Yu\u003c/a\u003e and \u003ca href = \"https://de.wikipedia.org/wiki/Han_Gaozu\"\u003eLiu Bang\u003c/a\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to \u003ca href = \"https://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention\"\u003ehere\u003c/a\u003e.\u003c/p\u003e\u003cp\u003eFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question: \u003ci\u003eWho is the stronger player of Xiangqi between Xiang Yu and Liu Bang\u003c/i\u003e? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner:\u003c/p\u003e\u003cp\u003e\u003ci\u003eIn a succession of Xiangqi games, once Xiang Yu wins Na games \u003cb\u003econsecutively\u003c/b\u003e, whereas Liu Bang has not won Nb games \u003cb\u003econsecutively\u003c/b\u003e, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times \u003cb\u003econsecutively\u003c/b\u003e, whereas Xiang Yu has not won Na times \u003cb\u003econsecutively\u003c/b\u003e, Liu Bang becomes the winner.\u003c/i\u003e\u003c/p\u003e\u003cp\u003eCody suggests that Na \u0026gt; 1 and Nb \u0026gt; 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\u003c/p\u003e\u003cpre\u003e                                sol = Xiangqi(p, Na, Nb)\u003c/pre\u003e\u003cp\u003ewith input: 0 \u0026lt;= p \u0026lt;= 1, Na \u0026gt; 1, Nb \u0026gt; 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\u003c/p\u003e\u003cpre\u003e                      abs(sol - Q) \u0026lt;= max(AbsTol, RelTol*abs(sol))\u003c/pre\u003e\u003cp\u003ewhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the \u003cb\u003espeed\u003c/b\u003e of your code.\u003c/p\u003e\u003cp\u003eHave fun!\u003c/p\u003e","function_template":"function sol = Xiangqi(p, Na, Nb)\r\n  sol = p;\r\nend","test_suite":"%%\r\n% By courtesy of Alfonso Nieto-Castanon\r\nurlwrite('https://sites.google.com/a/alfnie.com/alfnie/software/SetSolutionScore.p?attredirects=0\u0026amp;d=1','SetSolutionScore.p');\r\nrehash path;\r\n\r\n%%\r\nfh = fopen('EvaluateSolution.p','wb');\r\nfwrite(fh, hex2dec(reshape('7630312E30307630302E3030000E601C0AF25FB100000056000000A4000000D6820EB5B30514117A9E6E5DB36898AFFFCC5086DFAF59C2910AEB07B88523DABE546868AC2BDAC3795467A7BCD91A89E2F578F2EDE92D63472A3B8FCA3F216CB3B66B010B5B924A5F514E19B90225B0978A54DA881119917D211CB055361918CAA0670F6D0E8ED17B319492619F4361BFB4C3C31D68E11F4BA084C6456783C358296B3E63E16C78EF2B0279074BCB707265EB4C044BFF7F25BA0A9678B75D36B9ACEE6853',2,[]).')); rehash path; fclose(fh); \r\n\r\n%%\r\np = 0; Na = 2; Nb = 3;\r\nAbsTol = 1e-6; RelTol = 1e-5;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 1; Na = 3; Nb = 2;\r\nAbsTol = 1e-6; RelTol = 1e-5;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.4; Na = 2; Nb = 3;\r\nAbsTol = 5e-4; RelTol = 5e-4;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.7; Na = 4; Nb = 2;\r\nAbsTol = 5e-4; RelTol = 5e-4;\r\nsol = Xiangqi(p, Na, Nb);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\n\r\n%%\r\np = 0.15; Na = 4; Nb = 2;\r\nAbsTol = 5e-5; RelTol = 1e-6;\r\nt = builtin('tic');\r\nsol = Xiangqi(p, Na, Nb);\r\nscore = builtin('toc',t);\r\nassert(EvaluateSolution(p,Na,Nb,sol,AbsTol,RelTol));\r\nSetSolutionScore(round(500*score));","published":true,"deleted":false,"likes_count":3,"comments_count":1,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":9,"test_suite_updated_at":"2015-10-30T08:18:09.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2015-10-30T05:02:43.000Z","updated_at":"2025-11-30T16:38:45.000Z","published_at":"2015-10-30T05:45:36.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the first part of the Xiangqi series. The second part in this series is:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/42685-cody-meets-xiangqi-foresee-the-unseen-part-2\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody meets Xiangqi: foresee the unseen (Part 2)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiangqi\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiangqi\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, also known as\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChinese Chess\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (and 象棋 in Chinese characters), is one of the most popular board games in China. The modern Xiangqi board contains a middle section which divides two players' sides and is marked the \\\"Chu River–Han border\\\", in reference to the Chu–Han Contention between\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiang_Yu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiang Yu\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://de.wikipedia.org/wiki/Han_Gaozu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLiu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, two prominent warlords and opponents who fought thousands of battles against each other for supremacy over China in the late Qin dynasty (206–202 BC). Those interested in the story of the Chu–Han Contention and its relation to Xiangqi are referred to\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Chu%E2%80%93Han_Contention\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ehere\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFresh to Xiangqi, Cody becomes interested in Xiangqi by raising a question:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eWho is the stronger player of Xiangqi between Xiang Yu and Liu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e? To answer this question, Cody designs a match for Xiang Yu and Liu Bang, in which Cody serves as the referee. The smart Cody referee also sets an intelligent rule to determine the winner:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eIn a succession of Xiangqi games, once Xiang Yu wins Na games\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, whereas Liu Bang has not won Nb games\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, Cody immediately announces Xiang Yu as the winner. Contrarily, once Liu Bang defeats Xiang Yu Nb times\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, whereas Xiang Yu has not won Na times\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003econsecutively\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003e, Liu Bang becomes the winner.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eCody suggests that Na \u0026gt; 1 and Nb \u0026gt; 1, in order to enhance, to some extent, the confidence of the result of the match. Suppose in each individual game, the probability Xiang Yu would win is p, and the probability Liu Bang would win is 1 - p, which implicitly assumes that the probability of a tie is 0 (because they both refuse to draw and will fight to death). Unfortunately, this well-designed match has never taken place. Regretfully, Cody requests us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write a function\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                                sol = Xiangqi(p, Na, Nb)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewith input: 0 \u0026lt;= p \u0026lt;= 1, Na \u0026gt; 1, Nb \u0026gt; 1, and output: sol --- the probability that Xiang Yu wins. Your solution will be tested against its true value Q (which is computed but hided in the P-file EvaluateSolution.p) according to a hybrid absolute and relative error tolerance criterion:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                      abs(sol - Q) \u003c= max(AbsTol, RelTol*abs(sol))]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere AbsTol and RelTol are absolute and relative error tolerances, respectively, which will be specified in the test suite. You are encouraged to optimize the performance (rather than the usual Cody size) of your code as much as possible, as the score of your solution will be measured based on the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003espeed\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e of your code.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHave fun!\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":42685,"title":"Cody meets Xiangqi: foresee the unseen (Part 2)","description":"This is the second part of the Xiangqi series. The first part in this series is: \u003chttp://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen Cody meets Xiangqi: foresee the unseen (Part 1)\u003e\r\n\r\nBeing increasingly interested in \u003chttps://en.wikipedia.org/wiki/Xiangqi Xiangqi\u003e (a.k.a., *Chinese Chess*), Mr. Cody has designed a new Xiangqi match for \u003chttps://en.wikipedia.org/wiki/Xiang_Yu Xiang Yu\u003e and \u003chttps://de.wikipedia.org/wiki/Han_Gaozu Liu Bang\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\r\n\r\nOnce\r\n\r\n   1) Xiang Yu wins Na games consecutively,\r\n   2) Liu Bang wins Nb games consecutively, \r\n   3) No ties occur consecutively, \r\n\r\n*whichever comes first*, Mr. Cody announces the outcome accordingly as follows:\r\n\r\n   1) Xiang Yu is the final winner,\r\n   2) Liu Bang is the final winner, \r\n   3) They end up with a final draw.\r\n\r\nAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\r\n\r\n                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\r\n\r\nwhere \r\n\r\n* a: the probability that Xiang Yu wins one individual game\r\n* b: the probability that Liu Bang wins one individual game\r\n* Na: # of consecutive wins required for Xiang Yu to become the final winner\r\n* Nb: # of consecutive wins required for Liu Bang to become the final winner\r\n* Nc: # of consecutive ties required to result in a final draw\r\n* Pa: the probability that Xiang Yu wins the match\r\n* Pb: the probability that Liu Bang wins the match\r\n* Pc: the probability of a final draw\r\n\r\nThe main focus of this problem is on *Monte Carlo simulations*, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\r\n\r\n1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u003c tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected. \r\n\r\n2) Check if your solution is based on *pure Monte Carlo simulations* or *analytical approaches*. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations. \r\n\r\n3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get. \r\n\r\nIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks. \r\n\r\n ","description_html":"\u003cp\u003eThis is the second part of the Xiangqi series. The first part in this series is: \u003ca href = \"http://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen\"\u003eCody meets Xiangqi: foresee the unseen (Part 1)\u003c/a\u003e\u003c/p\u003e\u003cp\u003eBeing increasingly interested in \u003ca href = \"https://en.wikipedia.org/wiki/Xiangqi\"\u003eXiangqi\u003c/a\u003e (a.k.a., \u003cb\u003eChinese Chess\u003c/b\u003e), Mr. Cody has designed a new Xiangqi match for \u003ca href = \"https://en.wikipedia.org/wiki/Xiang_Yu\"\u003eXiang Yu\u003c/a\u003e and \u003ca href = \"https://de.wikipedia.org/wiki/Han_Gaozu\"\u003eLiu Bang\u003c/a\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\u003c/p\u003e\u003cp\u003eOnce\u003c/p\u003e\u003cpre\u003e   1) Xiang Yu wins Na games consecutively,\r\n   2) Liu Bang wins Nb games consecutively, \r\n   3) No ties occur consecutively, \u003c/pre\u003e\u003cp\u003e\u003cb\u003ewhichever comes first\u003c/b\u003e, Mr. Cody announces the outcome accordingly as follows:\u003c/p\u003e\u003cpre\u003e   1) Xiang Yu is the final winner,\r\n   2) Liu Bang is the final winner, \r\n   3) They end up with a final draw.\u003c/pre\u003e\u003cp\u003eAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\u003c/p\u003e\u003cpre\u003e                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\u003c/pre\u003e\u003cp\u003ewhere\u003c/p\u003e\u003cul\u003e\u003cli\u003ea: the probability that Xiang Yu wins one individual game\u003c/li\u003e\u003cli\u003eb: the probability that Liu Bang wins one individual game\u003c/li\u003e\u003cli\u003eNa: # of consecutive wins required for Xiang Yu to become the final winner\u003c/li\u003e\u003cli\u003eNb: # of consecutive wins required for Liu Bang to become the final winner\u003c/li\u003e\u003cli\u003eNc: # of consecutive ties required to result in a final draw\u003c/li\u003e\u003cli\u003ePa: the probability that Xiang Yu wins the match\u003c/li\u003e\u003cli\u003ePb: the probability that Liu Bang wins the match\u003c/li\u003e\u003cli\u003ePc: the probability of a final draw\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eThe main focus of this problem is on \u003cb\u003eMonte Carlo simulations\u003c/b\u003e, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\u003c/p\u003e\u003cp\u003e1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u0026lt; tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected.\u003c/p\u003e\u003cp\u003e2) Check if your solution is based on \u003cb\u003epure Monte Carlo simulations\u003c/b\u003e or \u003cb\u003eanalytical approaches\u003c/b\u003e. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations.\u003c/p\u003e\u003cp\u003e3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get.\u003c/p\u003e\u003cp\u003eIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks.\u003c/p\u003e","function_template":"function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)\r\n% a: the probability that Xiang Yu wins one individual game\r\n% b: the probability that Liu Bang wins one individual game\r\n% Na: # of consecutive wins required for Xiang Yu to become the final winner\r\n% Nb: # of consecutive wins required for Liu Bang to become the final winner\r\n% Nc: # of consecutive ties required to result in a final draw\r\n% Pa: the probability that Xiang Yu wins the match\r\n% Pb: the probability that Liu Bang wins the match\r\n% Pc: the probability of a final draw\r\n    Pa = ;\r\n    Pb = ;\r\n    Pc = ;\r\nend","test_suite":"%%\r\n% Thanks to Alfonso Nieto-Castanon\r\nurlwrite('https://sites.google.com/a/alfnie.com/alfnie/software/SetSolutionScore.p?attredirects=0\u0026amp;d=1','SetSolutionScore.p');\r\nrehash path;\r\n\r\n%%\r\nfh = fopen('EvaluateSolution.p','wb');\r\nfwrite(fh, hex2dec(reshape('7630312E30307630302E3030000C701C97F61FB1000002D5000001CF000004E73DD68930A391F7C60A534B45A03EAF72EB08941F39EE01BF25BAE04DF43CF342FC1A763DF6B8F26BBED0BD4F2ABBB5927B1EEBAE8795E487F6E4EF2737CBB6646BC4DF145E14664B3A4DACCD7CB01C4EC2328AD76F196231D2CA02CDC2B15466FBA5BDDF9E6C0E5DE12CF07B2AAA50BD2F04FFB92E9BECBE232E01031340A8EDCA5C10DAC01BBE43685ED0AB79D9C6F2A090FB4E0E75CAA236D3D73AD659E0705C42792BC77D85951B2FC49DE856FB97AFD74C1DB66C874EDF5517BCFA14C6706CA5E61DE60F2771B64F6D634B858A1A30AD7C49778534CCCE7551C637DC53846B02140046F729C5EB2DC9C65F16C2FC4F34EA9F03A2056B8218ACAB9A9A8BC5DC8F2F3312740F86626ABA38E00903CE76846DEE175BEE04DC0815E050E4CD95BA8E5BD27A0B57F2413B71A0E4837FB0F86328AA82732C584F1F55C6CCD79CBC69D052011BA93357AAE3568E0086F159C083D665645A38584955283925F900254A6562F0C755323C40805328D04F27FD863E8B774B52E27FC3AA30CE689AC57DEFEE274DBBC2A4D9D320CF19AD873AA0AE806721EE78496F7ADA99EA48060F26FDEE7ED5E9F85B27F79AE176A6E8EAC4DD5127299122FF88139BC4B56A2ED3C5338A72676C4C99AD6DEBCD8BB5EB09',2,[]).')); rehash path; fclose(fh);\r\n\r\n%%\r\nfid = fopen('Xiangqi2.m');\r\ndelim = {' ', '\\n', ',', '.', ';', '''', '@', '+', '-', '*', '/', '\\', '^', '\u003e', '\u003c', '=', '\u0026', '|', '~', '{', '}', '[', ']', '(', ')'};\r\nfile = textscan(fid, '%s', 'CommentStyle', '%', 'MultipleDelimsAsOne', 1, 'Delimiter', delim); fclose(fid); \r\nassert(~any(ismember({'rng','RandStream','seed','state','twister','shufle','default'},file{1})));\r\n\r\n%%\r\na = 0; b = 0; Na = 2; Nb = 3; Nc = 2; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 0; b = 1; Na = 1; Nb = 2; Nc = 1; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 1; b = 0; Na = 3; Nb = 2; Nc = 1; tol = 1e-6;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol); \r\n\r\n%%\r\na = 0.15; b = 0.85; Na = 4; Nb = 2; Nc = 1; tol = 1e-4;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\na = 0.9; b = 0; Na = 3; Nb = 1; Nc = 2; tol = 1e-3;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\na = 0.65; b = 0.3; Na = 3; Nb = 2; Nc = 2; tol = 1e-3;\r\nEvaluateSolution(a, b, Na, Nb, Nc, tol);\r\n\r\n%%\r\nNa = 3; Nb = 2; Nc = 1; tol = 2e-3; \r\np = sort(rand(2,30)); \r\np = sort([p(1,:);diff(p);1-p(2,:)]);\r\nfor k = size(p,2):-1:1\r\n    a = p(3,k); b = p(2,k);\r\n    score(k) = EvaluateSolution(a, b, Na, Nb, Nc, tol);    \r\nend\r\nSetSolutionScore(round(mean(score)));","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":12569,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":5,"test_suite_updated_at":"2015-11-12T00:41:35.000Z","rescore_all_solutions":true,"group_id":1,"created_at":"2015-11-08T20:51:55.000Z","updated_at":"2015-11-12T03:39:15.000Z","published_at":"2015-11-10T00:22:37.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThis is the second part of the Xiangqi series. The first part in this series is:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://www.mathworks.com/matlabcentral/cody/problems/42674-cody-meets-xiangqi-foresee-the-unseen\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eCody meets Xiangqi: foresee the unseen (Part 1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBeing increasingly interested in\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiangqi\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiangqi\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e (a.k.a.,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eChinese Chess\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e), Mr. Cody has designed a new Xiangqi match for\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Xiang_Yu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eXiang Yu\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e and\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://de.wikipedia.org/wiki/Han_Gaozu\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eLiu Bang\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e by taking into account the likelihood of tie games. The new rule is described as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eOnce\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   1) Xiang Yu wins Na games consecutively,\\n   2) Liu Bang wins Nb games consecutively, \\n   3) No ties occur consecutively,]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ewhichever comes first\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, Mr. Cody announces the outcome accordingly as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[   1) Xiang Yu is the final winner,\\n   2) Liu Bang is the final winner, \\n   3) They end up with a final draw.]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAgain, Cody asks us --- active Cody players --- to foresee the outcome of this unseen match using Monte Carlo simulations. Our task is to write the following function\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[                         [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc)]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ewhere\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ea: the probability that Xiang Yu wins one individual game\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb: the probability that Liu Bang wins one individual game\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNa: # of consecutive wins required for Xiang Yu to become the final winner\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNb: # of consecutive wins required for Liu Bang to become the final winner\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eNc: # of consecutive ties required to result in a final draw\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePa: the probability that Xiang Yu wins the match\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePb: the probability that Liu Bang wins the match\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePc: the probability of a final draw\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe main focus of this problem is on\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMonte Carlo simulations\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, rather than analytical approaches. Your provided solution Xiangqi2.m will be checked by a P-file EvaluateSolution.p, which mainly does 3 things as follows:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1) Call your function [Pa, Pb, Pc] = Xiangqi2(a, b, Na, Nb, Nc) and then Check if the result P = [Pa, Pb, Pc] is within tolerance of its expected value Q. That is, If norm(P - Q) \u0026lt; tol holds, it means that your solution is accurate enough. If this does not hold, your solution will be rejected.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e2) Check if your solution is based on\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003epure Monte Carlo simulations\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e or\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eanalytical approaches\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. If it is based on analytical approaches (i.e., using analytical expressions to directly compute the probabilities), then your solution will be rejected. EvaluateSolution.p accomplishes this goal by exploiting a combination of distinct features possessed by analytical solutions, but are generally not shared by Monte Carlo simulations.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e3) If your solution passes the above two checks, then the score of your solution will be determined based on the speed of your code. The faster your solution is, the smaller score you get.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eIf you have any concerns or suggestions on this problem, please feel free to leave me a comment. Thanks.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"xiangqi\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"xiangqi\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"xiangqi\"","","\"","xiangqi","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f534b01c458\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f534b01c3b8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f534b01b378\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f534b01ca98\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f534b01c8b8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f534b01c818\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f534b01c598\u003e":"tag:\"xiangqi\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f534b01c598\u003e":"tag:\"xiangqi\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"xiangqi\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"xiangqi\"","","\"","xiangqi","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f534b01c458\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f534b01c3b8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f534b01b378\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f534b01ca98\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f534b01c8b8\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f534b01c818\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f534b01c598\u003e":"tag:\"xiangqi\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f534b01c598\u003e":"tag:\"xiangqi\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":42674,"difficulty_rating":"medium"},{"id":42685,"difficulty_rating":"unrated"}]}}