{"id":7771,"date":"2024-09-27T11:36:56","date_gmt":"2024-09-27T09:36:56","guid":{"rendered":"https:\/\/oopm.org\/?page_id=7771"},"modified":"2024-12-13T11:30:35","modified_gmt":"2024-12-13T10:30:35","slug":"12-2-lotto-example-sketch-new","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=7771","title":{"rendered":"13.2 Lotto example sketch"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7771&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages7771&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">The next example is an experiment on playing Lotto. In this simplified version of Lotto you have to guess seven different numbers in the interval from 1 to 34. You may submit one bet each week. At the end of the week the Lotto system chooses randomly seven different winner numbers, and the winning players are those that have submitted a bet with these winner numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We use a <code>MonitorSystem<\/code> to represent the players and the Lotto. Lotto is represented by an object with <code>MonitorProcess<\/code> as superclass. A player is represented by an instance of class <code>Player<\/code> that is a subclass of <code>MonitorProcess<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Lotto<\/code> system has the following overall structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LottoExperiment: <strong>obj<\/strong> MonitorSystem\n   mnoOfPlayers: <strong>val<\/strong> 500\n   betSize: <strong>val<\/strong> 7\n   <strong>class<\/strong> Hand:\n      :::\n   <strong>class<\/strong> Bet(thePlayer: <strong>ref<\/strong> Player, theHand: <strong>ref<\/strong> Hand):\n      :::\n   <strong>class<\/strong> Player(inx: <strong>var<\/strong> integer): MonitorProcess\n      :::\n   Lotto: <strong>obj<\/strong> MonitorProcess\n      :::\n   Lotto.start\n   generatePlayers: <strong>do<\/strong>\n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LottoExperiment<\/code> has the following main attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Constants <code>noOfPlayers<\/code> and <code>betSize<\/code>, which defines the number of <code>Player<\/code> objects and the size of a <code>Hand<\/code> in a <code>Bet<\/code>.<\/li>\n\n\n\n<li>Class <code>Hand<\/code> which represents a hand of seven numbers.<\/li>\n\n\n\n<li>Class <code>Bet<\/code> which represents a bet. It has parameters <code>thePlayer<\/code>, which is the <code>Player<\/code> that has submitted the <code>Bet<\/code> and <code>theHand<\/code>, which is the <code>Hand<\/code> of the <code>Bet<\/code>.<\/li>\n\n\n\n<li>Class <code>Player<\/code> which represents a player.<\/li>\n\n\n\n<li>An object <code>Lotto<\/code>, which represents the Lotto.<\/li>\n\n\n\n<li>A statement that starts the <code>Lotto<\/code> process.<\/li>\n\n\n\n<li>An object <code>generatePlayers<\/code> which is an object that generates the players.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Player<\/code> has the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> Player: MonitorProcess  \n      cycle\n         Lotto.submit(Bet(this(Player),Hand)\n         doSomethingElse<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <code>Player<\/code> repeatedly submits a <code>Bet<\/code> by invoking <code>Lotto.submit<\/code>. <\/li>\n\n\n\n<li>The argument of <code>submit<\/code> is the object <code>Bet(this(Player), Hand)<\/code>.<\/li>\n\n\n\n<li>The first argument of <code>Bet<\/code> is a reference to the <code>Player<\/code> submitting the <code>Bet<\/code>.<\/li>\n\n\n\n<li>The second argument of <code>Bet<\/code> is a <code>Hand<\/code> which randomly generates 7 numbers.<\/li>\n\n\n\n<li>After submitting a <code>Bet<\/code>, it executes <code>doSomethingElse<\/code> before playing the next time; <code>doSomethingElse<\/code> is not specified here.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The Lotto is represented by an object&nbsp;<code>Lotto<\/code>. This object keeps the bets being submitted, it has the winning bet, and the deadline for submitting bets. As players in parallel submit bets by calling the method&nbsp;<code>submit<\/code>, the&nbsp;<code>Lotto<\/code>&nbsp;object is defined as a <code>MonitorProcess<\/code> with&nbsp;<code><code>submit<\/code><\/code>&nbsp;as an entry method &#8212; similar to en <code>entry<\/code>-method of a <code>Monitor<\/code> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The structure of <code>Lotto<\/code> is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   Lotto: <strong>obj<\/strong> MonitorProcess\n      submit(B<strong>:<\/strong> <strong>ref<\/strong> Bet): entry\n         bets.insert(B)\n      clearBets:\n         bets.clear\t \n      findWinningBets:\n         :::\n      bets: <strong>obj<\/strong> Set(#Bet)\n      :::<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Lotto<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An <code>entry<\/code>-method <code>submit<\/code>, which may be used by <code>Player<\/code> objects to <code>submit<\/code> bets. As mentioned, it works in the same way as an <code>entry<\/code> method of a <code>Monitor<\/code>. This means that at most one <code>submit<\/code> method may be executed at a given time.<\/li>\n\n\n\n<li>The <code>Bets<\/code> being submitted are stored in <code>bets<\/code> which is a <code>Set<\/code>.<\/li>\n\n\n\n<li>Methods <code>clearBets<\/code> and <code>findWinningBets<\/code> that are private to <code>Lotto<\/code> in the sense that they may not be invoked by objects (here <code>Players<\/code>) outside <code>Lotto<\/code>. As for <code>Monitor<\/code>, only methods being submethods of <code>entry<\/code> may be invoked from outside <code>Lotto<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">As said, a Lotto-period is a week. During a week the <code>Players<\/code> may submit bets. At the end of a week, Lotto stops accepting <code>Bets<\/code> via <code>submit<\/code>.  It then invokes <code>findWinningBets<\/code> to find the possible winners of the week. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When possible winners have been found, <code>Lotto<\/code> informs the winners. It invokes <code>clearBets<\/code> to remove all elements from <code>bets<\/code> and open up for a new round of submissions. This is represented by the following  statements of <code>Lotto<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Lotto: <strong>obj<\/strong> MonitorProcess\n   -\"-\n  run: <strong>do<\/strong>\n      waitAndAccept(aWeek)\n      findWinningBets\n      clearBets\n      restart(run)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The object <code>run<\/code> is executed forever:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When a <code>MonitorProcess<\/code> (here <code>Lotto<\/code>) is executing, it is not possible to execute an <code>entry<\/code>-method from outside.<\/li>\n\n\n\n<li>The method <code>waitAndAccept<\/code> is defined as an attribute of <code>MonitorProcess<\/code>.<\/li>\n\n\n\n<li>When <code>waitAndAccept(aWeek)<\/code>, the <code>MonitorProcess<\/code> (<code>Lotto<\/code>) waits for a period &#8211; here defined by <code>aWeek<\/code> and while waiting it accepts <code>entry<\/code>-methods like <code>submit<\/code>.<\/li>\n\n\n\n<li>When <code>Lotto<\/code> has waited for a week, <code>waitAndAccept<\/code> resumes execution and closes for execution of <code>entry<\/code>-methods like <code>submit<\/code>.<\/li>\n\n\n\n<li>It then executes <code>findWinningBets<\/code> followed by <code>clearBets<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>Lotto<\/code> executes <code>waitAndAccept<\/code>, there may be <code>Players<\/code> that have invoked <code>Lotto.submit<\/code>. These invocations may be executed before <code>waitAndAccept<\/code> closes for execution of <code>entry<\/code>-methods. This means that a <code>Player<\/code> may submit a <code>Bet<\/code> after a week has passed. From a modeling point-of-view this may be ok since the invocation of <code>submit<\/code> has happened before a week has passed. If one does not consider this to be ok, the implementation of <code>waitAndSubmit<\/code> has to be able to block <code>Players<\/code> waiting to execute a <code>submit<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We now show the details of <code>findWinningBets<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      findWinningBets:     \n         winningBet := Bet(Player(\"Winner\"), Hand)\n         bets.scan\n             if (current.equal(winningBet)) :then\n                \"Winner: \".print\n                current.print\t\t\n         newline<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As mentioned, it starts by assigning <code>possibleToSubmit<\/code> to false.<\/li>\n\n\n\n<li>It then generates a <code>winningBet<\/code> with a hypothetical <code>Player<\/code> called &#8220;Winner&#8221; and a <code>Hand<\/code> with seven random numbers as arguments<\/li>\n\n\n\n<li>It then scans the submitted bets in <code>bets<\/code> and checks whether or not the current <code>Bet<\/code> is equal to the <code>winningBet<\/code><\/li>\n\n\n\n<li>If a winner is found, the <code>Player<\/code> of this <code>Bet<\/code> is printed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Finally we show the structure of class <code>Hand<\/code> and class <code>Bet<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> Hand:\n      numbers: <strong>obj<\/strong> Array(betSize,#integer)\n      for (1):to(betSize):repeat\n           numbers.put(random(1,34)):at&#91;inx]\n\n   <strong>class<\/strong> Bet(thePlayer: <strong>ref<\/strong> Player, theHand: <strong>ref<\/strong> Hand):\n      equal(aBet <strong>ref<\/strong> Bet) -&gt; B: var boolean:\n         ...<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Class <code>Hand<\/code> has an array <code>numbers<\/code> that contains the seven random numbers of the <code>Hand<\/code>. <\/li>\n\n\n\n<li>It uses a <code>for:to:repeat<\/code> to generate and store seven random numbers in <code>numbers<\/code>.<\/li>\n\n\n\n<li>Class <code>Bet<\/code> has an <code>equal<\/code> method in addition to its two parameters.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For completeness, all the code for the Lotto example is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>LottoExperiment: obj MonitorSystem\n   mnoOfPlayers: val 500\n   betSize: val 7\n   class Hand:\n      numbers: obj Array(betSize,#integer)\n      for (1):to(betSize):repeat\n           numbers.put(random(1,34)):at&#91;inx]\n   class Bet(thePlayer: ref Player, theHand: ref Hand):\n      equal(aBet ref Bet) -> B: var boolean:\n         ...\n   class Player(inx: var integer): MonitorProcess\n       cycle\n          Lotto.submit(Bet(this(Player),Hand)\n          doSomethingElse\n   Lotto: obj MonitorProcess\n      submit(B: ref Bet): entry\n         bets.insert(B)\n      clearBets:\n         bets.clear\t \n      findWinningBets:\n         winningBet := Bet(Player(\"Winner\"), Hand)\n            bets.scan\n              if (current.equal(winningBet)) :then\n                 \"Winner: \".print\n                 current.print\t\t\n              newline\n      bets: obj Set(#Bet)\n      run: <strong>do<\/strong>\n         waitAndAccept(aWeek)\n         findWinningBets\n         clearBets\n         restart(run)\n   Lotto.start\n   generatePlayers: do\n      ...<\/code><\/pre>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=7771\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>The next example is an experiment on playing Lotto. In this simplified version of Lotto you have to guess seven different numbers in the interval from 1 to 34. You may submit one bet each week. At the end of the week the Lotto system chooses randomly seven different winner numbers, and the winning players [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1669,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-7771","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7771"}],"version-history":[{"count":37,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7771\/revisions"}],"predecessor-version":[{"id":10323,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/7771\/revisions\/10323"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1669"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}