BABSTER

BABSTER preview image

1 collaborator

Jose Jose Alfaro (Author)

Tags

developing countriesa 

Tagged by Jose Alfaro over 8 years ago

developing countriesr 

Tagged by Jose Alfaro over 8 years ago

policy and decision tools 

Tagged by Jose Alfaro over 8 years ago

renewable energy 

Tagged by Jose Alfaro over 8 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.2.0 • Viewed 570 times • Downloaded 41 times • Run 0 times
Download the 'BABSTER' modelDownload this modelEmbed this model

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


Comments and Questions

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

Click to Run Model

; This allows Netlogo to use the Network and GIS extensions. These extensions must be in the right folder
; with the Netlogo app.  They should be in the Netlogo/extensions folder.  
extensions [nw gis]

;Here are the global variables we will use in the model
globals [
  testname                      ;Holds the name of the test to create an appropriate file
  simulation                    ;With this variable the model keeps track of what simulation rep it is running during verification and general experiments
  average-cluster-coefficient ; This variable holds the cluster coefficient resulting in the world after the network is finished.  
  largest-centrality-group ; this is just to check that the centrality procedure is working right
  total-population              ;Total people in the world
  average-population            ;People per population center in the world
  discount-rate                 ;Sets the discount rate for all the projects' finances
  taxes-insurance               ;A combination of expected taxes and insurance and is used in the capital recovery factor (CRF)
  
  ;The capital recovery factor for solar, biomass, hydro, and transmission projects
  ;are different because of the expected lifetime of each infrastructure
  CRFsolar  
  CRFbatteries        
  CRFbiomass       
  CRFhydro
  CRFtrans       
  pl       ; This is the fuel price levelizer, a unit needed for CRF
           ;Financial costs for some of the technologies

    
     
  
  
  overall-lcoe                 ;This is a total levelized cost of electricity for the world
  
  ;Different life spans of the capital investments
  solar-lifespan battery-lifespan biomass-lifespan hydro-lifespan transmission-lifespan  
 
  darks-list                    ; Holds a list of all the darks (unelectrified populations)
  projects                      ; Agentset of all populations that still need electricity supply either partially or fully. 
  sockets                       ; Agentset available for others to connect to (including power-plants) because they
                                ; are now part of a secondary network 
  project-patch                 ; Holds the patch identified by the observer as the next site for development
  expansion-node                ; Population that is going to be connected or receive a project next
  %-unelectrified               ;; People remaining with no access to electricity
    
  ;The observer uses this variable to determine if his preference is for grid extension "ge", 
  ;or decentralized generation "dg"
  electrification-method        
                                
  total-plant-investment        ;; How much has been spent on generation equipment
  total-transmission-investment ;; How much has been spent on transmission lines
  initial-population            ; An agent that will be the first population to get connected.  This is used for the 
                                ;Largest-Population preference so the model can keep track of who is the population
                                ;that is the largest and is to be connected first.  
  total-generation              ;; The total generation capacity of all the power plants created
  
  
   
  area-of-patches
  hours-of-sun         
               
  ;; Percent of total kms of line of each grade
  %-33kv %-138kv %-230kv %-345kv %-500kv 
  %-right-of-way-area           ;; % of area needed for right of way of transmission lines
  

 
  ;; These variables hold the total installed capacity of each technology
  solar-installed-capacity biomass-installed-capacity hydro-installed-capacity
  
  ;; These variables hold the fuel portfolio with the percent generation available according 
  ;; to each technology
  %-solar-gen %-biomass-gen %-hydro-gen
  
  ;; These variables hold the percentage of the total potential of each technology that has 
  ;; been installed.  Or in other words, the percent used of the total available of each 
  ;; technology
  %-of-solar-used %-of-biomass-used %-of-hydro-used
  
  
  
  total-economic-inflows        ; The total yearly money flow created within communities due to renewable projects
  job-years                     ; Total job years created due to renewable energy projects
  ; GIS datasets loaded
  countys-dataset districts-dataset clans-dataset 
  hydro-dataset ;(vectors of the actual rivers)
  
  hydro-sites                   ; Holds a list of patches that are the identified sites for small hydro sites
  hydro-max-potentials          ; Holds the corresponding max potential for the sites in hydro-sties list
   
  secondary-grids               ; Holds the number of secondary grids that are made in the network
  dg-mix                        ; The level of decentralized generation that results
   
  county-list                   ; An actual list of the county names
  
  total-power-plants            ; Counts the number of power plants in the world
  clusters                      ;;Holds the number of clusters that are made in the network
  
  turn target-county ;; These are used to determine the county we should put the next power-plant in
                     ;; when working with the equal development strategy.  
  
  ;; These are the biomass potentials for each county
  county-biomass
  county-average-biomass
  county-patch-count
  
  bomi-biomass
  bomi-patches
  bomi-average-biomass
  
  bong-biomass
  bong-patches
  bong-average-biomass
  
  grand-bassa-biomass
  grand-bassa-patches
  grand-bassa-average-biomass
  
  grand-cape-mount-biomass
  grand-cape-mount-patches
  grand-cape-mount-average-biomass
  
  grand-gedeh-biomass
  grand-gedeh-patches
  grand-gedeh-average-biomass
  
  grand-kru-biomass
  grand-kru-patches
  grand-kru-average-biomass
  
  lofa-biomass
  lofa-patches
  lofa-average-biomass
  
  margibi-biomass
  margibi-patches
  margibi-average-biomass
  
  maryland-biomass
  maryland-patches
  maryland-average-biomass
  
  montserrado-biomass
  montserrado-patches
  montserrado-average-biomass
  
  nimba-biomass
  nimba-patches
  nimba-average-biomass
  
  rivercess-biomass
  rivercess-patches
  rivercess-average-biomass
  
  sinoe-biomass
  sinoe-patches
  sinoe-average-biomass
  
  river-gee-biomass
  river-gee-patches
  river-gee-average-biomass
  
  gbarpolu-biomass
  gbarpolu-patches
  gbarpolu-average-biomass
  
  ; Number of power plants in each county
  Bomi-pp Bong-pp Grand-Bassa-pp Grand-Cape-Mount-pp Grand-Gedeh-pp Grand-Kru-pp Lofa-pp Margibi-pp Maryland-pp  ;; Number of Power Plants in each county
  Montserrado-pp Nimba-pp Rivercess-pp Sinoe-pp River-Gee-pp Gbarpolu-pp
  
  
  ;Globals for verification data
   p-largest-potential ;patch with the largest resource potential
   p-central ; patch with the larges resource centrality
   biggest-population ;project with the most people
   p-biggest-population ; best patch for the project with the most people
   p-jobs ;patch with the most job potential
   p-inflows ; patch with the most economic inflows
   lowest-project-cost ; project with the lowest project cost
   p-lowest-cost ; best patch for the project with the lowest project cost
   lowest-cost ;actual project cost of the lowest project cost
  
   lowest-dg-project ;project with lowest dg cost
   lowest-dg-cost ;dg cost of the lowest dg cost project
   lowest-dg-best-patch ;best-patch] of lowest-dg-project
  
   lowest-ge-project ;project with the lowest ge cost
   lowest-ge-cost ;ge-cost of lowest-ge-project
   lowest-ge-best-patch ;best-patch of lowest-ge-project
  
   most-recent-plant ;latest plant created 
   most-recent-patch-developed ;patch where the most-recent-plant was built
  
   line-list ;contains the lines that are being created
   patch-v ;a patch I will follow for verification purposes
   agent-v ;a turtle I will follow for verification purposes
  ; (counties "Bomi" "Bong" "Grand Bassa" "Grand Cape Mount" "Grand Gedeh" "Grand Kru" "Lofa" "Margibi"
   ; "Maryland" "Montserrado" "Nimba" "Rivercess" "Sinoe" "River Gee" "Gbarpolu")
   storage-needed ; Shows the storage necessary to keep a kW of solar as dispatchable given the load curve
   load-factor
   solar-required-per-kw-demand  ;the kWp solar installation required per kW of solar demand calculated with the load factor
   patchlist ;a list of patches that need a tie breaker in case of similar variable values
   
   average-generator-size ;the average size of generation
   generator-list ;a list of the generators we have
   generator-list-size ;a list of the sizes of the generators
   solar-job-factor hydro-job-factor biomass-job-factor               ; The number of internal jobs created per unit energy produced

   solar-economic-factor hydro-economic-factor biomass-economic-factor ;Amount of internal economic flows created per unit energy produced
]

breed [ power-plants power-plant ] ;; Power Production Units 
breed [ darks dark ]               ;; Agents that are not connected
breed [ electrifieds electrified ] ;; Fully electrified agents
breed [ incompletes incomplete ]   ;; Connected agents with some demand remaining
breed [unmets unmet]               ;; Agents that can't be supplied because there is no further capacity to provide them

turtles-own [
  name                 ; Name of the district they represent
  in-county            ; County they are located in
  baseline-people      ; Number of people the agent starts with at the begining of the configuration
                       ; It helps keep the geography stable in case we move to making growing population experiments
  people               ; Number of people the agent has in this time step
  original-demand      ; Initial demand of the people in the agent
  remaining-demand     ; Demand that has not been met at this step
  closest-socket       ; Closest socket to a project
  distance-to-socket   ; Distance to the closest socket
  possible-supplier    ; A power plant that may provide the power demand of the turtle through the socket
  possible-supply      ; When calculating the transmission cost, turtles use this variable to determine the amount of electricitiy
                       ; they may be getting from their possible supplier.  
  possible-line-cost   ; The cost per km of a possible line to the possible-socket
  supplier             ; Power-plant an agent is connected to through its network supplying electricity
  supplier-list        ; List of Power-plants an agent is connected to through its network if more than one
  possible-technology  ; The source of electricity that the possible-supplier has
  technology           ; The source of electricity that the agent actually is connected to
  ge-cost              ; The cost of connecting to a grid with capacity
  dg-cost              ; The cost of decentralized generation including generation equipment and transmission
  project-cost         ; The lowest cost of either dg or ge costs
  best-patch           ; The patch that provides the cheapest dg cost for the agent determined by distance and available capacity 
  dg-distance          ; Distance to the best patch
  connection-mode      ; The agent's preference at this step between ge and dg
  
  network              ; A turtle set of all the agents connected to this agent (its secondary grid)
  transmission-links   ; A link set of all the links in connected to this agent (the transmission of its secondary grid)
  open-capacity?       ; True if the power plants in the agent's secondary grid have capacity for expansion
  transmission-capital-cost     ;;it is the variable that holds the cost of transmission wires
                                ;; which varies depending on capacity and length
  network-upgrade-cost ; This is the cost that would be incurred if a network has to be upgraded to
                       ; carry a load to a new agent 
  shortest-path        ; Link-set of shortest path between a turtle and its supplier (for possible upgrade cost)
  shortest-path-list
  turtles-on-shortest-path ; The agents that are on the shortes path to the supplier
  turtles-on-shortest-path-list
  demand-on-path       ; The sum of the demand that is on the shortest path
  
  ge-jobs
  dg-jobs
  max-jobs
  
  ge-flows
  dg-flows
  max-flows
]

electrifieds-own [
  node-to-connect      ; The dark or incomplete that the electrified will connect to (the counter part
                       ; of who this agent is a socket to)
]

power-plants-own [
  populations-serviced ; Agentset of populations the power plant is supplying (its secondary grid)
  demand-serviced      ; Sum of the demand in the power-plants secondary grid
  transmission-serviced; Sum of the lenghts of the links in the power plant secondary grid
  node-to-connect      ; The dark or incomplete that the electrified will connect to (the counter part
                       ; of who this agent is a socket to)
  max-capacity         ; Max capacity available in the patch the power plant is in
  available-capacity   ; Capacity that is in the patch and is not being supplied to agents by the power plant 
  generation-size      ; The generation size of the power plant
  capital-cost         ; Investment for this plant at its present capacity
  initial-connection   ; The first agent the power plant connects to
  location             ; The county where power plants are located
]


patches-own [
  baseline-potential   ; This is the original potential in the configuration of the world
                       ; It helps keep the geography stable
  largest-potential    ; The largest potential in the patch (only a single technology)
  developed?           ; True if there is a power-plant in the patch.  Patches can only hold one power plant

  distance-list        ; List of the distance to each population agent
  demand-list      ; List of all the populations
  ci-list              ; List of the ration of each population's number of people divided by its 
                       ; distance to this patch = (distance between  agetn i and this patch)/(people in i) 
  centrality           ; Sum of the items in the ci-list.  Low centrality is good, means the patch is
                       ; relatively close to a lot of people.  
  resource-centrality  ; The ratio of the largest potential to the centrality.  Large resource centrality 
                       ; is good.  Means that the patch puts high level of resources in relative proximity to 
                       ; a lot of people.  
  ; Each technology's LCOE for using the Patche's resources to an Agent calling the variable
  ; The patch owns the variable but it changes depending on the Agent who asks.  
  dg-solar-cost-per-kw        
  dg-biomass-cost-per-kw
  dg-hydro-cost-per-kw
  dg-cost-per-kw ; The chosen technology in the end LCOE
  dg-solar-jobs
  dg-biomass-jobs
  dg-hydro-jobs
  patch-possible-jobs
  dg-possible-jobs-list
  dg-solar-flows
  dg-hydro-flows
  dg-biomass-flows
  dg-possible-flows-list
  patch-possible-flows
  
  possible-transmission-capital-cost-list ; This list holds only the capital cost of transmission per kWh produced
  possible-transmission-cost-list
  possible-solar-transmission-cost
  possible-biomass-transmission-cost
  possible-hydro-transmission-cost
  possible-transmission-cost   ; The cost of building transmission lines to this Patch from a certain Agent. 
                       ; The patch owns the variable but it changes according to the Agent
                       ; calling the procedure.  
  county               ; The county the patch is located in
  biomass-shed                  ; The patch set around this Patch whose biomass potential can be 
                                ; economically transported to this patch for electricity generation
  
  biomass-potential             ; The potential in this patch according to each technology.  
  aggregated-biomass-potential  ; aggregated-biomass-potential refers to the total biomass potential the 
  solar-potential               ; the Patch has access to including its neighboring patches (radius)
  hydro-potential
  biomass-base-potential        ; The base potential that each patch receives at the begining of 
  solar-base-potential          ; a world configuration.  
  hydro-base-potential
  
  largest-potential-type        ; The technology that is the largest potential for this Path
  source                        ; The technology that the Production Unit in this patch is using
  
  solar-inflows                 ; Potential Yearly economic inflows from each renewable technology  
  biomass-inflows               ; in this patch and the economic flows from the technology chosen
  hydro-inflows                 ; by the production unit to be created or actually created here.  
  project-inflows
  
  solar-jobs                    ; Potential number of job years from each renewable technology
  biomass-jobs                  ; in this patch and the job years created from the 
  hydro-jobs                    ; technology chosen by the Production unit to be created or created here.  
  project-jobs
  line-cost
  closest-project
  priority
  ]

links-own [
  span                 ;Since length and distance are primitives a different variable name must be used
                       ;to store the length of the link in km.  
  grade                ;This is the grade of the link 
  cost-km              ;Cost of this grade of link per km
  new-cost-km          ;This is the cost per km that a cable would require if upgraded according to the 
                       ;different loadability tests
  upgrade-cost-km      ;The difference between the old cost of the cable and the new cost
  load-carried         ;The load that a link is transferring between its two nodes
  upgrade-load         ;The new load that a link would transfer if a new project is added to the secondary
                       ;network the link belongs to.  
]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to load-the-world

;; This procedure loads the GIS datasets that we need.  In the future the user may be able to 
;; select the datasets of different countries.  
;; The procedure also creates the populations and sets the needed variables.  
ca
; Load all datasets
set countys-dataset gis:load-dataset "LiberiaGis/county boundary.shp"
set districts-dataset gis:load-dataset "LiberiaGis/district boundary.shp"
set clans-dataset gis:load-dataset "LiberiaGis/clan boundary.shp"
; The river data is included only for possible visual representation.  
set hydro-dataset gis:load-dataset "LiberiaRenewables/rivers.shp"
gis:set-world-envelope (gis:envelope-union-of (gis:envelope-of countys-dataset)
  (gis:envelope-of districts-dataset)
  (gis:envelope-of clans-dataset))
set county-list (list "Bomi" "Bong" "Grand Bassa" "Grand Cape Mount" "Grand Gedeh" "Grand Kru" "Lofa" "Margibi" "Maryland"
  "Montserrado" "Nimba" "Rivercess" "Sinoe" "River Gee" "Gbarpolu")
set area-of-patches 89453012.05 ;This is the area of a patch in the model in m2 according to the size of Liberia and the number of patches here.  
create-population-centers
;Please note that because we calculate patch potentials in the load-world, anytime we change the load curve shape we will have to go back to loading the world
; The same is true with the battery requirements.
set hours-of-sun insolation
calculate-load-factor
set-technology-factors ; Sets the job and ecnomic factors  
set-patch-potentials
calculate-CRF
if randomize-tech-costs = true
[calculate-technology-costs]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to set-technology-factors
  set solar-job-factor 0.88 * 10 ^ -6 ;Jobs per kWh
  set solar-economic-factor 9.1 * 10 ^ -3 ;$ per kWh
  
  set hydro-job-factor 0.28 * 10 ^ -6 ;Jobs per kWh
  set hydro-economic-factor 0 * 10 ^ -3 ;$ per kWh
  
  set biomass-job-factor 0.21 * 10 ^ -6 ;Jobs per kWh
  set biomass-economic-factor 36.8 * 10 ^ -3 ;$ per kWh
end 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  ; This procedure sets up the world to start a simulation.  Notice it doesnt change things
  ;that have been loaded with the world.  However, it allows for new configurations of the same
  ;world.  For instance you can try the GE and DG preferences of the central stakeholder without 
  ;changing the financial parameters and demand of the population.  
  
  ; Because the user might be trying to compare some preferences to each other we need to be able
  ;return everything to its original state.  So the first few comands are for this purpose.  
  reset-ticks
  ask links [die]
  ask power-plants [die]
  if duration-base-load + duration-morning-peak + duration-evening-peak > 24 
  [user-message ("There are more than 24 hours in your load curve!")
    stop]
  set darks-list []
  set line-list []
  set agent-v one-of turtles 
  set patch-v one-of patches
  ask turtles [
    set breed darks
    set network turtle-set self 
    set supplier-list turtle-set nobody 
    set transmission-links link-set nobody 
    set shortest-path link-set nobody
    set shape "house" set size 1
    set original-demand (people / 7) * peak-power-demand / 1000 ;This puts units in kW to match the potentials data later
    set remaining-demand original-demand
    set darks-list lput (self) darks-list ; Starts a list with all darks
    set projects turtle-set darks
    set possible-supplier nobody]
  reset-base-potentials
  calculate-load-factor
  calculate-storage-needed
  ask patches with [not is-string? county]
  [set pcolor grey
    set developed? true] 
  pick-project-patch
  print project-patch
  initiate-grid
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  nw:set-context turtles links
  set expansion-node nobody
  set darks-list []
  set projects (turtle-set darks incompletes)           ; This puts all the turtles that need supply into one group
  set sockets (turtle-set power-plants electrifieds)    ; This puts all the turtles that are supplied in one group
 
  ask patches [                                         ; This ensures that the patches are using fresh data to calculate decisions
    set possible-transmission-cost 0
    set dg-cost-per-kw 0
    set dg-solar-jobs 0
    set dg-biomass-jobs 0
    set dg-hydro-jobs 0
    set patch-possible-jobs 0
    set dg-possible-jobs-list []
    set dg-solar-flows 0
    set dg-hydro-flows 0
    set dg-biomass-flows 0
    set dg-possible-flows-list []
    set patch-possible-flows 0]
  
  ask patches with [developed? = false][
    update-potentials
    ]
  
  

  ask sockets [
  ;; Sockets whose power-plants have capacity update their status so that other projects can connect to them  
    ifelse [available-capacity] of supplier > 0 
    [set open-capacity? true]
    [set open-capacity? false]]
  
  
  
  ifelse any? projects
  ;; If there are still populations that dont have enough supply the model continues 
  [ask projects [
      set darks-list lput (self) darks-list
      set best-patch nobody
    ]     ;; All projects decide what is cheaper for them dg or ge
  pick-next-project       ;; The model determines who is going to get connected
  ask expansion-node [      ;; The turtle to get connected is connected or a dg project is built
    grow-network]
  calculate-reporting-variables
  
   ;print-verification-data
 ;print project-patch
   tick  
   ]
  
  ;; If all the populations have supply the power-plants update their variables and the model stops.  
  [calculate-reporting-variables
    
    stop   ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-reporting-variables 
 
   set total-plant-investment sum [capital-cost] of power-plants
   set total-transmission-investment sum [span * cost-km ] of links * 9.46
   set total-generation sum [max-capacity - available-capacity] of power-plants
   calculate-generation-portfolio
   set overall-lcoe (%-solar-gen * solar-capital * CRFsolar / (load-factor * 8760)) + (%-solar-gen * solar-o&m / (load-factor * 8760))
   + (%-solar-gen * battery-capital * (((morning-peak * duration-morning-peak + base-load * duration-base-load + duration-evening-peak) - hours-of-sun * base-load) / round-trip-battery-efficiency) / (load-factor * 8760))  
   + (%-biomass-gen * biomass-capital * CRFbiomass / (load-factor * 8760)) + (%-biomass-gen * biomass-o&m / (load-factor * 8760)) 
   + %-biomass-gen * ((Pl * biomass-fuel) / (biomass-heat-content * .30 * 278 ))
   + (%-hydro-gen * hydro-capital * CRFhydro / (load-factor * 8760)) + (%-hydro-gen * hydro-o&m / (load-factor * 8760))
   + (total-transmission-investment * CRFtrans / (load-factor * 8760 * sum [original-demand] of turtles))
   calculate-line-percentages
   calculate-total-economic-inflows
   calculate-job-years
   
   set secondary-grids (length nw:weak-component-clusters) - count darks
   set dg-mix secondary-grids / (count electrifieds)
   report-locations
   
   set total-population sum [people] of turtles
   set average-population total-population / (count turtles - count power-plants)
   set %-unelectrified sum [people] of projects / sum [people] of turtles 
   set total-power-plants count power-plants
   set average-generator-size total-generation / total-power-plants
   ask power-plants [
     set generation-size max-capacity - available-capacity]
   
   
   
   
   if any? projects [
     set p-largest-potential max-one-of patches with [developed? = false] [largest-potential] 
     set biggest-population max-one-of projects [people]
     set p-biggest-population [best-patch] of biggest-population
     set p-jobs max-one-of patches with [developed? = false] [project-jobs]
     set p-inflows max-one-of patches with [developed? = false] [project-inflows]
     
     set lowest-project-cost min-one-of projects [project-cost]
     set p-lowest-cost [best-patch] of lowest-project-cost 
     set lowest-cost [project-cost] of lowest-project-cost
     
     set lowest-dg-project min-one-of projects [dg-cost]
     set lowest-dg-cost [dg-cost] of lowest-dg-project
     set lowest-dg-best-patch [best-patch] of lowest-dg-project
     
     set lowest-ge-project min-one-of projects [ge-cost]
     set lowest-ge-cost [ge-cost] of lowest-ge-project
     set lowest-ge-best-patch [best-patch] of lowest-ge-project
     
     set most-recent-plant max-one-of power-plants [who]
     set most-recent-patch-developed [patch-here] of most-recent-plant
     set electrification-method [connection-mode] of expansion-node
   ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-line-percentages
  ;This procedure takes the span of each grade of line and calculates the percentage of the total
  ; for each.  
  set %-33kv 100 * (sum [span] of links with [grade = 33] )/ sum [span] of links
  set %-138kv 100 * (sum [span] of links with [grade = 138] )/ sum [span] of links
  set %-230kv 100 * (sum [span] of links with [grade = 230]) / sum [span] of links
  set %-345kv 100 * (sum [span] of links with [grade = 345]) / sum [span] of links
  set %-500kv 100 * (sum [span] of links with [grade = 500] )/ sum [span] of links
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to create-population-centers
  ; This procedure makes a population center agent in the centroid of each district present in the
  ; district dataset.  It loads the accompanying data in the dataset of number of people and 
  ; sets the appropriate demand variables.  
  set darks-list []
  gis:set-drawing-color cyan
  gis:draw districts-dataset 1
  
  foreach gis:feature-list-of districts-dataset
    [ let centroid gis:location-of gis:centroid-of ?
      ; centroid will be an empty list if it lies outside the bounds
      ; of the current NetLogo world, as defined by our current GIS
      ; coordinate transformation
      if not empty? centroid
      [ create-darks 1
        [ set xcor item 0 centroid
          set ycor item 1 centroid
          set size 1
          set name gis:property-value ? "FIRST_DNAM"
          set baseline-people gis:property-value ? "Population"
          set people baseline-people
          set in-county gis:property-value ? "FIRST_FIRS"
          set network turtle-set self ; We initiate the "network" for this turtle, its secondary grid.  
          set supplier-list turtle-set nobody ;We initiate the supplier-list
          set transmission-links link-set nobody ;We initiate the link set of links to this turtle
          set shortest-path link-set nobody
          ifelse in-county = "Montserrado" ;The model ignores Monrovia and the Montserrado urban areas.    
          [ die ]
          [ set shape "house" set size 1
            ]
          set darks-list lput (self) darks-list ; Starts a list with all darks
          ]

        ]]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to set-patch-potentials
; This procedure stes the patches up with the name of their location and the energy potentials
; it owns based on each technology.  
  gis:apply-coverage countys-dataset "FIRST_FIRS" county ;This command applies the names of the countys in the datase to the patches variable "county".
                                                         ;If a patch lies in two counties it gets the name of the county that takes up most of the patches area.  
  ask patches with [not is-string? county]
  [set pcolor grey
    set developed? true]  ;; This takes all patches outside of the country of Liberia and makes then "developed" so that the agents dont use them in calculations.  
  set-biomass-potential
  set-solar-potential
  set-hydro-potential
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to set-biomass-potential
  ; This procedure takes the total biomass potential available in a county in dry tonnes per year and figures out the 
  ; average maximum potential in each patch of that county. The procedure
  ; converts it to kW of maximum dispatchable potential.  
  ; The procedure also sets the patches biomass-shed, which is the patch set of neighboring 
  ; patches that the particular patch can collect resources from to aggregate its biomass potential. 
  ; This has to happen since biomass can be moved limited distances without affecting the LCOE.  
  set bomi-biomass 88765 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set bomi-patches count patches with [county = "Bomi"]
  set bomi-average-biomass (bomi-biomass) / (8760 * bomi-patches * load-factor) 
  ask patches with [county = "Bomi"]
  [set biomass-base-potential bomi-average-biomass]
  
  set bong-biomass 1127053 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set bong-patches count patches with [county = "Bong"]
  set bong-average-biomass (bong-biomass) / (8760 * bong-patches * load-factor)
  ask patches with [county = "Bong"]
  [set biomass-base-potential bong-average-biomass]
  
  set grand-bassa-biomass 512567 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set grand-bassa-patches count patches with [county = "Grand Bassa"]
  set grand-bassa-average-biomass (grand-bassa-biomass) / (8760 * grand-bassa-patches * load-factor)
  ask patches with [county = "Grand Bassa"]
  [set biomass-base-potential grand-bassa-average-biomass]
  
  set grand-cape-mount-biomass 292417 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set grand-cape-mount-patches count patches with [county = "Grand Cape Mount"]
  set grand-cape-mount-average-biomass (grand-cape-mount-biomass) / (8760 * grand-cape-mount-patches * load-factor)
  ask patches with [county = "Grand Cape Mount"]
  [set biomass-base-potential grand-cape-mount-average-biomass]
  
  set grand-gedeh-biomass 138597 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set grand-gedeh-patches count patches with [county = "Grand Gedeh"]
  set grand-gedeh-average-biomass (grand-gedeh-biomass) / (8760 * grand-gedeh-patches * load-factor)
  ask patches with [county = "Grand Gedeh"]
  [set biomass-base-potential grand-gedeh-average-biomass]
  
  set grand-kru-biomass 78392 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set grand-kru-patches count patches with [county = "Grand Kru"]
  set grand-kru-average-biomass (grand-kru-biomass) / (8760 * grand-kru-patches * load-factor)
  ask patches with [county = "Grand Kru"]
  [set biomass-base-potential grand-kru-average-biomass]
  
  set lofa-biomass 497025 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set lofa-patches count patches with [county = "Lofa"]
  set lofa-average-biomass (lofa-biomass) / (8760 * lofa-patches * load-factor)
  ask patches with [county = "Lofa"]
  [set biomass-base-potential lofa-average-biomass]
  
  set margibi-biomass 679251 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set margibi-patches count patches with [county = "Margibi"]
  set margibi-average-biomass (margibi-biomass) / (8760 * margibi-patches * load-factor)
  ask patches with [county = "Margibi"]
  [set biomass-base-potential margibi-average-biomass]
  
  set maryland-biomass 336302 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set maryland-patches count patches with [county = "Maryland"]
  set maryland-average-biomass (maryland-biomass) / (8760 * maryland-patches * load-factor)
  ask patches with [county = "Maryland"]
  [set biomass-base-potential  maryland-average-biomass]
  
  set montserrado-biomass 2289024 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set montserrado-patches count patches with [county = "Montserrado"]
  set montserrado-average-biomass (montserrado-biomass) / (8760 * montserrado-patches * load-factor)
  ask patches with [county = "Montserrado"]
  [set biomass-base-potential montserrado-average-biomass]
  
  set nimba-biomass 2398052 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set nimba-patches count patches with [county = "Nimba"]
  set nimba-average-biomass (nimba-biomass) / (8760 * nimba-patches * load-factor)
  ask patches with [county = "Nimba"]
  [set biomass-base-potential nimba-average-biomass]
  
  set rivercess-biomass 99591 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set rivercess-patches count patches with [county = "Rivercess"]
  set rivercess-average-biomass (rivercess-biomass) / (8760 * rivercess-patches * load-factor)
  ask patches with [county = "Rivercess"]
  [set biomass-base-potential rivercess-average-biomass]
  
  set sinoe-biomass 316351 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set sinoe-patches count patches with [county = "Sinoe"]
  set sinoe-average-biomass (sinoe-biomass) / (8760 * sinoe-patches * load-factor)
  ask patches with [county = "Sinoe"]
  [set biomass-base-potential sinoe-average-biomass]
  
  set river-gee-biomass 170596 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set river-gee-patches count patches with [county = "River Gee"]
  set river-gee-average-biomass (river-gee-biomass) / (8760 * river-gee-patches * load-factor)
  ask patches with [county = "River Gee"]
  [set biomass-base-potential river-gee-average-biomass]
  
  set gbarpolu-biomass 407402 * 0.25 * biomass-heat-content * 277.8
  ;dry tonnes/yr * 0.25 efficiency * 17.5 Gj/ton * 277.8 kWh/Gj
  set gbarpolu-patches count patches with [county = "Gbarpolu"]
  set gbarpolu-average-biomass (gbarpolu-biomass) / (8760 * gbarpolu-patches * load-factor)
  ask patches with [county = "Gbarpolu"]
  [set biomass-base-potential gbarpolu-average-biomass]
  

ask patches [
    set biomass-shed (patch-set patches with [pxcor >= ([pxcor] of myself - 2) and pxcor <= ([pxcor] of myself + 2) 
        and pycor >= ([pycor] of myself - 2) and pycor <= ([pycor] of myself + 2)])
    set aggregated-biomass-potential sum [biomass-base-potential] of biomass-shed]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   

to set-solar-potential
  ; This procedure sets the solar potential in each patch in kW of potential 
  
  set hours-of-sun insolation ;4030 Wh/m2/day According to worst case month in liberia as reported by [1] European Commission -- Joint Research Centre -- 
                       ;Institute for Environment and Sustainability, “Photovoltaic Geographical InformatioN Systems.” 2015.
  ask patches with [developed? != true]   
  [set solar-base-potential ((round-trip-battery-efficiency * panel-efficiency * (1 - PV-system-losses) * area-of-patches * area-for-solar * insolation) /
    (morning-peak * duration-morning-peak + base-load * duration-base-load + duration-evening-peak )) ; gives solar potential in kw
    ] 
end    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  

to set-hydro-potential
  ; This procedure assigns hydro potential to the patches that have been identified as promising.  
  ; The Africa Energy Unit Report provides a map which was translated netlogo world patches.  
  ; The model ignores large potential sites (> 10 MW) since those are at the St John river and 
  ; Mount Coffee sites expected to be used for the Monrovia Grid.  They would also involve different
  ; infrastructure than the ones considered here.  
    
  set hydro-sites (list patch -16 12 patch -15 12 patch -13 12 patch -7 17 patch -5 18 patch -7 21 patch -5 23 patch -4 21 patch -2 22 patch -19 3 patch -18 3
    patch -18 4 patch -15 9 patch -13 3 patch -12 5 patch -9 6 patch -4 9 patch 9 10 patch -4 -2 patch -3 -1 patch 1 1 patch 4 2 patch 10 0 patch 9 -1 patch 7 -4 
    patch 6 -6 patch 4 -7 patch -3 -7 patch 10 -18 patch 13 -17 patch 20 -16 patch 12 -20 patch -9 1)
  set hydro-max-potentials (list 10000 10000 10000 10000 1000 1000 1000 1000 1000 10000 10000 10000 10000 10000 10000 10000 10000 1000 10000 10000 10000 10000
    1000 1000 1000 10000 10000 1000 1000 1000 1000 100 10000)
  
  (foreach hydro-sites hydro-max-potentials [
    ask ?1 [
      set hydro-base-potential ?2 * 0.3 ;We cut the maximum potential at each side by 70% as a way to control 
                                   ; demand and peak power.  This way we know for sure the potential will
                                   ;be enough for the demand even in the dry season.  

    ]
  ])
  ask patches [
    if hydro-base-potential = 0
    [set hydro-base-potential 0.001]]
end 
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-storage-needed
  set storage-needed (duration-evening-peak + duration-morning-peak * morning-peak + base-load * duration-base-load
    - hours-of-sun * base-load) / round-trip-battery-efficiency ; This provides needed storage in kWh/kW of installed capacity
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-load-factor
  ;This is the same as the load factor necessary for the electricity calculations.  
  set load-factor (base-load * duration-base-load + duration-evening-peak + duration-morning-peak * morning-peak) / 24
      ;This provides my daily electricity demand per kW of peak electricity in kWh/kW
  if load-factor >= 1 [
    user-message ("Load factor is greater than 1")]
  
  set solar-required-per-kw-demand (duration-evening-peak + duration-morning-peak * morning-peak + 
        base-load * (duration-base-load + (round-trip-battery-efficiency - 1) * hours-of-sun )) 
    / (hours-of-sun * ( 1 - PV-system-losses) * round-trip-battery-efficiency)
end  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-CRF
  ; This procedure sets all the capital recovery factor (CRF) parameters needed and 
  ;calculates a CRF for the world.  Notice, the CRF will not change until you load the world again. 
  ;This is done so that you can compare a full run of DG, GE, and other preferences under the same
  ;costs.  
  
  set discount-rate .12 ;The discount rate for LCOE calculations
  set taxes-insurance .03 ; The taxes and insurance for LCOE calculations
  set solar-lifespan 25 ;According to Irena
  set battery-lifespan 2.5 ; Modi in Liberia
  set biomass-lifespan 20 ;According to Irena
  set hydro-lifespan 30 ;According to Irena
  set transmission-lifespan 40 ; No reference
  set CRFsolar (discount-rate / ( 1 - (( 1 + discount-rate ) ^ (-1 * solar-lifespan)))) + taxes-insurance
  set CRFbatteries (discount-rate / ( 1 - (( 1 + discount-rate ) ^ (-1 * battery-lifespan)))) + taxes-insurance
  set CRFbiomass (discount-rate / ( 1 - (( 1 + discount-rate ) ^ (-1 * biomass-lifespan)))) + taxes-insurance
  set CRFhydro (discount-rate / ( 1 - (( 1 + discount-rate ) ^ (-1 * hydro-lifespan)))) + taxes-insurance
  set CRFtrans (discount-rate / ( 1 - (( 1 + discount-rate ) ^ (-1 * transmission-lifespan)))) + taxes-insurance
  set pl discount-rate / (discount-rate - 0.02)
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-technology-costs
  ; This procedure sets all the costs for the different renewable technologies considered
  ;The level of data available forces us to use triangular distributions around the averages
  ;minium an maximum of each parameter.  As with CRF the costs are only calculated when the
  ;world is loaded allowing us to compare preferences in the same world.  
  
  ;; WE create a triangular distribution for solar capital-cost
  let z random-float 1
  ifelse z >= .5
  [ set solar-capital 4100 - (((1 - z) * 1400 * 700) ^ .5)]
  [ set solar-capital 2700 + ((z * 1400 * 700)^ .5) ]

;; WE create a triangular distribution for solar om
  let a random-float 1
  ifelse a >= .5
  [ set solar-o&m 14.02 - (((1 - a) * 4.68 * 2.34) ^ .5)]
  [ set solar-o&m 9.34 + ((a * 4.68 * 2.34)^ .5) ]
  
;; WE create a triangular distribution for hydro capital cost
  let b random-float 1
  ifelse b >= .5
  [ set hydro-capital 2400 - (((1 - b) * 800 * 400) ^ .5)]
  [ set hydro-capital 1600 + ((b * 800 * 400)^ .5) ]

;; WE create a triangular distribution for hydro om
  let c random-float 1
  ifelse c >= .5
  [ set hydro-o&m .04 - (((1 - b) * .03 * .015) ^ .5)]
  [ set hydro-o&m .01 + ((b * .03 * .015)^ .5) ]
  
;; WE create a triangular distribution for hydro duty factor
  let d random-float 1
  ifelse d >= .72
  [ set hydro-duty-factor .60 - (((1 - d) * .40 * .20) ^ .5)]
  [ set hydro-duty-factor .20 + ((b * .40 * .20)^ .5) ]
  
;; WE create a triangular distribution for biomass capital cost
  let f random-float 1
  ifelse f >= .5
  [ set biomass-capital 4100 - (((1 - f) * 1500 * 750) ^ .5)]
  [ set biomass-capital 2600 + ((f * 1500 * 750)^ .5) ]

;; WE create a triangular distribution for bio om
  let g random-float 1
  ifelse g >= .5
  [ set biomass-o&m 77.34 - (((1 - g) * 25.74 * 12.89) ^ .5)]
  [ set biomass-o&m 51.56 + ((b * 25.74 * 12.85)^ .5) ]

;; WE create a triangular distribution for biomass fuel cost
  let h random-float 1
  ifelse h >= .5
  [ set biomass-fuel 3 - (((1 - h) * 2 * 1) ^ .5)]
  [ set biomass-fuel 1 + ((h * 2 * 1)^ .5) ]
  
;; WE create a triangular distribution for biomass duty factor
  let i random-float 1
  ifelse i >= .5
  [ set biomass-duty-factor .90 - (((1 - i) * .20 * .10) ^ .5)]
  [ set biomass-duty-factor .70 + ((i * .20 * .10)^ .5) ]
  
  set biomass-heat-content 17.5 ;Gj/ton
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-project-patch
;; This procedure picks that patch where a new power-plant will be built by the Observer. 
;; The patch is selected according to the choice of the user for the Observer preference rules.    
  
  if decision-strategy = "multiple-resource"  
  [
    ask projects [calculate-lcoe-dg]
    
    set initial-population min-one-of projects [dg-cost]  
    set project-patch [best-patch] of initial-population
      ;We pick the patch for the cheapest project
    ]
  
  ;.............................................................
  
  
  if decision-strategy = "central-resource"
  ;; The patch will be the one with the maximum resource-centrality 
  [calculate-resource-centrality
    set p-central max-one-of patches with [developed? = false ] [resource-centrality]
  set project-patch max-one-of patches with [developed? = false ] [resource-centrality]
  set initial-population nobody]
  
  
  ;.............................................................
  
  
  
  if decision-strategy = "largest-resource"
  ;; Picks the largest resource patch
  [ask patches with [developed? = false] [update-potentials]
    set project-patch max-one-of patches with [developed? = false ] [largest-potential]
    
    ;This is a tie breaker if there is more than one patch with largest potential being solar
    if [source] of project-patch = "solar" [
      set initial-population max-one-of projects [people] 
    
      set project-patch patch ([xcor] of initial-population) ([ycor] of initial-population) 
    ] 
    
    ;This is a tie breaker in case there are more than on patch with the same max hydro potential
    if [source] of project-patch = "hydro" [
      set patchlist nobody
      set patchlist patches with [developed? = false and source = "hydro" and hydro-potential = [hydro-potential] of project-patch]
       ask patchlist [
        set closest-project min-one-of projects [distance myself]
        set priority [people] of closest-project / distance closest-project]
       set project-patch max-one-of patchlist [priority] 
    ]
       
    ;This is a tie breaker in case there are more than on patch with the same max biomass potential
    if [source] of project-patch = "biomass" [
      set patchlist nobody
      set patchlist patches with [developed? = false and source = "biomass" and aggregated-biomass-potential = [aggregated-biomass-potential] of project-patch]
       ask patchlist [
        set closest-project min-one-of projects [distance myself]
        set priority [people] of closest-project / distance closest-project]
       set project-patch max-one-of patchlist [priority] 
    ]
    set initial-population nobody]
  
  ;.............................................................
  
  
  
  if decision-strategy = "largest-population"
  ;; Picks a patch chosen by the larges population in the world
  [set initial-population max-one-of projects [people]
    ask initial-population
    [calculate-lcoe-dg
      set project-patch [best-patch] of initial-population
      ;; The largest population picks the best patch considering distance and power potential
    ]]
  
  ;.............................................................
  
  
  
  if decision-strategy = "largest-inflows"
  [ask projects [calculate-dg-flows]
    
    set initial-population max-one-of projects [dg-flows]  
    set project-patch [best-patch] of initial-population
    ]
    
    
  ;.............................................................
  
  if decision-strategy = "most-jobs" [
  
  ask projects [calculate-dg-jobs]
    
    set initial-population max-one-of projects [dg-jobs]  
    set project-patch [best-patch] of initial-population
    ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to initiate-grid
;; This procedure creates the first connection for a new power plant  
  ask project-patch [
    create-power-plant
    ask power-plants-here [
      ifelse initial-population != nobody 
      [set expansion-node initial-population]
      [set expansion-node min-one-of projects [distance myself]]
      ask expansion-node [set closest-socket myself]
    ]]
  ask expansion-node [connect-project]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-resource-centrality
;  This procedure calculates the resource centrality of the patches.  Resource Centrality
; considers the distance of the patch to all people in the world and the resources
; available at the patch. Then it combines it in an index to show the pactches potential 
; compared to its distance to a lot of people.     
  
  ask patches with [developed? = false] [
    set distance-list []
    set demand-list []
    set ci-list []
    foreach darks-list [
      set distance-list lput (distance ? ) distance-list ;Puts the distance to every dark agent in the list
      set demand-list lput ( [remaining-demand] of ? ) demand-list ; Puts the remaining demand of every project in the list
    ]
    
    set ci-list ( map / distance-list demand-list ) ;Divides the lists above
    set centrality sum ci-list ; Sets the centrality value as the sum of every item in the list.  In other words
                               ; It sums the distance of the agent to all demand in the world
    set resource-centrality largest-potential / centrality ; The ratio of resource potential to the centrality
    

  ]
  set largest-centrality-group max-n-of 5 patches [resource-centrality]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-lcoe-dg
;  This procedure is where populations calculate their decentralized generation cost. 
; Each population calculates what would be the cost to establish a production unit in each
; patch available while also considering its own demand the available potential in the patch 
; and the distance to the patch. The turtle sets the patch that results in the lowest lcoe cost
; as its best patch and the technology in the patch as its possible source.  

  ask patches with [developed? = false]
    [calculate-possible-transmission-capital-cost
      set dg-solar-cost-per-kw (solar-required-per-kw-demand * solar-capital * CRFsolar / (load-factor * 8760)) + possible-solar-transmission-cost 
      + battery-capital * CRFbatteries * storage-needed / (load-factor * 8760) 
      + (solar-o&m / (load-factor * 8760))
      
      set dg-biomass-cost-per-kw (biomass-capital * CRFbiomass / (load-factor * 8760)) + possible-biomass-transmission-cost + (biomass-o&m / (load-factor * 8760))
         + ((Pl * biomass-fuel) / (biomass-heat-content * .25 * 278 )) ;assumes 30% efficiency for biomass and 278 is a conversion factor for Gj to kWh
      
      if hydro-potential = 0 [set hydro-potential .001] 
      set dg-hydro-cost-per-kw (hydro-capital * CRFhydro / (8760 * load-factor)) + possible-hydro-transmission-cost + 
         (hydro-o&m / (load-factor * 8760)) 
         
        
      if dg-solar-cost-per-kw < dg-biomass-cost-per-kw and dg-solar-cost-per-kw < dg-hydro-cost-per-kw
      [set dg-cost-per-kw dg-solar-cost-per-kw
        set source "solar"]
      if dg-biomass-cost-per-kw < dg-solar-cost-per-kw and dg-biomass-cost-per-kw < dg-hydro-cost-per-kw
      [set dg-cost-per-kw dg-biomass-cost-per-kw
        set source "biomass"]
      if dg-hydro-cost-per-kw < dg-solar-cost-per-kw and dg-hydro-cost-per-kw < dg-biomass-cost-per-kw
      [set dg-cost-per-kw dg-hydro-cost-per-kw
        set source "hydro"]
      
    ]
    
  set best-patch min-one-of patches with [developed? = false] [dg-cost-per-kw]
  
  ifelse best-patch != nobody
  ;; If the turtle did find a patch then that patch cost becomes its dg-cost
  [set dg-cost [dg-cost-per-kw] of best-patch
    set technology [source] of best-patch]
  ;; If it didnt then the cost is 0 which is dealt with in the pick-connection-mode procedure
  [set dg-cost 0] 
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-project-inflows
;  This procedure calculates the amount of yearly economic flows that are created.  
; Values for the economic flows of each technology come from literature.  The procedure
; sets the possible flows to the maximum of the flows created by the technologies and the source
; technology as the one that created the maximum flows.   
set solar-inflows solar-potential * 0.0091 * 8760 * load-factor;this provides the inflows in $/year
set biomass-inflows aggregated-biomass-potential * 0.0368 * 8760 * load-factor

ifelse solar-inflows > biomass-inflows
[set project-inflows solar-inflows
  set source "solar"]
[set project-inflows biomass-inflows
  set source "biomass"]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-jobs
  ;  This procedure calculates the number of job years that are possible for the patch.  
; Values for the job years of each technology come from literature.  The procedure
; sets the possible job years to the maximum created by the technologies and the source
; technology as the one that created the maximum jobs.
set solar-jobs solar-potential * 8760 * load-factor * 0.87 / 1000000 ;this provides the numnber of job years for the project
set biomass-jobs aggregated-biomass-potential * 8760 * load-factor * 0.21 / 1000000
set hydro-jobs hydro-potential * 8760 * load-factor * 0.27 / 1000000

if solar-jobs > biomass-jobs and solar-jobs > hydro-jobs
[set project-jobs solar-jobs
  set source "solar"]
if biomass-jobs > solar-jobs and biomass-jobs > hydro-jobs
[set project-jobs biomass-jobs
  set source "biomass"]
if hydro-jobs > solar-jobs and hydro-jobs > biomass-jobs
[set project-jobs hydro-jobs
  set source "hydro"]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to create-power-plant
; This procedure creates a power plant at a chosen project patch  
    sprout-power-plants 1 [ 
      set size 1.5
      set supplier self
      if source = "solar"
      [set max-capacity solar-potential
      set technology "solar"
      set shape "sun"
      set color 15
      ]
      
      if source = "biomass"
      ; In the case of biomass we need to make sure that all patches that are providing
      ;resources to the new plant have their biomass and aggregated biomass potentials set
      ;to zero to avoid double counting of those potentials.  
      [set max-capacity aggregated-biomass-potential
      set technology "biomass"
      set shape "plant"
      set color 65
      ask biomass-shed [
        set biomass-potential 0
        set aggregated-biomass-potential 0]
      ]
      
      if source = "hydro"
      [set max-capacity hydro-potential
      set technology "hydro"
      set shape "drop"
      set color 97]
      
      
      set developed? true
      set available-capacity max-capacity
      set populations-serviced []
      set network turtle-set self
      set transmission-links link-set nobody
      set shortest-path link-set nobody
    ]    
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to connect-project 
; This procedure is where the expansion-node and the socket 
;actually get connected to each other.  Then all variables 
;are updated.    
  let node1 expansion-node
  let node2 [supplier] of closest-socket
  
  create-link-with closest-socket
    [
    ifelse [remaining-demand] of node1 < [available-capacity] of node2
      [set load-carried [remaining-demand] of node1]
      [set load-carried [available-capacity] of node2]
      
    set-link-properties
    set line-list lput self line-list]
  
  set supplier [supplier] of closest-socket
  set technology [technology] of supplier
    
    
    ifelse [available-capacity] of supplier >= remaining-demand
    ;; This happens if the supplier has enough capacity for the demand. 
    ;; The dark will now be an electrified and the available-capacity of the 
    ;; power-plant is lowered
      [ask supplier[
        set available-capacity available-capacity - [remaining-demand] of expansion-node
        if technology = "solar" 
        [set capital-cost capital-cost + ([remaining-demand] of expansion-node * solar-capital / load-factor)
          + battery-capital * ([remaining-demand] of expansion-node / load-factor)
          * (((morning-peak * duration-morning-peak + base-load * duration-base-load + duration-evening-peak)/ round-trip-battery-efficiency) - hours-of-sun * base-load)]
        if technology = "biomass" 
        [set capital-cost capital-cost + [remaining-demand] of expansion-node * biomass-capital / load-factor]
        if technology = "hydro" 
        [set capital-cost capital-cost + [remaining-demand] of expansion-node * hydro-capital / load-factor]
        set network (turtle-set network [network] of myself) 
        set transmission-links (link-set [my-links] of network)
        ask network [set network [network] of myself                  ;This ensures the connected turtles                                
          set transmission-links (link-set [my-links] of network) ]]  ; all have the same network and the same links
      ask shortest-path [
        set load-carried load-carried + [remaining-demand] of myself]
      upgrade-path
      set breed electrifieds
      set darks-list remove expansion-node darks-list ;; Here we remove the agent from the darks list
      set shape "house"
      set color yellow
      set supplier-list (turtle-set supplier-list supplier) 
      set remaining-demand 0
      ]
    
    ;; This happens if the supplier does not have enough capacity.  The dark will 
    ;; become an incomplete, lower its demand and the power-plant places its capacity to zero  
      [
        ask shortest-path [
        set load-carried load-carried + [available-capacity] of [supplier] of myself]
        upgrade-path
        ask supplier [
        if technology = "solar" 
        [set capital-cost capital-cost + (available-capacity * solar-capital / load-factor)
          + battery-capital * (available-capacity / load-factor)
          * (((morning-peak * duration-morning-peak + base-load * duration-base-load + duration-evening-peak)/ round-trip-battery-efficiency) - hours-of-sun * base-load)]
        if technology = "biomass" 
        [set capital-cost capital-cost + available-capacity * biomass-capital / load-factor]
        if technology = "hydro" 
        [set capital-cost capital-cost + available-capacity * hydro-capital / load-factor]
        set network (turtle-set network [network] of myself)
        set transmission-links (link-set my-links)
        ask network [set network [network] of myself                  ;This ensures the connected turtles 
          set transmission-links (link-set [my-links] of network)]]   ; all have the same network and the same links
      set remaining-demand remaining-demand - [available-capacity] of supplier
      set breed incompletes
      set color cyan
      set supplier-list (turtle-set supplier-list supplier)
      
      ask supplier [set available-capacity 0]
      ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-possible-transmission-capital-cost
  ;; This procedure is different from the calculate-transmisstion-capital-cost because it only applies to the 
  ;; hypothetical situation where a patch would be connected to a possible socket.  Because of that patches own the 
  ;; possible-transmission-cost as opposed to transmission-capital-cost which is owned by populations.  
  
  ; We need to check which is lower the remaining demand of the project or the available capacity of the patch.  This also means
  ; the model needs to check each potential to calculate the appropriate costs for transmission.  
  
  set possible-transmission-cost-list []
  set dg-possible-jobs-list []
  set dg-possible-flows-list []
  (foreach (list (solar-potential) (aggregated-biomass-potential) (hydro-potential) )
    (list (solar-required-per-kw-demand) 1 1) ;We add this multiplier because 1kW of demand needs more than 1kW of solar installed
    (list (solar-capital) (biomass-capital) (hydro-capital))
    (list (solar-job-factor) (biomass-job-factor) (hydro-job-factor))
    (list (solar-economic-factor) (biomass-economic-factor) (hydro-economic-factor))
    (list (battery-capital) 0 0)
  
 [
   ifelse [remaining-demand] of myself < ?1
  
   [if distance myself <= 8.45
  ;if the distance is less than 80 km
  ;we check loadability
     [if [remaining-demand] of myself < 14500 ;this is an assumed end I need to double check
       [set line-cost cost-of-33kv];use 33 kV
     if [remaining-demand] of myself >= 14500 and [remaining-demand] of myself < 156000 
     [set line-cost cost-of-138kv];use 138 kV
     if [remaining-demand] of myself >= 156000 and [remaining-demand] of myself < 435000
     [set line-cost cost-of-230kv];use 230 kV
     if [remaining-demand] of myself >= 435000 and [remaining-demand] of myself < 1275000
     [set line-cost cost-of-345kv];use 345 kV
     if [remaining-demand] of myself >= 1275000 
     [set line-cost cost-of-500kv];use 500 kV
     ]
    
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ; between 80 and 100 km
  if distance myself > 8.45 and distance myself < 10.57
  [
    if [remaining-demand] of myself < 143000 
    [set line-cost cost-of-138kv];use 138 kV
    if [remaining-demand] of myself >= 143000 and [remaining-demand] of myself < 398750 
    [set line-cost cost-of-230kv];use 230 kV
    if [remaining-demand] of myself >= 398750 and [remaining-demand] of myself < 1168750
    [set line-cost cost-of-345kv];use 345 kV
    if [remaining-demand] of myself >= 1168750 
    [set line-cost cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 100 and 200 km
  if distance myself >= 10.57 and distance myself < 21.14
  [
    if [remaining-demand] of myself < 117000 
    [set line-cost cost-of-138kv];use 138 kV
    if [remaining-demand] of myself >= 117000 and [remaining-demand] of myself < 326250 
    [set line-cost cost-of-230kv];use 230 kV
    if [remaining-demand] of myself >= 326250 and [remaining-demand] of myself < 956250
    [set line-cost cost-of-345kv];use 345 kV
    if [remaining-demand] of myself >= 956250 
    [set line-cost cost-of-500kv];use 500 kV
  ]
   
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 200 and 300 km
  if distance myself >= 21.14 and distance myself < 31.71
  [
    if [remaining-demand] of myself < 91000 
    [set line-cost cost-of-138kv];use 138 kV
    if [remaining-demand] of myself >= 91000 and [remaining-demand] of myself < 253750 
    [set line-cost cost-of-230kv];use 230 kV
    if [remaining-demand] of myself >= 253750 and [remaining-demand] of myself < 743750
    [set line-cost cost-of-345kv];use 345 kV
    if [remaining-demand] of myself >= 743750
    [set line-cost cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if distance myself >= 31.71 and distance myself < 42.28
  [
    if [remaining-demand] of myself < 67600 
    [set line-cost cost-of-138kv];use 138 kV
    if [remaining-demand] of myself >= 67600 and [remaining-demand] of myself < 188500 
    [set line-cost cost-of-230kv];use 230 kV
    if [remaining-demand] of myself >= 188500 and [remaining-demand] of myself < 552500
    [set line-cost cost-of-345kv];use 345 kV
    if [remaining-demand] of myself >= 552500
    [set line-cost cost-of-500kv];use 500 kV
  ]
   
 ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if distance myself >= 42.28
  [
    if [remaining-demand] of myself < 57200
    [set line-cost cost-of-138kv];use 138 kV
    if [remaining-demand] of myself >= 57200 and [remaining-demand] of myself < 159500 
    [set line-cost cost-of-230kv];use 230 kV
    if [remaining-demand] of myself >= 159500 and [remaining-demand] of myself < 467500
    [set line-cost cost-of-345kv];use 345 kV
    if [remaining-demand] of myself >= 467500
    [set line-cost cost-of-500kv];use 500 kV
  ]   
    
    let x (distance myself * line-cost * 9.46 * CRFtrans) / ([remaining-demand] of myself * load-factor * 24 * 365)
    set possible-transmission-cost-list lput x possible-transmission-cost-list
    
    let y ([remaining-demand] of myself * load-factor * 24 * 365) * ?4 / 
    (([remaining-demand] of myself * (?2 * ?3 + storage-needed * ?6)) + (distance myself * line-cost * 9.46)) 
    ;Gives units of jobs generated per $ invested 
    set dg-possible-jobs-list lput y dg-possible-jobs-list
    
    let z (([remaining-demand] of myself * load-factor * 24 * 365) * ?5) / (([remaining-demand] of myself * ?2 * ?3) + (distance myself * line-cost * 9.46))
    ;Gives units of $ generated yearly / $ invested
    set dg-possible-flows-list lput z dg-possible-flows-list
    
    ]
  
  
  
  
  
  
  ;;;;;Otherwise
  
   [if distance myself <= 8.45
     ;if the distance is less than 80 km
     ;we check loadability
     [if ?1 < 14500 ;this is an assumed end I need to double check
       [set line-cost cost-of-33kv];use 33 kV
     if ?1 >= 14500 and ?1 < 156000 
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 156000 and ?1 < 435000
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 435000 and ?1 < 1275000
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 1275000 
     [set possible-transmission-cost cost-of-500kv];use 500 kV
     ]
   
   ;otherwise we must use higher voltage lines
   ;but we must check the loadability of the lines
   ; between 80 and 100 km
   if distance myself > 8.45 and distance myself < 10.57
   [
     if ?1 < 143000 
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 143000 and ?1 < 398750 
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 398750 and ?1 < 1168750
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 1168750 
     [set possible-transmission-cost cost-of-500kv];use 500 kV
   ]
   
   ;otherwise we must use higher voltage lines
   ;but we must check the loadability of the lines
   ;between 100 and 200 km
   if distance myself >= 10.57 and distance myself < 21.14
   [
     if ?1 < 117000 
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 117000 and ?1 < 326250 
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 326250 and ?1 < 956250
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 956250 
     [set possible-transmission-cost cost-of-500kv];use 500 kV
   ]
   
   ;otherwise we must use higher voltage lines
   ;but we must check the loadability of the lines
   ;between 200 and 300 km
   if distance myself >= 21.14 and distance myself < 31.71
   [
     if ?1 < 91000 
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 91000 and ?1 < 253750 
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 253750 and ?1 < 743750
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 743750
     [set possible-transmission-cost cost-of-500kv];use 500 kV
   ]
   
   ;otherwise we must use higher voltage lines
   ;but we must check the loadability of the lines
   ;between 300 and 400 km
   if distance myself >= 31.71 and distance myself < 42.28
   [
     if ?1 < 67600 
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 67600 and ?1 < 188500 
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 188500 and ?1 < 552500
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 552500
     [set possible-transmission-cost cost-of-500kv];use 500 kV
   ]
   
   ;otherwise we must use higher voltage lines
   ;but we must check the loadability of the lines
   ;between 300 and 400 km
   if distance myself >= 42.28
   [
     if ?1 < 57200
     [set possible-transmission-cost cost-of-138kv];use 138 kV
     if ?1 >= 57200 and ?1 < 159500 
     [set possible-transmission-cost cost-of-230kv];use 230 kV
     if ?1 >= 159500 and ?1 < 467500
     [set possible-transmission-cost cost-of-345kv];use 345 kV
     if ?1 >= 467500
     [set possible-transmission-cost cost-of-500kv];use 500 kV
   ]  
   if ?1 = 0 [ set ?1 ?1  + 0.01] 
   let x (distance myself * line-cost * 9.46 * CRFtrans) / (?1 * load-factor * 24 * 365)
   set possible-transmission-cost-list lput x possible-transmission-cost-list
       
    let y (?1 * load-factor * 24 * 365 * ?4) / (?1 * (?2 * ?3 + storage-needed * ?6 ) + (distance myself * line-cost * 9.46))
    ;Gives units of jobs generated per $ invested 
    set dg-possible-jobs-list lput y dg-possible-jobs-list
    
    let z (?1 * load-factor * 24 * 365 * ?5) / ((?1 * ?2 * ?3) + (distance myself * line-cost * 9.46))
    ;Gives units of $ generated yearly / $ invested
    set dg-possible-flows-list lput z dg-possible-flows-list
   
   ]
 ]
 )
 set possible-solar-transmission-cost item 0 possible-transmission-cost-list
 set possible-biomass-transmission-cost item 1 possible-transmission-cost-list
 set possible-hydro-transmission-cost item 2 possible-transmission-cost-list
 
 set dg-solar-jobs item 0 dg-possible-jobs-list
 set dg-biomass-jobs item 1 dg-possible-jobs-list
 set dg-hydro-jobs item 2 dg-possible-jobs-list
 
 set dg-solar-flows item 0 dg-possible-flows-list
 set dg-biomass-flows item 1 dg-possible-flows-list
 set dg-hydro-flows item 2 dg-possible-flows-list
end 


  
  
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to set-link-properties
;;In this procedure the links get their appropriate initial grades and costs
 if link-length <= 8.46
  ;if the distance is less than 80 km then we check the line for loadability
  [if load-carried < 14500 
    [set-33kv]  
  if load-carried >= 14500 and load-carried < 156000 
      [set-138kv]
  if load-carried >= 156000 and load-carried < 435000
    [set-230kv]
  if load-carried >= 435000
    [set-345kv]
  ]
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ; between 80 and 100 km
  if link-length > 8.46 and link-length < 10.57
  [if load-carried < 143000 
    [set-138kv]  
  if load-carried >= 143000 and load-carried < 398750 
      [set-230kv]
  if load-carried >= 398750 and load-carried < 1168750
    [set-345kv]
  if load-carried >= 1168750
    [set-500kv]
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 100 and 200 km
  if link-length >= 10.57 and link-length < 21.14
  [
    if load-carried < 117000 
    [set-138kv];use 138 kV
    if load-carried >= 117000 and load-carried < 326250 
    [set-230kv];use 230 kV
    if load-carried >= 326250 and load-carried < 956250
    [set-345kv];use 345 kV
    if load-carried >= 956250
    [set-500kv];use 500 kV
  ]
   
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 200 and 300 km
  if link-length >= 21.14 and link-length < 31.71
  [
    if load-carried < 91000 
    [set-138kv];use 138 kV
    if load-carried >= 91000 and load-carried < 253750 
      [set-230kv];use 230 kV
    if load-carried >= 253750 and load-carried < 743750
    [set-345kv];use 345 kV
    if load-carried >= 743750
    [set-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if link-length >= 31.71 and link-length < 42.28
  [
    if load-carried < 67600 
    [set-138kv];use 138 kV
    if load-carried >= 67600 and load-carried < 188500 
      [set-230kv];use 230 kV
    if load-carried >= 188500 and load-carried < 552500
    [set-345kv];use 345 kV
    if load-carried >= 552500
    [set-500kv];use 500 kV
  ]
   
 ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;above 400 km
  if link-length >= 42.28
  [
    if load-carried < 57200
    [set-138kv];use 138 kV
    if load-carried >= 57200 and load-carried < 159500 
    [set-230kv];use 230 kV
    if load-carried >= 159500 and load-carried < 467500
    [set-345kv];use 345 kV
    if load-carried >= 467500
    [set-500kv];use 500 kV
  ]   
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Here we actually give the links their different properties according to their grade

to set-33kv
  set grade 33
  set cost-km cost-of-33kv
  set color 89.9 
  set span link-length
end 

to set-138kv
  set grade 138
  set cost-km cost-of-138kv
  set span link-length
  set color 65
end 

to set-230kv
 set grade 230
 set cost-km cost-of-230kv
 set span link-length
 set color 45   
end 

to set-345kv
  set grade 345
  set cost-km cost-of-345kv
  set span link-length
  set color 25
end 

to set-500kv
  set grade 500
  set cost-km cost-of-500kv
  set span link-length
  set color 15
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to upgrade-path 
  ; This procedure asks the links in the shortest path between a new project and the 
  ;generation unit that will serve it to upgrade according to the loadability tests
  ;Because the links were created with appropriate grades according to their distance and 
  ;loads the upgrade must occur only if the load is higher than the highest load 
  ask shortest-path [
    if span <= 8.46
    ;if the distance is less than 80 km then we check the line for loadability
    [if load-carried < 14500 
      [set-33kv]  
    if load-carried >= 14500 and load-carried < 156000 
      [set-138kv]
    if load-carried >= 156000 and load-carried < 435000
    [set-230kv]
    if load-carried >= 435000
    [set-345kv]
    ]
    ;otherwise we must use higher voltage lines
    ;but we must check the loadability of the lines
    ; between 80 and 100 km
    if span > 8.46 and span < 10.57
    [if load-carried < 143000 
      [set-138kv]  
    if load-carried >= 143000 and load-carried < 398750 
      [set-230kv]
    if load-carried >= 398750 and load-carried < 1168750
    [set-345kv]
    if load-carried >= 1168750
    [set-500kv]
    ]
  
    ;otherwise we must use higher voltage lines
    ;but we must check the loadability of the lines
    ;between 100 and 200 km
    if span >= 10.57 and span < 21.14
  [
    if load-carried < 117000 
    [set-138kv];use 138 kV
    if load-carried >= 117000 and load-carried < 326250 
    [set-230kv];use 230 kV
    if load-carried >= 326250 and load-carried < 956250
    [set-345kv];use 345 kV
    if load-carried >= 956250
    [set-500kv];use 500 kV
  ]
   
    ;otherwise we must use higher voltage lines
    ;but we must check the loadability of the lines
    ;between 200 and 300 km
    if span >= 21.14 and span < 31.71
    [
      if load-carried < 91000 
      [set-138kv];use 138 kV
      if load-carried >= 91000 and load-carried < 253750 
      [set-230kv];use 230 kV
      if load-carried >= 253750 and load-carried < 743750
      [set-345kv];use 345 kV
      if load-carried >= 743750
      [set-500kv];use 500 kV
    ]
  
    ;otherwise we must use higher voltage lines
    ;but we must check the loadability of the lines
    ;between 300 and 400 km
    if span >= 31.71 and span < 42.28
    [
      if load-carried < 67600 
      [set-138kv];use 138 kV
      if load-carried >= 67600 and load-carried < 188500 
      [set-230kv];use 230 kV
      if load-carried >= 188500 and load-carried < 552500
      [set-345kv];use 345 kV
      if load-carried >= 552500
      [set-500kv];use 500 kV
    ]
   
    ;otherwise we must use higher voltage lines
    ;but we must check the loadability of the lines
    ;above 400 km
    if span >= 42.28
    [
      if load-carried < 57200
      [set-138kv];use 138 kV
      if load-carried >= 57200 and load-carried < 159500 
      [set-230kv];use 230 kV
      if load-carried >= 159500 and load-carried < 467500
      [set-345kv];use 345 kV
      if load-carried >= 467500
      [set-500kv];use 500 kV
    ]
  ]   
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-connection-mode
  
  if decision-strategy = "multiple-resource" [
    pick-lcoe-based-connection-mode] 
  
  if decision-strategy = "most-jobs" [
    pick-job-based-connection-mode] 

  if decision-strategy = "largest-inflows" [
    pick-flow-based-connection-mode]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-lcoe-based-connection-mode
  
 ;; In this procedure all project populations decide what is 
;; cheapest for them. This procedure was called by asking the projects   
  calculate-lcoe-dg  ;Calculate how much a dg project costs
  calculate-lcoe-ge  ;Calculate how much a ge project costs
  
  ifelse dg-cost > ge-cost
  ;; If it is cheaper to do ge then it decides to do that
  [set connection-mode "ge"
    set project-cost ge-cost]
  ;; Otherwise it does dg
  [set connection-mode "dg"
    set project-cost dg-cost]
  
  ;; If the grid extension cost is 0 then there is no grid project 
  ;; and it must do dg.  
  if ge-cost = 0 
  [set connection-mode "dg"
    set project-cost dg-cost]
  
  ;; If the project cost is still 0 then there are no more options for this
  ;; project and it is impossible for the turtle to be supplied.  
  if project-cost = 0
   [set breed unmets
     set color blue] 
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-job-based-connection-mode
  calculate-dg-jobs  ;Calculate how many jobs a dg project generates
  calculate-ge-jobs  ;Calculate how many jobs a ge project generates
  
  ifelse dg-jobs > ge-jobs
  ;; If it is cheaper to do ge then it decides to do that
  [set connection-mode "dg"
    set max-jobs dg-jobs]
  ;; Otherwise it does dg
  [set connection-mode "ge"
    set max-jobs ge-jobs]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-flow-based-connection-mode
  calculate-dg-flows  ;Calculate how many flows a dg project generates
  calculate-ge-flows  ;Calculate how many flows a ge project generates
  
  ifelse dg-flows > ge-flows
  ;; If it is better to do dg then it decides to do that
  [set connection-mode "dg"
    set max-flows dg-flows]
  ;; Otherwise it does dg
  [set connection-mode "ge"
    set max-flows ge-flows]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;






;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This procedure calculates the amount of each technology used for generation and sets it in 
;percentage of total generation to define the generation portfolio

to calculate-generation-portfolio
  set solar-installed-capacity (sum [max-capacity - available-capacity] of power-plants with [technology = "solar"])
  set %-solar-gen solar-installed-capacity / total-generation 
  set %-of-solar-used 100 * solar-installed-capacity / sum [solar-potential] of patches
  
  set biomass-installed-capacity (sum [max-capacity - available-capacity] of power-plants with [technology = "biomass"])
  set %-biomass-gen biomass-installed-capacity / total-generation
  set %-of-biomass-used 100 * biomass-installed-capacity / sum [biomass-base-potential] of patches
  
  set hydro-installed-capacity (sum [max-capacity - available-capacity] of power-plants with [technology = "hydro"])
  set %-hydro-gen hydro-installed-capacity / total-generation
  set %-of-hydro-used 100 * hydro-installed-capacity / sum [hydro-potential] of patches 
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-total-economic-inflows
  ;This procedure calculates the economic inflows theoretically created by the different renewable
  ;energy production units.  The source of the economic inflows comes from literature: Buchholz, Thomas, and Izael Da Silva. 
  ;“Potential of Distributed Wood-Based Biopower Systems Serving Basic Electricity Needs in Rural Uganda.” 
  ;Energy for Sustainable Development 14 (2010): 56–61. Web. 15 Oct. 2012.  They assume that flows from a large hydro is 0 and
  ;I assume it is the same in the case of mini-hydro but I need to double check the assumption.
  ; The results are in Million $  
  set total-economic-inflows (sum [max-capacity - available-capacity] of power-plants with [source = "biomass"] * 0.0368 * load-factor) +
    (sum [max-capacity - available-capacity] of power-plants with [source = "solar"] * 0.0091 * load-factor)
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-job-years
  ;This procedure calculates the economic inflows theoretically created by the different renewable
  ;energy production units.  The source of the economic inflows comes from literature:  Wei, Max, Shana Patadia, 
  ;and Daniel M. Kammen. “Putting Renewables and Energy Efficiency to Work: How Many Jobs Can the Clean Energy 
  ;Industry Generate in the US?” Energy Policy 38 (2010): 919–931. Web. 11 Feb. 2014.
  set job-years ((sum [max-capacity - available-capacity] of power-plants with [source = "biomass"] * 0.21 * biomass-lifespan) +
    (sum [max-capacity - available-capacity] of power-plants with [source = "solar"] * 0.88 * solar-lifespan) +
    (sum [max-capacity - available-capacity] of power-plants with [source = "hydro"] * 0.28 * hydro-lifespan)) * 8760 * load-factor / (10 ^ 6)
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This procedure counts the number of power production units in each of the counties

to report-locations
 ask power-plants
 [ set location county]
 set Bomi-pp count power-plants with [location = "Bomi"] 
 set Bong-pp count power-plants with [location = "Bong"]
 set Grand-Bassa-pp count power-plants with [location = "Grand Bassa"]  
 set Grand-Cape-Mount-pp count power-plants with [location = "Grand Cape Mount"] 
 set Grand-Gedeh-pp count power-plants with [location = "Grand Gedeh"] 
 set Grand-Kru-pp count power-plants with [location = "Grand Kru"] 
 set Lofa-pp count power-plants with [location = "Lofa"] 
 set Margibi-pp count power-plants with [location = "Margibi"] 
 set Maryland-pp count power-plants with [location = "Maryland"] 
 set Montserrado-pp count power-plants with [location = "Montserrado"]
 set Nimba-pp count power-plants with [location = "Nimba"]
 set Rivercess-pp count power-plants with [location = "Rivercess"]
 set Sinoe-pp count power-plants with [location = "Sinoe"]
 set River-Gee-pp count power-plants with [location = "River Gee"]
 set Gbarpolu-pp count power-plants with [location = "Gbarpolu"]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-lcoe-ge ;turtle context 
;; In this procedure the projects calculate their cost to connect to an 
;; existing secondary grid.  They have to find a socket whose network has capacity to expand  
  set closest-socket min-one-of sockets with [open-capacity? = true] [distance myself]
  
  ifelse closest-socket != nobody
  ;; If they find a socket then they calculate the cost to expand that network
  ;; including increasing the generation capacity, creating the transmission line to the socket, 
  ;; and upgrading the path of transmission lines to it if necessary.
    
  [set distance-to-socket distance closest-socket
    set possible-supplier [supplier] of closest-socket
    set possible-technology [technology] of closest-socket
    
    
    ifelse remaining-demand < [available-capacity] of possible-supplier
    [set possible-supply remaining-demand]
    [set possible-supply [available-capacity] of possible-supplier] 
  
    calculate-transmission-capital-cost
    calculate-network-upgrade-cost   

  ;The costs depend on the technology the project is connecting to.  
  if possible-technology = "solar"
    [set ge-cost ((solar-capital * solar-required-per-kw-demand * CRFsolar) / (8760 * load-factor))
      + battery-capital * CRFbatteries * storage-needed 
       + transmission-capital-cost / (8760 * possible-supply * load-factor)
       + (CRFtrans * network-upgrade-cost / (8760 * possible-supply * load-factor)) 
       + (solar-o&m / (8760 * load-factor))]
    
  if possible-technology = "biomass"
    [set ge-cost ((biomass-capital * CRFbiomass) / (8760 * load-factor))  
      + transmission-capital-cost / (8760 * possible-supply * load-factor)
      + (CRFtrans * network-upgrade-cost / (8760 * possible-supply * load-factor))
      + (biomass-o&m / (8760 * load-factor)) 
      + ((Pl * biomass-fuel) / (biomass-heat-content * .25 * 278 ))] ;assumes 30% efficiency for biomass and 278 is a conversion factor for Gj to kWh
    
  if possible-technology = "hydro"
    [set ge-cost  ((hydro-capital * CRFhydro) / (8760 * load-factor)) 
      + transmission-capital-cost / (8760 * possible-supply * load-factor)
      + (CRFtrans * network-upgrade-cost / (8760 * possible-supply * load-factor))
      + (hydro-o&m / 8760 * load-factor )
      ]
  ]
    
    ;; If there is no available socket then their ge-cost goes to 0 and that is dealt with 
  ;in the pick-connection-mode procedure
  [set ge-cost 0] 
end  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-transmission-capital-cost
  ;This procedure calculates the possible transmission capital cost to the a GE project.  It differs from the
  ;calculate-possible-transmission-capital-cost in that this procedure is calculating the cost to a possible socket and not
  ;to a possible patch.  
  
  
  if distance-to-socket <= 8.45
  ;if the distance is less than 80 km
  ;we check loadability
  [if possible-supply < 14500 ;this is an assumed end I need to double check
    [set possible-line-cost cost-of-33kv];use 33 kV
    if possible-supply >= 14500 and possible-supply < 156000 
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 156000 and possible-supply < 435000
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 435000 and possible-supply < 1275000
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 1275000 
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]
    
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ; between 80 and 100 km
  if distance-to-socket > 8.45 and distance-to-socket < 10.57
  [
    if possible-supply < 143000 
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 143000 and possible-supply < 398750 
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 398750 and possible-supply < 1168750
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 1168750 
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 100 and 200 km
  if distance-to-socket >= 10.57 and distance-to-socket < 21.14
  [
    if possible-supply < 117000 
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 117000 and possible-supply < 326250 
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 326250 and possible-supply < 956250
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 956250 
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]
   
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 200 and 300 km
  if distance-to-socket >= 21.14 and distance-to-socket < 31.71
  [
    if possible-supply < 91000 
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 91000 and possible-supply < 253750 
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 253750 and possible-supply < 743750
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 743750
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if distance-to-socket >= 31.71 and distance-to-socket < 42.28
  [
    if possible-supply < 67600 
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 67600 and possible-supply < 188500 
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 188500 and possible-supply < 552500
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 552500
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]
   
 ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if distance-to-socket >= 42.28
  [
    if possible-supply < 57200
    [set possible-line-cost cost-of-138kv];use 138 kV
    if possible-supply >= 57200 and possible-supply < 159500 
    [set possible-line-cost cost-of-230kv];use 230 kV
    if possible-supply >= 159500 and possible-supply < 467500
    [set possible-line-cost cost-of-345kv];use 345 kV
    if possible-supply >= 467500
    [set possible-line-cost cost-of-500kv];use 500 kV
  ]   

 
  set transmission-capital-cost (distance-to-socket * possible-line-cost * 9.46 * CRFtrans) 
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  

to calculate-network-upgrade-cost
;; In this procedure agents calculate the cost of upgrading a network
;; if it is necessary for them to do so
set network-upgrade-cost 0
ask links [
  set new-cost-km 0
  set upgrade-cost-km 0
  ]
ask closest-socket [
  set shortest-path-list []
  set turtles-on-shortest-path-list []
  set shortest-path nobody
  set turtles-on-shortest-path nobody
  set demand-on-path 0
  set shortest-path-list nw:weighted-path-to supplier "span"
  set turtles-on-shortest-path-list nw:turtles-on-weighted-path-to supplier "span"]
 
set shortest-path link-set [shortest-path-list] of closest-socket
set turtles-on-shortest-path turtle-set [turtles-on-shortest-path-list] of closest-socket
ask shortest-path [
  set upgrade-load load-carried + [possible-supply] of myself

  if span <= 8.45
  ;if the distance is less than 80 km
  ;we check loadability
  [if upgrade-load < 14500 ;this is an assumed end I need to double check
    [set new-cost-km cost-of-33kv];use 33 kV
    if upgrade-load >= 14500 and upgrade-load < 156000 
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 156000 and upgrade-load < 435000
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 435000 and upgrade-load < 1275000
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 1275000 
    [set new-cost-km cost-of-500kv];use 500 kV
  ]
    
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ; between 80 and 100 km
  if span > 8.45 and span < 10.57
  [
    if upgrade-load < 143000 
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 143000 and upgrade-load < 398750 
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 398750 and upgrade-load < 1168750
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 1168750 
    [set new-cost-km cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 100 and 200 km
  if span >= 10.57 and span < 21.14
  [
    if upgrade-load < 117000 
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 117000 and upgrade-load < 326250 
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 326250 and upgrade-load < 956250
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 956250 
    [set new-cost-km cost-of-500kv];use 500 kV
  ]
   
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 200 and 300 km
  if span >= 21.14 and span < 31.71
  [
    if upgrade-load < 91000 
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 91000 and upgrade-load < 253750 
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 253750 and upgrade-load < 743750
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 743750
    [set new-cost-km cost-of-500kv];use 500 kV
  ]
  
  ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if span >= 31.71 and span < 42.28
  [
    if upgrade-load < 67600 
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 67600 and upgrade-load < 188500 
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 188500 and upgrade-load < 552500
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 552500
    [set new-cost-km cost-of-500kv];use 500 kV
  ]
   
 ;otherwise we must use higher voltage lines
  ;but we must check the loadability of the lines
  ;between 300 and 400 km
  if span >= 42.28
  [
    if upgrade-load < 57200
    [set new-cost-km cost-of-138kv];use 138 kV
    if upgrade-load >= 57200 and upgrade-load < 159500 
    [set new-cost-km cost-of-230kv];use 230 kV
    if upgrade-load >= 159500 and upgrade-load < 467500
    [set new-cost-km cost-of-345kv];use 345 kV
    if upgrade-load >= 467500
    [set new-cost-km cost-of-500kv];use 500 kV
  ]   
  
  ifelse cost-km < new-cost-km 
  [set upgrade-cost-km new-cost-km - cost-km]
  [set upgrade-cost-km 0]]
set network-upgrade-cost sum [upgrade-cost-km * span * 9.46] of shortest-path
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to reset-base-potentials
  ask patches with [is-string? county] [
    
    set biomass-potential biomass-base-potential
    set solar-potential solar-base-potential
    set hydro-potential hydro-base-potential
    set biomass-shed (patch-set patches with [pxcor >= ([pxcor] of myself - 2) and pxcor <= ([pxcor] of myself + 2) 
        and pycor >= ([pycor] of myself - 2) and pycor <= ([pycor] of myself + 2)])
    set aggregated-biomass-potential sum [biomass-base-potential] of biomass-shed
  
      if aggregated-biomass-potential > solar-potential and aggregated-biomass-potential > hydro-potential
      [set largest-potential aggregated-biomass-potential
        set largest-potential-type "biomass"
        set source "biomass"]
      if solar-potential > aggregated-biomass-potential and solar-potential > hydro-potential
      [set largest-potential solar-potential
        set largest-potential-type "solar"
        set source "solar"]
      if hydro-potential > aggregated-biomass-potential and hydro-potential > solar-potential
      [set largest-potential hydro-potential
        set largest-potential-type "hydro"
        set source "hydro"]
 
      set developed? false ;Establish that in the begining all patches in Liberia have no power plants
  ]
end  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to pick-next-project
;; In this procedure the model picks the project it will build according to the decision
;; strategies given by the user. 
  if decision-strategy = "multiple-resource" [
    ask projects [
      pick-connection-mode] ;All mode pick the cheapes project cost 
    set expansion-node min-one-of projects [project-cost]]
  
  if decision-strategy = "largest-resource" [
    ifelse any? sockets with [open-capacity? = true ]
   [ ask projects [
      calculate-lcoe-ge] ;All Projects calculate their dg cost and their respective sockets
    set expansion-node min-one-of projects [distance-to-socket]
    ask expansion-node [set connection-mode "ge"] ]
    
   [set expansion-node one-of projects
     ask expansion-node [set connection-mode "dg"]   
  ]
  ]
  
  
  if decision-strategy = "largest-population" [
    ifelse any? sockets with [open-capacity? = true ]
   [ ask projects [
      calculate-lcoe-ge] ;All Projects calculate their dg cost and their respective sockets
    set expansion-node min-one-of projects [distance-to-socket]
    ask expansion-node [set connection-mode "ge"] ]
    
   [set expansion-node one-of projects
     ask expansion-node [set connection-mode "dg"]   
  ]
  ]
     
      if decision-strategy = "most-jobs" [
    ask projects [
      pick-connection-mode] ;All mode pick the cheapes project cost 
    set expansion-node max-one-of projects [max-jobs]]
     
     
     if decision-strategy = "largest-inflows" [
    ask projects [
      pick-connection-mode] ;All mode pick the cheapes project cost 
    set expansion-node max-one-of projects [max-flows]]
     
     
     
     
  ;ifelse grid-extension = false
  ;; If we are not focusing on grid-extension then it picks the cheapest cost project as the expansion-node
 ; [set expansion-node min-one-of projects [project-cost]]
  ;; If we are focusing on ge then it picks the cheapest "ge" project as the expansion node
  ;[set expansion-node min-one-of projects with [ge-cost > 0] [distance-to-socket]
   ; if expansion-node != nobody 
    ;[ask expansion-node [set connection-mode "ge"]]
    ;if expansion-node = nobody 
    ;; If there is nobody that fits that then it picks the cheapest one with dg as the expansion node.  
    ;[set expansion-node min-one-of projects with [connection-mode = "dg"] [project-cost]]]
  ;if is-unmet? expansion-node [
   ; set expansion-node nobody
    ;go
    ;]
  ;; If the expansion-node is an unmet then the model goes back to go procedure where the lists of projects get updated
  ;; so the model can stop.  
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;expansion-node was asked to

to grow-network

;; Here the model picks what kind of project to create  
  
  ifelse [connection-mode] of expansion-node = "ge"
  [connect-project] ;; If the expansion-node is grid extension regardless of the 
                    ;; decision strategy it will get made so the project is created
                   
  [
    if decision-strategy = "multiple-resource" [
      if best-patch != nobody
     [ask best-patch [
       create-power-plant]
     ask expansion-node [
       set closest-socket max-one-of power-plants [who]]
     connect-project]
      ]
    
    if decision-strategy = "largest-resource" [
    pick-project-patch
    if project-patch != nobody
      [initiate-grid]
      ]
  
    if decision-strategy = "largest-population" [
    pick-project-patch
    if project-patch != nobody
      [initiate-grid]
      ]
    
    if decision-strategy = "most-jobs" [
      if best-patch != nobody
     [ask best-patch [
       create-power-plant]
     ask expansion-node [
       set closest-socket max-one-of power-plants [who]]
     connect-project]
      ]
    
    if decision-strategy = "largest-inflows" [
      if best-patch != nobody
     [ask best-patch [
       create-power-plant]
     ask expansion-node [
       set closest-socket max-one-of power-plants [who]]
     connect-project]
      ]
      ]            
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to update-potentials
  ;The patches with developed = false were asked to perform this  
    set aggregated-biomass-potential sum [biomass-potential] of biomass-shed
  
      if aggregated-biomass-potential > solar-potential and aggregated-biomass-potential > hydro-potential
      [set largest-potential aggregated-biomass-potential
        set largest-potential-type "biomass"
        set source "biomass"]
      if solar-potential > aggregated-biomass-potential and solar-potential > hydro-potential
      [set largest-potential solar-potential
        set largest-potential-type "solar"
        set source "solar"]
      if hydro-potential > aggregated-biomass-potential and hydro-potential > solar-potential
      [set largest-potential hydro-potential
        set largest-potential-type "hydro"
        set source "hydro"]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to run-verification-experiment
  
  
  open-files
  load-the-world
  
  set simulation 1
  let subsimulation 1
  while [simulation < 3]
    [foreach [ "largest-resource" "largest-population" "most-jobs" "equal-development" "largest-inflows" "central-resource"]
      [set decision-strategy ?
        foreach [true false]
        [setup  
          while [any? projects]
          [go
           print-verification-data
           print-line-verification-data
           print-agent-verification-data
           print-patch-verification-data
            ]
          
          set subsimulation subsimulation + 1 ]
          print subsimulation
    
        
            ]
      print simulation
      set simulation simulation + 1]    
    
  close-files 
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to open-files
  ;;Open general verification files
file-open "1model-verification1.txt"
  file-print (word "Start " date-and-time ";")
  file-print (word "")
  file-print (word "simulation; decision-strategy; grid-extension; demand-per-person;" 
    "cost-of-33kv; cost-of-138kv; cost-of-230kv; cost-of-345kv; cost-of-500kv;" 
    "total-population; average-population; discount-rate; taxes-insurance;"               
    "CRFsolar; CRFbiomass; CRFhydro; CRFtrans; pl;"
    "solar-capital; solar-o&m; hydro-capital; hydro-o&m; hydro-duty-factor; biomass-capital; biomass-o&m; biomass-duty-factor; biomass-fuel; biomass-heat-rate;"
    "solar-lifespan; biomass-lifespan; hydro-lifespan; transmission-lifespan;" 
     "overall-lcoe; ticks; project-patch; expansion-node; %-unelectrified;"
    "electrification-method; total-plant-investment; total-transmission-investment; initial-population; total-generation;"
    "%-33kv; %-138kv; %-230kv; %-345kv; %-500kv;" 
    "%-right-of-way-area; solar-installed-capacity; biomass-installed-capacity; hydro-installed-capacity;"
    "%-solar-gen; %-biomass-gen; %-hydro-gen;"
    "%-of-solar-used; %-of-biomass-used; %-of-hydro-used;"
    "total-economic-inflows; job-years; secondary-grids; dg-mix;" 
    "total-power-plants; turn; target-county;" 
    "bomi-biomass; bomi-patches; bomi-average-biomass;"
    "bong-biomass; bong-patches; bong-average-biomass;" 
    "grand-bassa-biomass;grand-bassa-patches;grand-bassa-average-biomass;"
    "grand-cape-mount-biomass; grand-cape-mount-patches; grand-cape-mount-average-biomass;"
    
    "grand-gedeh-biomass; grand-gedeh-patches; grand-gedeh-average-biomass;"
    "grand-kru-biomass; grand-kru-patches;grand-kru-average-biomass;"
    
    "lofa-biomass;lofa-patches;lofa-average-biomass;"
  
    "margibi-biomass;margibi-patches;margibi-average-biomass;"
    
    "maryland-biomass;maryland-patches;maryland-average-biomass;"
    
    "montserrado-biomass;montserrado-patches;montserrado-average-biomass;"
    
    "nimba-biomass;nimba-patches;nimba-average-biomass;"
    "rivercess-biomass;rivercess-patches;rivercess-average-biomass;"
    
    "sinoe-biomass;sinoe-patches;sinoe-average-biomass;"
    
    "river-gee-biomass;river-gee-patches;river-gee-average-biomass;"
    
    "gbarpolu-biomass; gbarpolu-patches;gbarpolu-average-biomass;"
    
    "Bomi-pp; Bong-pp; Grand-Bassa-pp; Grand-Cape-Mount-pp; Grand-Gedeh-pp; Grand-Kru-pp; Lofa-pp; Margibi-pp; Maryland-pp;"
    "Montserrado-pp; Nimba-pp; Rivercess-pp; Sinoe-pp; River-Gee-pp; Gbarpolu-pp;"
    
    "p-largest-potential;p-central ;biggest-population ;p-biggest-population ; p-jobs ;p-inflows ; lowest-project-cost ; p-lowest-cost ; "
    "lowest-cost ;lowest-dg-project ;lowest-dg-cost ;lowest-dg-best-patch ; lowest-ge-project ;lowest-ge-cost ;lowest-ge-best-patch ;"
    "most-recent-plant ;most-recent-patch-developed ;")

  ;;Open line verification files
file-open "1line-verification1.txt"
  file-print (word "Start " date-and-time ";")
  file-print (word "simulation;ticks;link-id;span;grade;cost-km;new-cost-km; upgrade-cost-km; load-carried; upgrade-load")
  file-print (word "")
  
  
  ;;Open agent verification files
file-open "1agent-verification1.txt"
  file-print (word "Start " date-and-time ";")
  file-print (word "simulation;ticks; who; name; in-county; baseline-people; people; original-demand; remaining-demand; closest-socket;"
  "distance-to-socket; possible-supplier; supplier; possible-technology; technology; ge-cost; dg-cost; project-cost;"
  "best-patch; dg-distance; connection-mode; open-capacity?; transmission-capital-cost; network-upgrade-cost; shortest-path;"
  "shortest-path-list; turtles-on-shortest-path; turtles-on-shortest-path-list;demand-on-path;"
 )
  file-print (word "")
  
   ;;Open agent verification files
file-open "1patch-verification1.txt"
  file-print (word "Start " date-and-time ";")
  file-print (word "simulation;ticks; xcor; ycor; baseline-potential; largest-potential; developed?; distance-list;"
      "demand-list;ci-list; centrality; resource-centrality; dg-solar-cost-per-kw; dg-biomass-cost-per-kw;"
      "dg-hydro-cost-per-kw; dg-cost-per-kw; possible-transmission-cost; county; biomass-shed; biomass-potential;"             
      "aggregated-biomass-potential;solar-potential;hydro-potential;biomass-base-potential; solar-base-potential;"          
      "hydro-base-potential; largest-potential-type;source; solar-inflows; biomass-inflows;hydro-inflows;project-inflows;"
      "solar-jobs; biomass-jobs;hydro-jobs;project-jobs;" )
 
  file-print (word "")
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to close-files
  file-open "1model-verification1.txt"
  file-print (word "End " date-and-time)
  file-print (word "")
  file-open "1line-verification1.txt"
  file-print (word "End " date-and-time)
  file-print (word "")
  file-open "1agent-verification1.txt"
  file-print (word "End " date-and-time)
  file-print (word "")
  file-open "1patch-verification1.txt"
  file-print (word "End " date-and-time)
  file-print (word "")
  file-close-all
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to print-verification-data
  file-open "1model-verification1.txt"
  file-print (word simulation";"decision-strategy";" grid-extension";" peak-power-demand";" 
    cost-of-33kv";" cost-of-138kv";" cost-of-230kv";" cost-of-345kv";" cost-of-500kv";"
    total-population";" average-population";" discount-rate";" taxes-insurance";"               
    CRFsolar";" CRFbiomass";" CRFhydro";" CRFtrans";" pl";"
    solar-capital";" solar-o&m";" hydro-capital";" hydro-o&m";" hydro-duty-factor";" biomass-capital";" biomass-o&m";" biomass-duty-factor";" biomass-fuel";" biomass-heat-content";"
    solar-lifespan";" biomass-lifespan";" hydro-lifespan";" transmission-lifespan";" 
    overall-lcoe";" ticks ";" project-patch";" expansion-node";" %-unelectrified";"
    electrification-method";" total-plant-investment";" total-transmission-investment";" initial-population";" total-generation";"
    %-33kv";" %-138kv";" %-230kv";" %-345kv";" %-500kv";" 
    %-right-of-way-area";" solar-installed-capacity";" biomass-installed-capacity";" hydro-installed-capacity";"
    %-solar-gen";" %-biomass-gen";" %-hydro-gen";"
    %-of-solar-used";" %-of-biomass-used";" %-of-hydro-used";"
    total-economic-inflows";" job-years";" secondary-grids";" dg-mix";" 
    total-power-plants";" turn";" target-county";" 
    bomi-biomass";" bomi-patches";"bomi-average-biomass";"
    bong-biomass";"bong-patches";" bong-average-biomass";"
    grand-bassa-biomass";" grand-bassa-patches";" grand-bassa-average-biomass";"
    grand-cape-mount-biomass";" grand-cape-mount-patches";" grand-cape-mount-average-biomass";"
    
    grand-gedeh-biomass";" grand-gedeh-patches";" grand-gedeh-average-biomass";"
    grand-kru-biomass";" grand-kru-patches";" grand-kru-average-biomass";"
    
    lofa-biomass";"lofa-patches";"lofa-average-biomass";"
    
    margibi-biomass";" margibi-patches";" margibi-average-biomass";"
    
    maryland-biomass";" maryland-patches";" maryland-average-biomass";"
    
    montserrado-biomass";" montserrado-patches";" montserrado-average-biomass";"
    
    nimba-biomass";" nimba-patches";" nimba-average-biomass";"
    rivercess-biomass";" rivercess-patches";" rivercess-average-biomass";"
    
    sinoe-biomass";" sinoe-patches";" sinoe-average-biomass";"
    
    river-gee-biomass";" river-gee-patches";" river-gee-average-biomass";"
    
    gbarpolu-biomass";" gbarpolu-patches";" gbarpolu-average-biomass";"
    
    Bomi-pp";" Bong-pp";" Grand-Bassa-pp";" Grand-Cape-Mount-pp";" Grand-Gedeh-pp";" Grand-Kru-pp";" Lofa-pp";" Margibi-pp";" Maryland-pp";"
    Montserrado-pp";" Nimba-pp";" Rivercess-pp";" Sinoe-pp";"River-Gee-pp";" Gbarpolu-pp";"
    
    p-largest-potential";" p-central";" biggest-population";" p-biggest-population";" p-jobs";" p-inflows";" lowest-project-cost";" p-lowest-cost";"
    lowest-cost";" lowest-dg-project";" lowest-dg-cost";" lowest-dg-best-patch ";" lowest-ge-project";" lowest-ge-cost ";" lowest-ge-best-patch ";"
    most-recent-plant";" most-recent-patch-developed ";")
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to print-line-verification-data
  
  file-open "1line-verification1.txt"
  foreach line-list [
    file-print (word simulation";"ticks ";" ?";" [span] of ? ";" [grade] of ?";" [cost-km] of ? ";" 
      [new-cost-km] of ? ";" [upgrade-cost-km] of ? ";" [load-carried] of ? ";" [upgrade-load] of ?";")]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to print-agent-verification-data
  
  file-open "1agent-verification1.txt"
  ask agent-v [
    file-print (word simulation";"ticks ";" who";" name";" in-county ";" baseline-people ";" 
  people ";" 
  original-demand";" 
  remaining-demand ";"
  closest-socket";"
  distance-to-socket";"
  possible-supplier";"
  supplier ";"
  possible-technology  ";" 
  technology           ";"
  ge-cost";"
  dg-cost ";"
  project-cost";"
  best-patch ";"
  dg-distance";"
  connection-mode";"
  open-capacity? ";"
  transmission-capital-cost";"
  network-upgrade-cost ";"
  shortest-path";"
  shortest-path-list";"
  turtles-on-shortest-path" ;" 
  turtles-on-shortest-path-list";"
  demand-on-path";"
 )]
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to print-patch-verification-data  
  file-open "1patch-verification1.txt"
    file-print (word simulation";"ticks";" [pxcor] of patch-v";" [pycor] of patch-v";" [baseline-potential] of patch-v";" [largest-potential] of patch-v ";"
      [developed?] of patch-v";" [distance-list] of patch-v";" [demand-list] of patch-v ";"[ci-list] of patch-v";" 
      [centrality] of patch-v";" [resource-centrality] of patch-v";" [dg-solar-cost-per-kw] of patch-v ";" 
      [dg-biomass-cost-per-kw] of patch-v";" [dg-hydro-cost-per-kw] of patch-v";" [dg-cost-per-kw] of patch-v";" 
      [possible-transmission-cost] of patch-v";" [county] of patch-v";" [biomass-shed] of patch-v";" [biomass-potential] of patch-v";"             
      [aggregated-biomass-potential] of patch-v";" [solar-potential] of patch-v";" [hydro-potential] of patch-v";"
      [biomass-base-potential] of patch-v";" [solar-base-potential] of patch-v";"[hydro-base-potential] of patch-v";" 
      [largest-potential-type] of patch-v";" [source] of patch-v";" [solar-inflows] of patch-v";" [biomass-inflows] of patch-v";"
      [hydro-inflows] of patch-v";"[project-inflows] of patch-v";" [solar-jobs] of patch-v";" [biomass-jobs] of patch-v";"
      [hydro-jobs] of patch-v";" [project-jobs] of patch-v";" )
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to run-experiment [ test ]
  
 file-open  (word test ".txt")
  open-experiment-file
  load-the-world
  
  set simulation 1
  let subsimulation 1
   foreach [150 190 200 250 300 350 400]
   [set peak-power-demand ?
     foreach [0.1 0.2 0.3 0.4 0.5]
     [ set base-load ?
     
       
       foreach [ "multiple-resource" "largest-resource" "largest-population" "most-jobs" "largest-inflows" ]
       [set decision-strategy ?
         set grid-extension true
         setup  
         while [any? projects]
           [go]
         print-experiment-data
         print (word subsimulation "-" simulation)
         set subsimulation subsimulation + 1 
       ]]]
            
  close-files  
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to open-experiment-file
  ;;Open general verification files

  file-print (word "Start " date-and-time ";")
  file-print (word "")
  file-print (word "simulation; decision-strategy; grid-extension;" 
    "overall-lcoe; ticks;total-economic-inflows; job-years; secondary-grids; dg-mix;"
    "peak-power-demand; morning-peak; duration-of-morning-peak; evening-peak; duration-of-evening-peak; base-load; duration-of-base-load;"
    "cost-of-33kv; cost-of-138kv; cost-of-230kv; cost-of-345kv; cost-of-500kv;" 
    "total-population; average-population; discount-rate; taxes-insurance;"               
    "CRFsolar; CRFbatteries; CRFbiomass; CRFhydro; CRFtrans; pl;"
    "solar-capital; solar-o&m; battery-capital; area-for-solar; panel-efficiency;"
    "hydro-capital; hydro-o&m; hydro-duty-factor; biomass-capital; biomass-o&m; biomass-duty-factor; biomass-fuel; biomass-heat-rate;"
    "solar-lifespan; biomass-lifespan; hydro-lifespan; transmission-lifespan;" 
     "project-patch; expansion-node; %-unelectrified;"
    "electrification-method; total-plant-investment; total-transmission-investment; initial-population; total-generation;"
    "%-33kv; %-138kv; %-230kv; %-345kv; %-500kv; total transmission length" 
    "%-right-of-way-area; solar-installed-capacity; biomass-installed-capacity; hydro-installed-capacity;"
    "%-solar-gen; %-biomass-gen; %-hydro-gen;"
    "%-of-solar-used; %-of-biomass-used; %-of-hydro-used;"
    "total-power-plants; average-generator-size"
    "p-largest-potential;p-central ;biggest-population ;p-biggest-population ; p-jobs ;p-inflows ; lowest-project-cost ; p-lowest-cost ; "
    "lowest-cost ;lowest-dg-project ;lowest-dg-cost ;lowest-dg-best-patch ; lowest-ge-project ;lowest-ge-cost ;lowest-ge-best-patch ;"
    "most-recent-plant ;most-recent-patch-developed; generator-sizes;")
 
  file-print (word "")
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to print-experiment-data
  file-print (word simulation";" decision-strategy";" grid-extension";" 
    overall-lcoe";" ticks";"total-economic-inflows";" job-years";" secondary-grids";" dg-mix";" 
    peak-power-demand";" morning-peak";" duration-morning-peak";" evening-peak";" duration-evening-peak";" base-load";" duration-base-load";"
    cost-of-33kv";" cost-of-138kv";" cost-of-230kv";" cost-of-345kv";" cost-of-500kv";" 
    total-population";" average-population";" discount-rate";" taxes-insurance";"               
    CRFsolar";" CRFbatteries";" CRFbiomass";" CRFhydro";" CRFtrans";" pl";"
    solar-capital";" solar-o&m";" battery-capital";" area-for-solar";" panel-efficiency";"
    hydro-capital";" hydro-o&m";" hydro-duty-factor";" biomass-capital";" biomass-o&m";" biomass-duty-factor";" biomass-fuel";" biomass-heat-content";"
    solar-lifespan";" biomass-lifespan";" hydro-lifespan";" transmission-lifespan";" 
      project-patch";" expansion-node";" %-unelectrified";"
    electrification-method";" total-plant-investment";" total-transmission-investment";" initial-population";" total-generation";"
    %-33kv";" %-138kv";" %-230kv";" %-345kv";" %-500kv";" (sum [span] of links * 9.46 )";"
    %-right-of-way-area";" solar-installed-capacity";" biomass-installed-capacity";" hydro-installed-capacity";"
    %-solar-gen";" %-biomass-gen";" %-hydro-gen";"
    %-of-solar-used";" %-of-biomass-used";" %-of-hydro-used";"
    total-power-plants";" average-generator-size ";" 
    p-largest-potential";" p-central";" biggest-population";" p-biggest-population";" p-jobs";" p-inflows";" lowest-project-cost";" p-lowest-cost";"
    lowest-cost";" lowest-dg-project";" lowest-dg-cost";" lowest-dg-best-patch";" lowest-ge-project";" lowest-ge-cost";" lowest-ge-best-patch ";"
    most-recent-plant";" most-recent-patch-developed";" [generation-size] of power-plants ";" )
end 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-dg-jobs
;  This procedure is where populations calculate their decentralized generation cost. 
; Each population calculates what would be the cost to establish a production unit in each
; patch available while also considering its own demand the available potential in the patch 
; and the distance to the patch. The turtle sets the patch that results in the lowest lcoe cost
; as its best patch and the technology in the patch as its possible source.  

  ask patches with [developed? = false]
    [calculate-possible-transmission-capital-cost
      if dg-solar-jobs > dg-hydro-jobs and dg-solar-jobs > dg-biomass-jobs [
        set patch-possible-jobs dg-solar-jobs
        set source "solar"]
      if dg-biomass-jobs > dg-hydro-jobs and dg-biomass-jobs > dg-solar-jobs [
        set patch-possible-jobs dg-biomass-jobs
        set source "biomass"]
      if dg-hydro-jobs > dg-solar-jobs and dg-hydro-jobs > dg-biomass-jobs [
        set patch-possible-jobs dg-hydro-jobs
        set source "hydro"]
    ]
    
  set best-patch max-one-of patches with [developed? = false] [patch-possible-jobs]
  
  ifelse best-patch != nobody
  ;; If the turtle did find a patch then that patch cost becomes its dg-cost
  [set dg-jobs [patch-possible-jobs] of best-patch
    set technology [source] of best-patch]
  ;; If it didnt then the cost is 0 which is dealt with in the pick-connection-mode procedure
  [set dg-jobs 0] 
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-ge-jobs
;; In this procedure the projects calculate their cost to connect to an 
;; existing secondary grid.  They have to find a socket whose network has capacity to expand  
  set closest-socket min-one-of sockets with [open-capacity? = true] [distance myself]
  
  ifelse closest-socket != nobody
  ;; If they find a socket then they calculate the cost to expand that network
  ;; including increasing the generation capacity, creating the transmission line to the socket, 
  ;; and upgrading the path of transmission lines to it if necessary.
    
  [set distance-to-socket distance closest-socket
    set possible-supplier [supplier] of closest-socket
    set possible-technology [technology] of closest-socket
    
    
    ifelse remaining-demand < [available-capacity] of possible-supplier
    [set possible-supply remaining-demand]
    [set possible-supply [available-capacity] of possible-supplier] 
  
    calculate-transmission-capital-cost
    calculate-network-upgrade-cost   

  ;The costs depend on the technology the project is connecting to.  
  if possible-technology = "solar"
    [set ge-jobs (solar-job-factor * possible-supply * load-factor * 8760) /
      (possible-supply * solar-capital * solar-required-per-kw-demand + possible-supply * storage-needed * battery-capital 
        + (transmission-capital-cost / crftrans) + network-upgrade-cost) ]
    
  if possible-technology = "biomass"
    [set ge-jobs (biomass-job-factor * possible-supply * load-factor * 8760) /
      (possible-supply * biomass-capital 
        + (transmission-capital-cost / crftrans) + network-upgrade-cost) ] 
    
  if possible-technology = "hydro"
    [set ge-jobs (hydro-job-factor * possible-supply * load-factor * 8760) /
      (possible-supply * hydro-capital 
        + (transmission-capital-cost / crftrans) + network-upgrade-cost)
      ]
  ]
    
    ;; If there is no available socket then their ge-cost goes to 0 and that is dealt with 
  ;in the pick-connection-mode procedure
  [set ge-jobs 0] 
end  



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-dg-flows
;  This procedure is where populations calculate their decentralized generation cost. 
; Each population calculates what would be the cost to establish a production unit in each
; patch available while also considering its own demand the available potential in the patch 
; and the distance to the patch. The turtle sets the patch that results in the lowest lcoe cost
; as its best patch and the technology in the patch as its possible source.  

  ask patches with [developed? = false]
    [calculate-possible-transmission-capital-cost
      if dg-solar-flows > dg-hydro-flows and dg-solar-flows > dg-biomass-flows [
        set patch-possible-flows dg-solar-flows
        set source "solar"]
      if dg-biomass-flows > dg-hydro-flows and dg-biomass-flows > dg-solar-flows [
        set patch-possible-flows dg-biomass-flows
        set source "biomass"]
      if dg-hydro-flows > dg-solar-flows and dg-hydro-flows > dg-biomass-flows [
        set patch-possible-flows dg-hydro-flows
        set source "hydro"]
    ]
    
  set best-patch max-one-of patches with [developed? = false] [patch-possible-flows]
  
  ifelse best-patch != nobody
  ;; If the turtle did find a patch then that patch cost becomes its dg-cost
  [set dg-flows [patch-possible-flows] of best-patch
    set technology [source] of best-patch]
  ;; If it didnt then the cost is 0 which is dealt with in the pick-connection-mode procedure
  [set dg-flows 0] 
end 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to calculate-ge-flows
;; In this procedure the projects calculate their cost to connect to an 
;; existing secondary grid.  They have to find a socket whose network has capacity to expand  
  set closest-socket min-one-of sockets with [open-capacity? = true] [distance myself]
  
  ifelse closest-socket != nobody
  ;; If they find a socket then they calculate the cost to expand that network
  ;; including increasing the generation capacity, creating the transmission line to the socket, 
  ;; and upgrading the path of transmission lines to it if necessary.
    
  [set distance-to-socket distance closest-socket
    set possible-supplier [supplier] of closest-socket
    set possible-technology [technology] of closest-socket
    
    
    ifelse remaining-demand < [available-capacity] of possible-supplier
    [set possible-supply remaining-demand]
    [set possible-supply [available-capacity] of possible-supplier] 
  
    calculate-transmission-capital-cost
    calculate-network-upgrade-cost   

  ;The costs depend on the technology the project is connecting to.  
  if possible-technology = "solar"
    [set ge-flows (solar-economic-factor * possible-supply * load-factor * 8760) /
      (possible-supply * solar-capital * solar-required-per-kw-demand + possible-supply * storage-needed * battery-capital 
        + transmission-capital-cost + network-upgrade-cost) ]
    
  if possible-technology = "biomass"
    [set ge-flows (biomass-economic-factor * possible-supply * load-factor * 8760) /
      (possible-supply * biomass-capital 
        + transmission-capital-cost + network-upgrade-cost) ] 
    
  if possible-technology = "hydro"
    [set ge-flows (hydro-economic-factor * possible-supply * load-factor * 8760) /
      (possible-supply * hydro-capital 
        + transmission-capital-cost + network-upgrade-cost)
      ]
  ]
    
    ;; If there is no available socket then their ge-cost goes to 0 and that is dealt with 
  ;in the pick-connection-mode procedure
  [set ge-flows 0] 
end  
  

There is only one version of this model, created over 8 years ago by Jose Alfaro.

Attached files

File Type Description Last updated
BABSTER.png preview Preview for 'BABSTER' over 8 years ago, by Jose Alfaro Download

This model does not have any ancestors.

This model does not have any descendants.