Oligopoly Put Quantity

No preview image

1 collaborator

Default-person Steven Kimbrough (Author)

Tags

"probe and adjust"R 

Tagged by Steven Kimbrough over 9 years ago

"probe and adjust"z 

Tagged by Steven Kimbrough over 9 years ago

cournot 

Tagged by Steven Kimbrough over 9 years ago

markets 

Tagged by Steven Kimbrough over 9 years ago

oligopoly 

Tagged by Steven Kimbrough over 9 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.3 • Viewed 626 times • Downloaded 59 times • Run 0 times
Download the 'Oligopoly Put Quantity' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


$Id: OligopolyPutQuantity.nlogo 4089 2014-03-20 21:01:52Z sok $

NOTE WELL

I've added some features and the documentation is now incomplete.

WHAT IS IT?

This NetLogo program, oligopolyBidQuantity.nlogo, models the following situation. A market for a product exists with a linear price function. A number of firms, N, supply the market by offering quantities, Q(i) for firm i, during each period (i = 0, 1, ..., (N-1)). The market price, P, for a unit of the product in a given period is a linear function of the total quantity, Q, of the product on offer: P(Q) = P = priceIntercept - dSlope*Q (priceIntercept > 0, dSlope > 0). Thus, as Q, the total quantity on offer, increases, the market's unit price for the product, P, decreases. (Until it reaches 0. We assume that P >= 0.)

The market properates discretely by periods. During each period, the firms supplying the market bid quantities, Q(i), that they wish to offer for that period. We assume that the firms only bid quantities that they are able to supply and that all bids are in fact realized. At the end of each period, Q is determined as the sum of the Q(i)s and the market price is determined as

 P = max(0, priceIntercept - dSlope*Q).  

(This is set in the procedure PriceGivenQuantity.)
Each firm, i, then receives revenue = P*Q(i), and the period ends. If a stopping condition has been reached, the run is over; otherwise the market continues with the start of a new period.

HOW IT WORKS

The firms supplying the market and offering quantity bids are the only agents in this model. At the end of each time period, each firm, i, observes the the realized price, P, for that period. The firm calculates its reward (profit, �(i)) for the period as

 reward = �(i) = revenue - costs 
 =  P*Q(i) - unitProductionCost(i)*Q(i)
 = (P - unitProductionCost(i))*Q(i)

(So we assume that each firm, i, has its own production costs, which are proportional to the amount supplied, Q(i).) Each firm records its reward for the period as follows. If the firm's bid in the period, Q(i), was greater than or equal to its currentBaseValue, its anchor bid value, then the reward received is recorded in the firm's record of upRewards; otherwise the reward is recorded in the firm's record of downRewards.

Further, each firm, i, calculates the total reward (net of costs) for ALL the firms supplying the market in the current period. This is called marketTotalReward. As before, if the firm's bid in the period, Q(i), was greater than or equal to its currentBaseValue, then the marketTotalReward is recorded in the firm's record of marketUpRewards; otherwise the marketTotalReward is recorded in the firm's record of marketDownRewards.

Note well: recording in this way the marketTotalReward values assumes that the individual firms have access to the bids and the costs of all the firms in the market.

The following procedure is used by each agent to record this information as described:

to Observe-and-Record [price]
  if (is-pAndA? self) [
    let reward (price * quantityBid) - (unitProductionCost * quantityBid)
    set totalReward totalReward + reward
    ifelse (quantityBid >= currentBaseValue)
      [set upRewards lput reward upRewards]
      [set downRewards lput reward downRewards]
    ifelse (quantityBid >= currentBaseValue)
      [set marketUpRewards lput marketTotalReward marketUpRewards]
      [set marketDownRewards lput marketTotalReward marketDownRewards]
   ] ; end of if is-pAndA?
 end ; of observe-and-record

Each firm uses one of a number of policies in formutating its bids. In a given period, a firm's bid of a quantity, Q(i), is determined as follows. The firm has a currentBaseValue for its bid. The actual bid it produces is drawn uniformly in the range from (currentBaseValue - delta(i)) to (currentBaseValue + delta(i)).

Each firm adjusts its currentBaseValue after a certain number of periods, called an epoch. The number of periods in a firm's epoch is called its epochLength. Each firm may have its own epochLength. If at the end of a period a firm's current epoch reaches its epochLength, then the firm uses on of three available policies to update its currentBaseValue. Once it does this, it forgets its statistics, recorded as above, and it starts a new epoch.

The policies available are as follows.

1) Simple Probe-and-Adjust ("Own Returns")

Each simple probe-and-adjust agent is characterized by four parameters---currentQbase (i.e., currantBaseValue), delta, epsilon, and epochLength---plus initialQbase, the initial value for currentQbase. Typical values would be: currentQbase, 50; delta, 3; epsilon, 1; epochLength, 30. At the beginning of a run, the agent's currentQbase is intialized with its value of initialQbase. The agent's bid is uniformly drawn from [currentQbase - delta, currentQbase + delta]. When a period is over the agent receives revenue = PQ(i) and profit (reward) of (P - unitProductionCost(i))Q(i). The agent records this profit value in one of two lists. If the agent bid a quantity, Q(i) >= currentQbase, then the agent records reward, �(i), in upperReturn; otherwise the �(i) is recorded in lowerReturn. Each agent keeps track of its own epochs, which consist of market periods of lengh epochLength. (These are all agent-specific values.) At the end of an epoch, an agent may adjust its value of currentQbase. Examining upperReturn and lowerReturn, the agent will increase currentQbase by epsilon if on average it gets a higher return in upperReturn than in lowerReturn; and conversely the agent will reduce currentQbase by epsilon if lowerReturn gives a higher on-average return. After making any required adjustment to currentQbase, the agent resets its epocCounter, upRewards, and downRewards to 0 and is ready for the next period.

When both agents use this policy, they converge in quantity offered and the sum of the quantities offered converges to the Cournot solution (but meanders stochastically).

    if (episodeCount >= epochLength) ; then update and reset
      [
       if (updateType = "Own Returns") [
       ifelse (upRewards = [])
         [set meanUp 0]
         [set meanUp mean upRewards]
       ifelse (downRewards = [])
         [set meanDown 0]
         [set meanDown mean downRewards]
       ifelse (meanUp > meanDown) 
         [set currentBaseValue currentBaseValue + epsilon]
         [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))]
         ] ; end of if (updateType = "Own Returns")

2) "Market Returns"

Each agent records the total market returns and adjusts according to them. See the Postpare-Episode procedure. What happens is that the total quantity bid moves to the monopoly price, but the quantities bid by the two agents do not converge to each other.

Notice that if one player plays "Own Returns" and the other plays "Market Returns", the monopoly price is reached, but the "Market Returns" player gets exploited and eventually bids almost nothing.

    if (episodeCount >= epochLength) ; then update and reset
      [ ...


       if (updateType = "Market Returns") [
         ifelse (marketUpRewards = [])
           [set meanUp 0]
           [set meanUp mean marketUpRewards]
         ifelse (downRewards = [])
           [set meanDown 0]
           [set meanDown mean marketDownRewards]
         ifelse (meanUp > meanDown) 
           [set currentBaseValue currentBaseValue + epsilon]
           [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
         ] ; end of if (updateType = "Market Returns")

3) "Market Returns and Own Returns"

An agent pursuing this policy looks, at the end of its epochs, at its mean quantity bid versus the mean quantity bid for the entire market (including its own bids). If its mean quantity bid plus epsilon is lower than the mean quantity bid for the market, the agent raises its baseline bid by epsilon; otherwise, it uses the "Market Returns" policy.

If both agents follow this policy, they achieve the monopoly quanity and equal bid quantities on average. If one agent instead follows the "Own Returns" policy, they converge to Cournot and to each other.

    if (episodeCount >= epochLength) ; then update and reset
      [ ...


       if (updateType = "Market Returns and Own Returns") [
         ifelse (mean myEpochBids + epsilon <= mean marketEpochBids) 
           [set currentBaseValue currentBaseValue + epsilon]  
           [ ; else, I'm ok so I play the good citizen
            ifelse (marketUpRewards = [])
             [set meanUp 0]
             [set meanUp mean marketUpRewards]
            ifelse (downRewards = [])
             [set meanDown 0]
             [set meanDown mean marketDownRewards]
            ifelse (meanUp > meanDown) 
             [set currentBaseValue currentBaseValue + epsilon]
             [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
            ] ; end of else in ifelse (mean myEpochBids...
         ] ; end of if (updateType = "Market Returns and Own Returns")

HOW TO USE IT

Try especially different combinations of the three policies, with different numbers of agents. If only one agent (or firm) is present, we have a monopoly situation and the firm is firm 0. If two agents are present we have a duopoloy between firms 0 and 1. If more than two agents are present, we may have firms 3, 5 or 10 firms. See the SetupNPandAFirms procedures for N=3, 5, 10.

THINGS TO NOTICE

In the "Quantities Bid" plot, the system plots the monopoly, Cournot, and competive price in the market, given the agents and their costs. Where do the suppliers end up? What price prevails? Look at the other plots to see how well each agent does in the market.

THINGS TO TRY

Change the other parameters of the model, accessible on the Interface tab. Look especially at costs and epoch length. Also, turn on random walk for the price function and see how well the agents track the changing price curve.

EXTENDING THE MODEL

There are indefinitely many ways to extend this model.
See in particular the SetupNPandAFirms procedures for N=3, 5, 10, which is fairly limited. You might generalize or at least alter these initialization procedures in order to explore other issues.

RELATED MODELS

oligopolyBidPrice.nlogo, by sok, served as a model for this model. In oligopolyBidPrice.nlogo, "The simulated market proceeds in episodes in which each participating firm offers to supply the entire demand of the market at a bid price. The market selects the price of the lowest bidder, and a new episode begins." This models what is called Bertrand competition.

But here, each firm bids a quantity. The market sums the quantities bid for the total quantity on offer. This sets the price that each firm gets and is often called Cournot competition.

CREDITS AND REFERENCES

This model was created and written by Steven O. Kimbrough in collaboration with Frederic H. Murphy. Please give us credit if you use this model or program.

The model is
freely downloadable at:
http://opim.wharton.upenn.edu/~sok/AGEbook/nlogo/OligopolyPutQuantity.nlogo.

To refer to this model in academic publications, please use: Kimbrough, Steven O. and Murphy, Frederic H. (2007, 2011). Oligopoly Put Quantity model. http://opim.wharton.upenn.edu/~sok/AGEbook/nlogo/OligopolyPutQuantity.nlogo.
In other publications, please use: Copyright Steven O. Kimbrough and Frederic H. Murphy. All rights reserved.

$Id: OligopolyPutQuantity.nlogo 4089 2014-03-20 21:01:52Z sok $

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [ dSlope ; (negative of) demand curve slope: Price = priceIntercept - dSlope * totalQuantity
          episodeAbsoluteCount ; the number of episodes begun in the current run
          currentQ ; the sum of all the quantity bids in the current episode
          currentP ; the market price in the current episode, given the demand curve and currentQ
          marketTotalReward
          quantityIntercept
          priceIntercept ; redundant with pIntercept
          m-quantity ; monopoly quantity, assuming costs are proportional to quantities: ki*Qi
          m-price ; monopoly price, assuming we have the m-quantity
          runningAverageBidList ; used to hold runningAvgLength currentQs
          runningAverageBid ; mean of runningAverageBidList
          runningAverageBidSD ; standard deviation of runningAverageBidList
          cournotQuantity
          runningAverageCournotList ; for the Cournot values
          runningAverageCournot          
          runningAverageMonopolyList ; for the monopoly values, m-quantity
          runningAverageMonopoly
          competitiveQuantity
          runningAverageCompetitiveList ; for the Cournot values
          runningAverageCompetitive
          totalUnitProductionCost ; the sum of all the production costs for all the turtles
          monopolyQFirm0 monopolyQFirm1
          daVersion
          RepetitionCounter ; 
          ratioOfferedCournot ; ratio of runningAverageBid to runningAverageCournot.  Collusion if < 1.
          rPrime ; see paper with Fred
  ] ; end of globals

breed [pAndAers pAndA]     ; Probe-and-Adjust

turtles-own [quantityBid totalReward bidsWon unitProductionCost 
             netReward ; equivalent to Fred's totreward. See FindNetReward
             myEpochBids ; list of bids I made during the present epoch
             marketEpochBids ; list of all bids made during the present epoch
             ]
pAndAers-own [epsilon delta currentBaseValue epochLength episodeCount 
              upRewards downRewards ; for "Own Rewards"
              marketUpRewards marketDownRewards ; for "Market Rewards"
              updateType pricesUp pricesDown PQUp PQdown
              runningAverageReward ; each pAndA player will keep track of the running average of its rewards, across episodes
              marketOwnMixture ; in [0, 1] 100% = Market Rewards, 0% = Own Rewards, 75% = 75% Market Rewards, 25% Own Rewards
              upMarketOwnMixedRewards downMarketOwnMixedRewards ; for Market-Own Mixed Rewards, used with parameter marketOwnMixture
               ;
              ]   
          ;delta ; probe-and-adjust parameter
          ;epsilon ; probe-and-adjust parameter
          ;currentBaseValue ; probe-and-adjust parameter, the base from which probes are made
          ;epochLength ; probe-and-adjust parameter; the number of episodes in an epoch
          ;episodeCount ; the count of the number of episodes in the current epoch
          ;upRewards ; list of rewards received, P*Q(i), when bidding at or above currentAnchorValue
          ;downRewards ; list of rewards received, P*Q(i), when bidding below currentAnchorValue

to Setup ;[daRepetitionCount]
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks
  ;set RepetitionCount daRepetitionCount
  set daVersion "$Id: OligopolyPutQuantity.nlogo 4089 2014-03-20 21:01:52Z sok $"
  if (daRandomSeed != "system clock")
    [random-seed daRandomSeed]  
  set priceIntercept InitPIntercept 
  set quantityIntercept (InitPIntercept / InitDSlope)
  set dSlope InitDSlope

  set runningAverageBidList []
  set runningAverageCournotList []
  set runningAverageMonopolyList []
  set runningAverageCompetitiveList []

  if (initialBaseQuantityFirm0 > initialBaseQuantityFirm1)
    [print "WARNING! initialBaseQuantityFirm0 > initialBaseQuantityFirm1."]
  
  ;PlotInitialDemandCurve
  ; Initialize other globals
  set episodeAbsoluteCount 0
  
  ; Create agents/firms supplying product to the market
  create-pAndAers numPandAFirms
  ask turtles [set totalReward 0
               set bidsWon 0
               set netReward 0
               set myEpochBids []
               set marketEpochBids []]
  ask pAndAers [set upRewards []   
                set downRewards [] 
                set marketUpRewards []
                set marketDownRewards []
                set pricesUp []
                set pricesDown []
                set PQUp []
                set PQDown []
                set runningAverageReward []
                set marketOwnMixture  (runresult ((word "marketOwnMixtureFirm"  [who] of self))) 
                set upMarketOwnMixedRewards []
                set downMarketOwnMixedRewards []
                ]  
  ; Initialize agents/firms supplying product to the market
  if (numPandAFirms = 1)
    [ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
      ]
  if (numPandAFirms = 2)
    [ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
    ask pAndA 1
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue initialBaseQuantityFirm1
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ]
  ] ; end of if numPandAFirms = 2
  if (numPandAFirms = 3)
    [Setup3PandAFirms]
  if (numPandAFirms = 4)
    [Setup4PandAFirms]
  if (numPandAFirms = 5)
    [Setup5PandAFirms]
  if (numPandAFirms = 10)
    [Setup10PandAFirms]

  set totalUnitProductionCost 0
  ask turtles
    [set totalUnitProductionCost (totalUnitProductionCost + unitProductionCost)]
   ;;;;
   ; priceIntercept and dSlope determine the quantityIntercept
   ; Price = PriceIntercept - dSlope*Quantity
   ; QuantityIntercept is the Quantity when Price = 0
   ; So, quantityIntercept = priceIntercept/dSlope
   set quantityIntercept priceIntercept / dSlope
   ; This is the monopolist's quantity when the monopolist has costs 
   ; proportional to quantity: k*Quantity
   ; Here we explicitly assume that Firm0's costs are universal wrt
   ; determining the monopoly quantity.
   set m-quantity ((priceIntercept - unitCostFirm0) / (2 * dSlope))   
   set m-price (InitPIntercept - InitDSlope * m-quantity)
   set cournotQuantity (numPandAFirms * priceIntercept - totalUnitProductionCost) / ((numPandAFirms + 1) * dSlope) 
   set competitiveQuantity (priceIntercept - unitCostFirm0) / dSlope
   PlotInitialDemandCurve
end  ; of setup

to Go
  ; See the documentation in the Information tab.
  set episodeAbsoluteCount episodeAbsoluteCount + 1
  ; 0. Perturb the demand curve if we're doing random-walk-as
  if (random-walk-as)
   [set priceIntercept (random-float 2 * aDelta) - aDelta + priceIntercept
   set quantityIntercept priceIntercept / dSlope
   ; This is the monopolist's quantity given proportional costs of totalUnitProductionCost
   set m-quantity ((priceIntercept - totalUnitProductionCost) / (2 * dSlope))   
   ] ; if of if (random-as)

  ; 1. All of the turtles are asked to determine a bid quantity. Each records his own, in quantityBid.
  ask turtles [
    set-quantityBid
  ] ; end of ask turtles
  ; 2. Sum the bids, the Q(i)s, to get currentQ
  set currentQ 0
  ask turtles [set currentQ (currentQ + quantityBid)]
  ; 2a. Collect quantities bid
  let quantitiesBid []
  ask turtles [set quantitiesBid lput quantityBid quantitiesBid]
  ask turtles [Update-QuantitiesBid(quantitiesBid)]
  ; 3. Determine the current price, P, for the episode
  set currentP PriceGivenQuantity(currentQ)
  ask turtles [findnetreward]
  ; Calculate total profits by all players:
  set marketTotalReward 0
  ask turtles [set marketTotalReward (marketTotalReward + netReward)]   
  ; 4. Turtles observe currentP, and record stats for the episode
  ask turtles [Observe-and-Record(currentP)(currentQ)]
  ; 5. Have agents/suppliers postpare episode
  ask turtles [Postpare-Episode]
  ; 6. Collect and calculate statistics
  set runningAverageBidList lput currentQ runningAverageBidList
  if (length runningAverageBidList > runningAvgLength)
    [set runningAverageBidList but-first runningAverageBidList]
  set runningAverageBid mean runningAverageBidList  
  if length runningAverageBidList > 1
    [set runningAverageBidSD standard-deviation runningAverageBidList]
  ;set runningAverageCournotList lput cournotQuantity runningAverageCournotList
  ;if (length runningAverageCournotList > runningAvgLength)
   ; [set runningAverageCournotList but-first runningAverageCournotList]
  ;set runningAverageCournot mean runningAverageCournotList    
  set runningAverageMonopolyList lput m-Quantity runningAverageMonopolyList
  if (length runningAverageMonopolyList > runningAvgLength)
    [set runningAverageMonopolyList but-first runningAverageMonopolyList]
  set runningAverageMonopoly mean runningAverageMonopolyList    
  set runningAverageCompetitiveList lput competitiveQuantity runningAverageCompetitiveList
  if (length runningAverageCompetitiveList > runningAvgLength)
    [set runningAverageCompetitiveList but-first runningAverageCompetitiveList]
  set runningAverageCompetitive mean runningAverageCompetitiveList  
  ; 7. Plot monopoly quantity, bid Qs, and other statistics:
  ;ifelse (runningAverageCournot > 0)
   ;  [set ratioOfferedCournot (runningAverageBid / runningAverageCournot)]
    ; [set ratioOfferedCournot "N/A"]
  set ratioOfferedCournot (runningAverageBid / cournotQuantity)
  if (cournotQuantity - m-quantity > 0)
   [set rPrime (cournotQuantity - runningAverageBid) / (cournotQuantity - m-quantity)]
;  ifelse (runningAverageCournot - runningAverageMonopoly > 0)
;     [set rPrime (runningAverageCournot - runningAverageBid) / (runningAverageCournot - runningAverageMonopoly)]
;     [set rPrime "N/A"]
  Plot-Quantities(m-quantity)(currentQ) ; (priceIntercept / (2 * dSlope))(currentQ)
  Plot-Bids
  Plot-Average-Rewards  
end  ; of Go

to Postpare-Episode
  ;print "episodeAbsoluteCount = " + episodeAbsoluteCount + " " + who-of self
  if (is-pAndA? self) [
    let meanUp 0
    let meanDown 0
    if (episodeCount >= epochLength) ; then update and reset
      [; The following I added for debugging purposes 2007-5-25, but
       ; it's a better way to handle things anyway.
       let meanUpMarketReturns 0
       let meanDownMarketReturns 0
       ifelse (marketUpRewards = [])
         [set meanUpMarketReturns 0]
         [set meanUpMarketReturns mean marketUpRewards]       
       ifelse (marketDownRewards = [])
         [set meanDownMarketReturns 0]
         [set meanDownMarketReturns mean marketDownRewards]

       let meanUpMarketOwnMixedRewards  0
       let meanDownMarketOwnMixedRewards  0
       ifelse (upMarketOwnMixedRewards = [])
         [set meanUpMarketOwnMixedRewards 0]
         [set meanUpMarketOwnMixedRewards mean upMarketOwnMixedRewards]
       ifelse (downMarketOwnMixedRewards = [])
         [set meanDownMarketOwnMixedRewards 0]
         [set meanDownMarketOwnMixedRewards mean downMarketOwnMixedRewards]
       ;print "111: " + who-of self + " " + meanUpMarketReturns + " " + meanUpMarketOwnMixedRewards + " " + meanDownMarketReturns + " " + meanDownMarketOwnMixedRewards
       ;print "222: " + who-of self + " " + marketUpRewards 
       ;print "333: " + who-of self + " " + upMarketOwnMixedRewards
       ; end of added for debugging
       if (updateType = "Own Returns") [
       ifelse (upRewards = [])
         [set meanUp 0]
         [set meanUp mean upRewards]
       ifelse (downRewards = [])
         [set meanDown 0]
         [set meanDown mean downRewards]
       ifelse (meanUp > meanDown) 
         [set currentBaseValue currentBaseValue + epsilon]
         [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))]
         ] ; end of if (updateType = "Own Returns")

       if (updateType = "Market Returns") [
         ifelse (marketUpRewards = [])
           [set meanUp 0]
           [set meanUp mean marketUpRewards]
         ; oops! this looks like a bug!! 2007-5-25: ifelse (downRewards = [])
         ; should be: It is a bug. But it didn't make much difference.
         ifelse (marketDownRewards = [])
           [set meanDown 0]
           [set meanDown mean marketDownRewards]
         ifelse (meanUp > meanDown) 
           [set currentBaseValue currentBaseValue + epsilon]
           [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
         ] ; end of if (updateType = "Market Returns")

       if (updateType = "Market Prices") [
         ifelse (pricesUp = [])
           [set meanUp 0]
           [set meanUp mean pricesUp]
         ifelse (pricesDown = [])
           [set meanDown 0]
           [set meanDown mean pricesDown]
         ifelse (meanUp > meanDown) 
           [set currentBaseValue currentBaseValue + epsilon]
           [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
         ] ; end of if (updateType = "Market Prices")

       if (updateType = "Market P*Q") [
         ifelse (PQUp = [])
           [set meanUp 0]
           [set meanUp mean PQUp]
         ifelse (PQDown = [])
           [set meanDown 0]
           [set meanDown mean PQDown]
         ifelse (meanUp > meanDown) 
           [set currentBaseValue currentBaseValue + epsilon]
           [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
         ] ; end of if (updateType = "Market P*Q")
         
       if (updateType = "Market Returns, Constrained by Own Returns") [
         let referenceLevel 0
         let tolerance 0
         if (referenceBidGroup = "All bids")
           [set referenceLevel mean marketEpochBids
            set tolerance epsilon]
         if (referenceBidGroup = "Upper quartile of bids")
           [set referenceLevel mean UpperQuartile(marketEpochBids)
            set tolerance delta]       
         ifelse (mean myEpochBids + tolerance <= referenceLevel) 
           [set currentBaseValue currentBaseValue + epsilon]  
           [ ; else, I'm ok so I play the good citizen
            ifelse (marketUpRewards = [])
             [set meanUp 0]
             [set meanUp mean marketUpRewards]
            ifelse (downRewards = [])
             [set meanDown 0]
             [set meanDown mean marketDownRewards]
            ifelse (meanUp > meanDown) 
             [set currentBaseValue currentBaseValue + epsilon]
             [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
            ] ; end of else in ifelse (mean myEpochBids...
         ] ; end of if (updateType = "Market Returns and Own Returns")

       if (updateType = "Market Prices and Own Returns") [
         ifelse ((mean myEpochBids) + epsilon <= mean marketEpochBids) 
           [set currentBaseValue currentBaseValue + epsilon]  
           [ ; else, I'm ok so I play the good citizen
            ifelse (pricesUp = [])
             [set meanUp 0]
             [set meanUp mean pricesUp]
            ifelse (pricesDown = [])
             [set meanDown 0]
             [set meanDown mean pricesDown]
             ;print "meanUp = " + meanUp + " meanDown = " + meanDown
            ifelse (meanUp > meanDown) 
             [set currentBaseValue currentBaseValue + epsilon]
             [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
            ] ; end of else in ifelse (mean myEpochBids...
         ] ; end of if (updateType = "Market Prices and Own Returns")

       if (updateType = "Market Prices and Own Returns") [
         ifelse ((mean myEpochBids) + epsilon <= mean marketEpochBids) 
           [set currentBaseValue currentBaseValue + epsilon]  
           [ ; else, I'm ok so I play the good citizen
            ifelse (PQUp = [])
             [set meanUp 0]
             [set meanUp mean PQUp]
            ifelse (PQDown = [])
             [set meanDown 0]
             [set meanDown mean PQDown]
            ifelse (meanUp > meanDown) 
             [set currentBaseValue currentBaseValue + epsilon]
             [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
            ] ; end of else in ifelse (mean myEpochBids...
         ] ; end of if (updateType = "Market Prices and Own Returns")
 
        if (updateType = "Mixture of Market and Own Returns") [
            ifelse (upMarketOwnMixedRewards = [])
             [set meanUp 0]
             [set meanUp mean upMarketOwnMixedRewards]
            ifelse (downMarketOwnMixedRewards = [])
             [set meanDown 0]
             [set meanDown mean downMarketOwnMixedRewards]
            ifelse (meanUp > meanDown) 
             [set currentBaseValue currentBaseValue + epsilon]
             [set currentBaseValue max (list epsilon (currentBaseValue - epsilon))] 
         ] ; end of if (updateType = "Mixture of Market Own Returns")
         
       set upRewards []
       set downRewards []
       set marketUpRewards []
       set marketDownRewards []
       set pricesUp []
       set pricesDown []
       set PQUp []
       set PQDown []
       set upMarketOwnMixedRewards []
       set downMarketOwnMixedRewards []
       set marketEpochBids []
       set myEpochBids []
       set episodeCount 0
      ] ; of if episodeCount >= epochLength
   ] ; end of if is-pAndA?
end  ; of postpare-episode

to Plot-Bids
  set-current-plot "Individual Bids"
  set-current-plot-pen "Agent0"
  plot [currentbasevalue] of panda 0 
  if (numPandAFirms > 1)
    [set-current-plot-pen "Agent1"
     plot [currentbasevalue] of panda 1] 
  if (numPandAFirms > 2)
    [set-current-plot-pen "Agent2"
     plot [currentbasevalue] of panda 2] 
  if (numPandAFirms > 3)
    [set-current-plot-pen "Agent3"
     plot [currentbasevalue] of panda 3] 
  if (numPandAFirms > 4)
    [set-current-plot-pen "Agent4"
     plot [currentbasevalue] of panda 4
     ] 
  if (numPandAFirms > 5)
    [set-current-plot-pen "Agent5"
     plot [currentbasevalue] of panda 5
     ] 
  if (numPandAFirms > 6)
    [set-current-plot-pen "Agent6"
     plot [currentbasevalue] of panda 6
     ] 
  if (numPandAFirms > 7)
    [set-current-plot-pen "Agent7"
     plot [currentbasevalue] of panda 7
     ] 
  if (numPandAFirms > 8)
    [set-current-plot-pen "Agent8"
     plot [currentbasevalue] of panda 8
     ] 
  if (numPandAFirms > 9)
    [set-current-plot-pen "Agent9"
     plot [currentbasevalue] of panda 9
     ] 
end  ; of Plot-Bids

to Plot-Average-Rewards
  set-current-plot "Average Rewards"
  set-current-plot-pen "Agent0"
  let daAvg ([totalReward] of panda 0 / episodeAbsoluteCount)
  plot daAvg
  if (numPandAFirms > 1)
    [set-current-plot-pen "Agent1"
     set daAvg ([totalReward] of panda 1 / episodeAbsoluteCount)
     plot daAvg] 
  if (numPandAFirms > 2)
    [set-current-plot-pen "Agent2"
     set daAvg ([totalReward] of panda 2 / episodeAbsoluteCount)
     plot daAvg] 
  if (numPandAFirms > 3)
    [set-current-plot-pen "Agent3"
     set daAvg ([totalReward] of panda 3 / episodeAbsoluteCount)
     plot daAvg] 
  if (numPandAFirms > 4)
    [set-current-plot-pen "Agent4"
     set daAvg ([totalReward] of panda 4 / episodeAbsoluteCount)
     plot daAvg
     ] 
  if (numPandAFirms > 5)
    [set-current-plot-pen "Agent5"
     set daAvg ([totalReward] of panda 5 / episodeAbsoluteCount)
     plot daAvg
     ] 
  if (numPandAFirms > 6)
    [set-current-plot-pen "Agent6"
     set daAvg ([totalReward] of panda 6 / episodeAbsoluteCount)
     plot daAvg
     ] 
  if (numPandAFirms > 7)
    [set-current-plot-pen "Agent7"
     set daAvg ([totalReward] of panda 7 / episodeAbsoluteCount)
     plot daAvg
     ] 
  if (numPandAFirms > 8)
    [set-current-plot-pen "Agent8"
     set daAvg ([totalReward] of panda 8 / episodeAbsoluteCount)
     plot daAvg
     ] 
  if (numPandAFirms > 9)
    [set-current-plot-pen "Agent9"
     set daAvg ([totalReward] of panda 9 / episodeAbsoluteCount)
     plot daAvg
     ] 
end  ; of Plot-Average-Rewards

to Plot-Quantities [daMonopoly daTotalBid]
  set-current-plot "Quantities Bid"
  set-current-plot-pen "Total Quantity Bid"
  plot daTotalBid
  ;set-current-plot-pen "Monopoly Quantity"
  ;plot daMonopoly
  ;set competitiveQuantity priceIntercept / dSlope 
  set-current-plot-pen "Competitive Quantity"
  plot competitiveQuantity
  ; set cournotQuantity (numPandAFirms * priceIntercept - totalUnitProductionCost) / ((numPandAFirms + 1) * dSlope) 
  set-current-plot-pen "Cournot Quantity"
  plot cournotQuantity
  set-current-plot-pen "Monopoly Q, Firm(s) 1"
  if (numPandAFirms > 1)
    [set monopolyQFirm1 ((priceIntercept - [unitProductionCost] of pAndA 1) / (2 * dSlope))
     plot monopolyQFirm1] 
  set-current-plot-pen "Monopoly Q, Firm 0"
  set monopolyQFirm0 ((priceIntercept - [unitProductionCost] of pAndA 0) / (2 * dSlope))
  plot monopolyQFirm0
  set-current-plot-pen "Monopoly Quantity"
  plot daMonopoly
end  ; of Plot-Quantities

to Observe-and-Record [price quantity]
  if (is-pAndA? self) [
    let reward (price * quantityBid) - (unitProductionCost * quantityBid)
    let mixedReward (1 - marketOwnMixture) * reward + (marketOwnMixture * (marketTotalReward )  / numPandAFirms)
    ;print reward + " " + mixedReward + " " + marketTotalReward
    set totalReward totalReward + reward
    ifelse (quantityBid >= currentBaseValue)
      [set upRewards lput reward upRewards
       ;set pricesDown lput 0 pricesDown
       set pricesUp lput price pricesUp
       set PQUp lput (price * quantity) PQUp]
      [set downRewards lput reward downRewards
       ;set pricesUp lput 0 pricesUp
       set pricesDown lput price pricesDown
       set PQDown lput (price * quantity) PQDown]
    ifelse (quantityBid >= currentBaseValue)
      [set marketUpRewards lput marketTotalReward marketUpRewards
       set upMarketOwnMixedRewards lput mixedReward upMarketOwnMixedRewards]
      [set marketDownRewards lput marketTotalReward marketDownRewards
       set downMarketOwnMixedRewards  lput mixedReward downMarketOwnMixedRewards]
   ] ; end of if is-pAndA?
end  ; of observe-and-record

to Set-QuantityBid
 if (is-pAndA? self) [
  set quantityBid max (list 0 ((random-float 2 * delta) - delta + currentBaseValue))
  set episodeCount episodeCount + 1
  set myEpochBids lput quantityBid myEpochBids ; record my bid in the epoch record
 ]
end  ; of Set-QuantityBid

to PlotInitialDemandCurve
  set-current-plot "Initial Demand Curve"
  set-current-plot-pen "Demand Curve"
  plot-pen-up
  plotxy 0 InitPIntercept
  plot-pen-down
  plotxy InitPIntercept / InitDSlope 0 ; plot to the quantity intercept 
  let InitMonopolyQ m-quantity ; InitPIntercept / 2
  let InitMonopolyP InitPIntercept - dSlope * InitMonopolyQ ;InitPIntercept / (2 * InitDSlope)
  set-current-plot-pen "Monopoly Q"
  plot-pen-up
  plotxy InitMonopolyQ 0
  plot-pen-down
  plotxy InitMonopolyQ InitMonopolyP
  set-current-plot-pen "Monopoly P"
  plot-pen-up
  plotxy 0 InitMonopolyP
  plot-pen-down
  plotxy InitMonopolyQ InitMonopolyP
end  ; of PlotInitialDemandCurve

to-report PriceGivenQuantity [daQuantity]
  let daPrice priceIntercept - dSlope * daQuantity
  report max (list 0 daPrice)
end  ; of to-report PriceGivenQuantity

to FindNetReward 
  set netReward 0
  if (is-pAndA? self) [
  set netReward ((CurrentP * quantityBid) - (unitProductionCost * quantityBid))
  set runningAverageReward lput netReward runningAverageReward
   if (length runningAverageReward > runningAvgLength)
    [set runningAverageReward but-first runningAverageReward]
  ]
end  ; of FindNetReward

to Update-QuantitiesBid [daBids]
 if (is-pAndA? self) [
  foreach daBids [
  set marketEpochBids lput ? marketEpochBids
  ] ; end of foreach
 ]
end  ; of Update-QuantitiesBid

to Setup3PandAFirms
    ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
    ask pAndA 1
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue initialBaseQuantityFirm1
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ]
    ask pAndA 2
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue ((initialBaseQuantityFirm1 + initialBaseQuantityFirm0) / 2)
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
end  ; of Setup3PandAFirms

to Setup4PandAFirms
  let increment (initialBaseQuantityFirm1 - initialBaseQuantityFirm0)
    ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
    ask pAndA 1
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue initialBaseQuantityFirm1
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ]
    ask pAndA 2
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue  (initialBaseQuantityFirm1 + increment) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 3
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue  (initialBaseQuantityFirm1 + (2 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
end  ; of Setup4PandAFirms

to Setup5PandAFirms
  let increment (initialBaseQuantityFirm1 - initialBaseQuantityFirm0)
    ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
    ask pAndA 1
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue initialBaseQuantityFirm1
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ]
    ask pAndA 2
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue  (initialBaseQuantityFirm1 + increment) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 3
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue  (initialBaseQuantityFirm1 + (2 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 4
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue  (initialBaseQuantityFirm1 + (3 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
end  ; of Setup5PandAFirms

to Setup10PandAFirms
  let increment (initialBaseQuantityFirm1 - initialBaseQuantityFirm0)
    ask pAndA 0
      [set epsilon epsilonFirm0
       set delta deltaFirm0
       set currentBaseValue initialBaseQuantityFirm0
       set unitProductionCost unitCostFirm0
       set epochLength epochLengthFirm0
       set episodeCount 0
       set updateType updateTypeFirm0
      ]
    ask pAndA 1
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue initialBaseQuantityFirm1
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ]
    ask pAndA 2
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + increment) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 3
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (2 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 4
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (3 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 5
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (4 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 6
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (5 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 7
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (6 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 8
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (7 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
    ask pAndA 9
     [set epsilon epsilonFirm1
     set delta deltaFirm1
     set currentBaseValue (initialBaseQuantityFirm1 + (8 * increment)) 
     set unitProductionCost unitCostFirm1
     set epochLength epochLengthFirm1
     set episodeCount 0
     set updateType updateTypeFirm1
    ] 
end  ; of Setup10PandAFirms

to-report UpperQuartile [ aList ]
  if (empty? aList)
   [print "In UpperQuartile reporter. The list is empty."
    let bob 1 / 1]  ; fix this to halt
  let bList reverse sort aList
  ;print bList
  let daQuartileSize int (length aList / 4)
  ;print daQuartileSize
  ifelse (length bList mod 4 = 0)
    [report sublist bList 0 (daQuartileSize)]
    [report sublist bList 0 (daQuartileSize + 1)]
  ;ifelse (daRemainder 
end 

to RRepetitions
print "Output coming from the RRepetitions procedure:"
;let daRepetitionCount RepetitionCount
if (file-exists? "oligopolyBidQuantity-repetitions.txt")
  [file-delete "oligopolyBidQuantity-repetitions.txt"]
; Open the parameter sweep file and print the column headers.
; Also print the column headers to the output window
; Then close the parameter sweep file

file-open "oligopolyBidQuantity-repetitions.txt"
file-print "RepetitionCount,runningAverageBid,runningAverageBidSd,ratioOfferedCournot,rPrime,meanRewardFirm0,meanRewardFirm1"
file-close
print "RepetitionCount,runningAverageBid,runningAverageBidSd,ratioOfferedCournot,rPrime,meanRewardFirm0,meanRewardFirm1"
let numFirms numPandAFirms
let runningAverageBidAccumulator []
;let pAndAAccumulator []
let pAndA0Accumulator []
let pAndA1Accumulator []
let pAndA2Accumulator []
let pAndA3Accumulator []
let pAndA4Accumulator []
let pAndA5Accumulator []
let pAndA6Accumulator []
let pAndA7Accumulator []
let pAndA8Accumulator []
let pAndA9Accumulator []
;foreach (n-values NumRepetitions [?])
let RepetitionCount 0
while [RepetitionCount < NumRepetitions]
[
Setup ; (RepetitionCount)
let torun numEpisodesToRun
while [torun > 0]
[go
set torun torun - 1
]
set RepetitionCount RepetitionCount + 1
set RepetitionCounter RepetitionCount
;set rPrime (runningAverageCournot - runningAverageBid) / (runningAverageCournot - runningAverageMonopoly)
file-open "oligopolyBidQuantity-repetitions.txt"
file-print (word RepetitionCount  ","  runningAverageBid  ","  runningAverageBidSD  ","  ratioOfferedCournot  ","  rPrime  ","  mean [runningAverageReward] of pAndA 0  ","  mean [runningAverageReward] of pAndA 1)
file-close
print (word RepetitionCount   ","   runningAverageBid   ","   runningAverageBidSD   ","   ratioOfferedCournot   ","   rPrime    ","   mean [runningAverageReward] of pAndA 0   ","   mean [runningAverageReward] of pAndA 1)
set runningAverageBidAccumulator fput runningAverageBid runningAverageBidAccumulator

; This is really inelegant, but I couldn't get run and runresult to do it for me.
; Could use lists as arrays... well I'm in a hurry and the max is 10
; Actually, to test in the future, I can try creating local agents mirroring
; the panda agents. The local ones won't be zapped by Setup. I tried it doesn't work.
set pAnda0Accumulator fput mean [runningAverageReward] of pAndA 0 pAnda0Accumulator
if (numFirms > 1)
[set pAnda1Accumulator fput mean [runningAverageReward] of pAndA 1 pAnda1Accumulator]
if (numFirms > 2)
[set pAnda2Accumulator fput mean [runningAverageReward] of pAndA 2 pAnda2Accumulator]
if (numFirms > 3)
[set pAnda3Accumulator fput mean [runningAverageReward] of pAndA 3 pAnda3Accumulator]
if (numFirms > 4)
[set pAnda4Accumulator fput mean [runningAverageReward] of pAndA 4 pAnda4Accumulator]
if (numFirms > 5)
[set pAnda5Accumulator fput mean [runningAverageReward] of pAndA 5 pAnda5Accumulator]
if (numFirms > 6)
[set pAnda6Accumulator fput mean [runningAverageReward] of pAndA 6 pAnda6Accumulator]
if (numFirms > 7)
[set pAnda7Accumulator fput mean [runningAverageReward] of pAndA 7 pAnda7Accumulator]
if (numFirms > 8)
[set pAnda8Accumulator fput mean [runningAverageReward] of pAndA 8 pAnda8Accumulator]
if (numFirms > 9)
[set pAnda9Accumulator fput mean [runningAverageReward] of pAndA 9 pAnda9Accumulator]


] ; end of  while
if (RepetitionCounter > 1)
[
print (word "overall mean sd of avg. bid,"   mean runningAverageBidAccumulator   ","   standard-deviation runningAverageBidAccumulator)
print "\\begin{center}"
print "\\begin{tabular}{|r|r|r|}\\hline"
print "Firm & Mean of Running Averages & SD of Running Averages \\\\ \\hline\\hline"
if (numFirms > 0)
[print (word "0 & "   mean pAnda0Accumulator   " & "   standard-deviation pAnda0Accumulator   "\\\\ \\hline")]
if (numFirms > 1)
[print (word "1 & "  mean pAnda1Accumulator  " & "  standard-deviation pAnda1Accumulator  "\\\\ \\hline")]
if (numFirms > 2)
[print (word "2 & "  mean pAnda2Accumulator  " & "  standard-deviation pAnda2Accumulator  "\\\\ \\hline")]
if (numFirms > 3)
[print (word "3 &"  mean pAnda3Accumulator  " & "  standard-deviation pAnda3Accumulator   "\\\\ \\hline")]
if (numFirms > 4)
[print (word "4 & "  mean pAnda4Accumulator  " & "  standard-deviation pAnda4Accumulator  "\\\\ \\hline")]
if (numFirms > 5)
[print (word "5 & "  mean pAnda5Accumulator  " & "  standard-deviation pAnda5Accumulator   "\\\\ \\hline")]
if (numFirms > 6)
[print (word "6 & "  mean pAnda6Accumulator  " & "  standard-deviation pAnda6Accumulator  "\\\\ \\hline")]
if (numFirms > 7)
[print (word "7 & "  mean pAnda7Accumulator  " & "  standard-deviation pAnda7Accumulator  "\\\\ \\hline")]
if (numFirms > 8)
[print (word "8 & "  mean pAnda8Accumulator  " & "  standard-deviation pAnda8Accumulator  "\\\\ \\hline")]
if (numFirms > 9)
[print (word "9 & "  mean pAnda9Accumulator  " & "  standard-deviation pAnda9Accumulator  "\\\\ \\hline")]
print "\\end{tabular}"
print "\\end{center}"
]
end 

to ParameterSweep
 reset-timer
; Delete the parameter sweep file if it exists, so that we
; don't append data to existing data.
;if (file-exists? "oligopolyBidQuantity-parametersweep-epochlengths.txt")
;  [file-delete "oligopolyBidQuantity-parametersweep-epochlengths.txt"]
if (file-exists? "oligopolyBidQuantity-parametersweep.txt")
  [file-delete "oligopolyBidQuantity-parametersweep.txt"]
; Open the parameter sweep file and print the column headers.
; Also print the column headers to the output window
; Then close the parameter sweep file
;file-open "oligopolyBidQuantity-parametersweep-epochlengths.txt"

file-open "oligopolyBidQuantity-parametersweep.txt"
file-print "RepetitionCount,numEpisodesToRun,epochLengthFirm0,epochLengthFirm1,meanRewardFirm0,meanRewardFirm1,runningAverageBid,runningAverageBidSD,deltaFirm0,deltaFirm1,epsilonFirm0,epsilonFirm1,timer"
print "RepetitionCount,numEpisodesToRun,epochLengthFirm0,epochLengthFirm1,meanRewardFirm0,meanRewardFirm1,runningAverageBid,runningAverageBidSD,deltaFirm0,deltaFirm1,epsilonFirm0,epsilonFirm1,timer"
file-close

let deltaFirm0sweep 0
let deltaFirm0hold deltaFirm0
let deltaFirm1sweep 0
let deltaFirm1hold deltaFirm1

let epochLengthFirm0sweep 0
let epochLengthFirm0hold epochLengthFirm0
let epochLengthFirm1sweep 0
let epochLengthFirm1hold epochLengthFirm1

let torun 0
let RepetitionCount 0
while [RepetitionCount < NumRepetitions]
[
foreach (list 6600 7600)
[set numEpisodesToRun ?
foreach (list 2.2  3.8)
[set deltaFirm0 ?
foreach (list 2.2  3.8)
[set deltaFirm1 ?
foreach (list 0.4 1.0)
[set epsilonFirm0 ?
foreach (list 0.4 1.0)
[set epsilonFirm1 ?
foreach (list 24  36)
[set epochLengthFirm0sweep ?
foreach (list 24  36)
[set epochLengthFirm1sweep ?
Setup 
set RepetitionCounter RepetitionCount
set epochLengthFirm0 epochLengthFirm0sweep
set epochLengthFirm1 epochLengthFirm1sweep
set torun numEpisodesToRun
while [torun > 0]
[go
set torun torun - 1
]
file-open "oligopolyBidQuantity-parametersweep.txt"
file-print (word RepetitionCount  ","  numEpisodesToRun  ","  epochLengthFirm0  ","  epochLengthFirm1  ","   mean [runningAverageReward] of pAndA 0  ","  
 mean [runningAverageReward] of pAndA 1  ","  runningAverageBid  ","  runningAverageBidSD  ","  deltaFirm0  ","  deltaFirm1
   ","  epsilonFirm0  ","  epsilonFirm1  ","  timer)
file-close
print (word RepetitionCount  ","  numEpisodesToRun  ","  epochLengthFirm0  ","  epochLengthFirm1  ","   mean [runningAverageReward] of pAndA 0  
","   mean [runningAverageReward] of pAndA 1  ","  runningAverageBid  ","  runningAverageBidSD  
","  deltaFirm0  ","  deltaFirm1  ","  epsilonFirm0  ","  epsilonFirm1  ","  timer)

] ; end of first foreach
] ; end of second foreach
] ; end of third foreach
] ; end of fourth foreach
] ; end of fifth foreach
] ; end of sixth foreach
] ; ebd if seventh foreach
set RepetitionCount RepetitionCount + 1
set RepetitionCounter RepetitionCount

] ; end of while
; Restore original values
set epochLengthFirm0 epochLengthFirm0hold
set epochLengthFirm1 epochLengthFirm1hold
end 

to test
print "Hi from test."
let pAndA0Accumulator []
print pAndA0Accumulator
set pAndA0Accumulator fput "bob" pAndA0Accumulator
print pAndA0Accumulator
set pAndA0Accumulator fput "carol" runresult ((word "pAndA"  0  "Accumulator"))
print pAndA0Accumulator
;set (run runresult ("pAndA" + 0 + "Accumulator") fput "ted" runresult ("pAndA" + 0 + "Accumulator"))
print pAndA0Accumulator
end  

There is only one version of this model, created over 9 years ago by Steven Kimbrough.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.