Loading ...
Sorry, an error occurred while loading the content.

question of random-n-of

Expand Messages
  • netlogojj
    Does random-n-of allow repetition?
    Message 1 of 6 , Oct 1, 2005
    • 0 Attachment
      Does random-n-of allow repetition?
    • Josh Unterman
      nope. [netlogojj ]
      Message 2 of 6 , Oct 1, 2005
      • 0 Attachment
        nope.


        [netlogojj <netlogojj@...>]
        > Does random-n-of allow repetition?
        >
        >
        >
        >
        >
        >
        >
        > Yahoo! Groups Links
        >
        >
        >
        >
        >
        >
      • Taagelsir Gasmelseid
        Dear all list members: In the attached code I have been trying to plot three charts: agent population, water-budget and cost-return. When I plotted agent
        Message 3 of 6 , Oct 1, 2005
        • 0 Attachment

          Dear all list members:

          In the attached code I have been trying to plot three charts: agent population, water-budget and cost-return. When I plotted agent population alone it worked well but when I added the other two and included some procedures to compute results (costs, requirements, return etc) I got the following message:

           

          error while observer executing SET-CURRENT-PLOT in procedure DO-PLOT

          called by procedure GO-SUPPLIER

          called by procedure GO

          no such plot: "water-budget"

          (halted execution of go)

           

          I changed the plot in the setup to include each plot in a separate definition but got the same message. I want first to make sure that the procedures used for making computations and updating water-invent are true and how to fix the charts. I assigned the variables to be computed an initial value equal to zero.

          Please reply to tagelsir.gasmelseid@...

          ; add model procedures here

          globals [

          ticks;; to count for time frames.

          farmers;; the number of farmers in the model.

          suppliers;; the number of suppliers in the model.

          water-req;; water requirements estimated by a speficif farmer in accordance with a water-use scenario or cropping plan.

          areact;; farm area cultivated in cotton.

          areawt;; farm area cultivated in wheat.

          areasg;; farm area cultivated in sorghum.

          areagn;; farm area cultivated in groundnuts.

          water-reqct;;

          water-reqwt;;

          water-reqsg;;

          water-reqgn;;

          water-allocated;; maximum water threshold a supplier agent can allocate for a farmer agent against his water requirements declared.

          water-invent;; water inventory maintained by the supplier agent.

          min-water-req;; least crop water requitements to be used for the computation of min-water.

          max-water-req;; most crop water requitements to be used for the computation of max-water.

          cost;; costs associated with a specific water-use scenario or crop protfolio.

          fixed-cost;;

          varcost-ct;;

          varcost-sg;;

          vrcost-wt;;

          varcost-qgn;;

          return;; returns associated with a specific water-use scenario or crop protfolio.

          return-sg;;

          return-gn;;

          return-wt;;

          return-ct;;

          rate;;rate used by the supplier agent to compute max-water.

          ]

           

          breeds [ farmer supplier ]

          turtles-own [ energy ]

          patches-own [ countdown ]

           

          to setup

          ca

          setup-constants

          setup-patches

          setup-farmer

          setup-supplier

          display-labels

          plot count turtles

          end

           

          to setup-constants

          set ticks 0

          set water-req 0

          set areact 5

          set areawt 5

          set areagn 5

          set areasg 5 

          set water-allocated 0 

          set water-invent 70000

          set cost 0

          set return 0

          set rate 0.8

          set water-reqct 150

          set water-reqwt 160

          set water-reqgn 170

          set water-reqsg 150

          end

           

          to setup-patches 

          ask patches [ set  pcolor green ]

          if water? [ ;;ask if the water switcch is on if true then farmer can irrigate if not he cant

          ask patches [

                       set countdown random water-recharge-time ;;initilaize the recharge clock

                       if (random 2) = 0 ;;half the patches start out with water

                       [ set pcolor brown ]

          ]

          ]

          end

           

          to setup-farmer

          set-default-shape turtles "person farmer"

          create-custom-farmer population-farmer ;; create the farmer then intialize their values

          [

                set color white

                set label-color blue - 2

                set energy random (2 * farmer-gain-from-wateruse) ;;a measure of overall farmer satisfaction with a particular water use scenario

                setxy random-float screen-size-x

                random-float screen-size-y

                ]

                end

               

          to setup-supplier

                set-default-shape turtles "person"

                create-custom-supplier population-supplier ;;create the supplier then intialize their values

                [

                set color black

                set energy random (2 * water-allocated)

                setxy random-float screen-size-x

                      random-float screen-size-y

                      ]

                      display-labels

                     

                      plot count turtles

          end

           

          to go

             go-farmer

             go-supplier

             end

           

          to go-farmer

                 ask farmer [

                 move

                    if water? [

                       set energy energy - 1 ;;deduct energy for farmer only if water? switch is on

                       farmer-planning

                       ]

                 use-water

                 reproduce-farmer

                 death

          ]

          end

           

          to move ;;turtles procedure

                rt random-float 50 - random-float 50

                fd 1

                end

           

          to farmer-planning;; to make computations

          set water-req ((areact * water-reqct) + (areasg * water-reqsg) + (areawt * water-reqwt) + (areagn * water-reqgn))

          set cost (fixed-cost + ((areact * varcost-ct) + (areasg * varcost-sg) + (areawt * vrcost-wt) + (areagn * varcost-qgn) ))

          set return ((return-sg + return-gn + return-wt + return-ct) - cost )

          end

           

          to use-water ;;farmer procedure

                       ;; farmers use water and turn the patch brown

                       if pcolor = green [

                       set pcolor brown

                       set energy energy + farmer-gain-from-wateruse ;;farmer gain energy by irrigating

                       ]

                       end

                      

          to reproduce-farmer ;;

                 if random-float 100 < farmer-reproduce [

                 set energy (energy / 2)

                 hatch 1 [ rt random-float 360 fd 1 ]

          ]

          end

           

          to death ;;turtle procedure when energy and also water goes below zero

          if energy < 0 [ die ]

          end           

           

          to go-supplier

                ask supplier [

                moves

                      set energy energy - 1 ;;supplier loses energy as they allocate water i.e. move

                      reproduce-supplier    ;;if at any time one canal exhausted its water available the farmer can use the other

                                          ;;one which becomes automatically active

                      supplier-decision

                      death                 ;;a canal is closed if it is empty or no farmer is in need for water

               ]

                 if water? [ ask patches [ update-turtles ] ]

                      do-plot  ;;plot populations

                         every 0.5

                           [ display-labels ]

                         set ticks ticks + 1

                           if not any? turtles [ stop ]

          end

             

          to moves ;;turtles procedure

                rt random-float 50 - random-float 50

                fd 1

                end

           

          to reproduce-supplier ;;

                if random-float 100 < supplier-reproduce [

                set energy (energy / 2)

                hatch 1 [ rt random-float 360 fd 1 ]

          ]

          end

           

          to supplier-decision

                   if (water-invent <= 0) [ stop ]  ;;to stop if water inventory is equal to or less than zero

                   if (water-req > water-allocated)

                          [stop]

                        allocate-water

                        update-water

           end

           

          to allocate-water

                 if (water-req = 0) [stop ]

                 set water-allocated ((areact * water-reqct) + (areasg * water-reqsg) + (areawt * water-reqwt) + (areagn * water-reqgn)) * rate

                 update-water

          end

           

          to update-turtles ;;patch procedure

                       ;;countdown on brown patches, if reach 0 irrigate

                       if pcolor = brown [

                       ifelse countdown <= 0

                           [ set pcolor green

                                 set countdown water-recharge-time ]

                                   [ set countdown (countdown - 1) ]

                                       ]

                       plot count turtles

          end

           

          to update-water

                     set water-invent water-invent - water-allocated

          end

                      

          to display-labels

          ifelse show-energy?

          [

          ifelse water?

          [

          ask turtles

           [ set label round energy ]

           ]

           [

           ask supplier

           [ set label round energy ]

           ask farmer

           [ set label no-label ]

           ]

           ]

           [

           ask turtles

           [ set label no-label ]

           ]

           end

                      

          to do-plot

          set-current-plot "pop"

          set-current-plot-pen "farmer"

          plot count farmer

          set-current-plot-pen "supplier"

          plot count supplier

           

          set-current-plot "water-budget"

          set-current-plot-pen "water-req"

          plot water-req

          set-current-plot-pen "water-allocated"

          plot water-allocated

           

          set-current-plot "cost-benefit"

          set-current-plot-pen "cost"

          plot cost

          set-current-plot-pen "return"

          plot return

          end



           


          Yahoo! for Good
          Click here to donate to the Hurricane Katrina relief effort.
        • James Steiner
          ... No. Random-n-of selects N unique items from a set or list that has N or more members. The result therefore contains no duplicates. This would be especially
          Message 4 of 6 , Oct 1, 2005
          • 0 Attachment
            On 10/1/05, netlogojj <netlogojj@...> wrote:
            > Does random-n-of allow repetition?

            No.

            Random-n-of selects N unique items from a set or list that has N or
            more members.

            The result therefore contains no duplicates. This would be especially
            true when using random-n-of with an agentset, as agentsets can never
            contain duplicates.

            If you wish to make a list that contains N randomly selected items
            from a list, such that the same item may be selected more than once,
            you could do something like this:

            to-report non-unique-random-n [ n a-list ]
            report n-values n [ random-one-of a-list ]
            end

            Hope this helps!
            ~~James
          • James Steiner
            ... That error occurs when NetLogo can not find a plot on the interface whose name matches the name you provided to set-current-plot . So, go to your
            Message 5 of 6 , Oct 1, 2005
            • 0 Attachment
              On 10/1/05, Taagelsir Gasmelseid <tagelsir2002@...> wrote:

              > ....
              > I got the following message:
              > error while observer executing SET-CURRENT-PLOT in procedure DO-PLOT
              > called by procedure GO-SUPPLIER
              > called by procedure GO
              > no such plot: "water-budget"
              > (halted execution of go)
              > ....

              That error occurs when NetLogo can not find a plot on the interface
              whose name matches the name you provided to "set-current-plot". So,
              go to your interface pane, and make sure the names of your plot
              widgets (that is, the contents of the Name field for the plot) exactly
              match the names you are using in the code. By default, NetLogo names
              them "plot1", "plot2", and so on.

              Hope this helps,

              ~~James
            • James Steiner
              ... Tag, The file you sent contains a run-time error, unrelated to the plots or the monitors: Only the observer can ask the set of all turtles . This occurs
              Message 6 of 6 , Oct 2, 2005
              • 0 Attachment
                On 10/2/05, Tagelsir Gasmelseid <tagelsir.gasmelseid@...> wrote:
                > thanks for the reply. I did the change. But it seems that the impact of the
                > procedures used for making calculations and updating costs, returns and
                > water did not work. Are they? also the two monitors are also not working.
                > I am atatching to you the file for easy visualization.

                Tag,

                The file you sent contains a run-time error, unrelated to the plots or
                the monitors: "Only the observer can ask the set of all turtles".

                This occurs because your display-labels procedure contains an "ask
                turtles". It also asks several subgroups of turtles, too. So, this
                procedure should only ever get called by the observer. However, you
                use this procedure several times in your code, not just one time in go
                (where it should be).

                Once I altered that line so it would run, the plots updated.

                However, fixing the ask turtles is not the final solution... since you
                have individual turtles calling "display-labels", these individual
                turtles are not just updating their own labels, but causing all the
                turtles to update their lables... so any one turtle may be caused to
                update it's labels hundreds of times, every loop! So, The real
                soultion is to remove "display-labels" from all the assorted places
                that it appears, making sure it runs only once per loop.

                Now, the monitors: I see you are intending to store the counts of the
                two breeds, famer and supplier, in global variables called farmers and
                suppliers. It is these variables that appear in the counters. This
                would work, except that you never actually put the count of farmer
                into the farmers variable. Likewise suppliers. So they remain zero.
                You either need to put "set farmers count famer" in your GO, or
                abandon the global variable, and simply put "count farmer" in the
                monitor. Likewise for the suppliers.

                As a side note, you may wish to draw a diagram, such as a flow-chart,
                of how your model is *supposed* to work. That is, what happens first,
                then second, and so on. This may help you find places in your code
                where you are repeating actions, or doing things do many times, or
                where it is not necessary, or where you are *not* doing things that
                you actually want to happen..

                Hope this helps,
                ~~James
              Your message has been successfully submitted and would be delivered to recipients shortly.