Car Tax Model

No preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.1.1 • Viewed 149 times • Downloaded 11 times • Run 0 times
Download the 'Car Tax Model' 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

;; VERSION 3-September-2020 6:00
;; With more cars

extensions [csv table]

breed [ cars car ]
breed [ buyers buyer ]

globals [
  coef_uv_list ; coefficient of utility variables list. Mind the order!
  mu_car       ; mu coefficient table for the nested logit at the second step
  c_car

  ;; buyers variable coefficients
  b-hh-size
  b-spending1 b-spending2 b-spending3 b-spending4
  b-sex
  b-age1 b-age2 b-age3 b-age4 b-age5 b-age6 b-age7 b-age8
  b-ocp1 b-ocp2 b-ocp3 b-ocp4

  ;; cars variabel coefficients
  b-fuelcost
  b-co2
  b-price
  b-kerbweight
  b-dimension
  b-gasoline
  b-manual
]

cars-own [
  variant
  brand
  fuelconsmp
  fuelcost
  co2
  price
  kerbweight
  len
  width
  height
  gasoline
  manual
  chosen?       ; boolean variable mobil dipilih oleh pembeli
  dimension
  cars_uv_list
  car_utility
]

buyers-own [
  hh-size       ; ukuran rumah tangga. Jumlah seluruh orang dalam rumah tangga. (rasio)

  ;; pengeluaran per orang per bulan berdasarkan World Bank (2019), 5 kelas
  spending1  ; <354 ribu
  spending2  ; 354 ribu - 532 ribu
  spending3  ; 532 ribu - 1,2 juta
  spending4  ; 1,2 juta - 6,0 juta
  ;spending5 >6,0 juta. Tapi kan tidak ada variabelnya.

  sex           ; jenis kelamin. (nominal)

  ;; usia, 9 kelas
  age1       ; 21 - 25 tahun
  age2       ; 26 - 30 tahun
  age3       ; 31 - 35 tahun
  age4       ; 36 - 40 tahun
  age5       ; 41 - 45 tahun
  age6       ; 46 - 50 tahun
  age7       ; 51 - 55 tahun
  age8       ; 56 - 60 tahun
  ;age9 >60 tahun.

  ;; pekerjaan, nominal
  occupation1  ; swasta
  occupation2  ; pelajar/mahasiswa
  occupation3  ; TNI/Polri
  occupation4  ; PNS
  ;occupation5  BUMN.

  ;; choice-related variables
  cset-size
  who-choice-set
  car-choice

  buyers_uv_list
  uv_list
  buyer_utility
  utility

  ; tables related to cars agentset
  table_lefthalf
  table_righthalf
  table_exp_nested_sums
  brand_list
  table_car_probs
]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; S E T U P ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  clear-all         ;; start over the model clean
  file-close-all    ;; close any files open from last run

  set-buyers        ;; setup the car buyers condition using 'set-buyers' procedure
  set-cars          ;; setup the car condition using 'set-cars' procedure

  ;; setting the list for the utility function coefficients in the second step logit model
  ;; make sure not to change the order
  set b-hh-size 1
  set b-spending1 1 set b-spending2 1 set b-spending3 1 set b-spending4 1
  set b-sex 1
  set b-age1 1 set b-age2 1 set b-age3 1 set b-age4 1
  set b-age5 1 set b-age6 1 set b-age7 1 set b-age8 1
  set b-ocp1 1 set b-ocp2 1 set b-ocp3 1 set b-ocp4 1
  set b-fuelcost 1
  set b-co2 1
  set b-price -0.063
  set b-kerbweight 1
  set b-dimension 1
  set b-gasoline 1
  set b-manual 1

  set coef_uv_list (
    list b-hh-size b-spending1 b-spending2 b-spending3 b-spending4 b-sex
         b-age1 b-age2 b-age3 b-age4 b-age5 b-age6 b-age7 b-age8
         b-ocp1 b-ocp2 b-ocp3 b-ocp4 b-fuelcost b-price b-kerbweight
         b-dimension b-gasoline b-manual )

  ;; same goes for the buyers agent
  ask buyers [
    set buyers_uv_list ( list hh-size spending1 spending2 spending3 spending4 spending4 sex
    age1 age2 age3 age4 age4 age5 age6 age7 age8 occupation1 occupation2 occupation3
    occupation4 )

    set buyer_utility
    (b-hh-size * hh-size) + (b-spending1 * spending1) + (b-spending2 * spending2) + (b-spending3 * spending3) +
    (b-spending4 * spending4) + (b-sex * sex) + (b-age1 * age1) + (b-age2 * age2) + (b-age3 * age3) +
    (b-age4 * age4) + (b-age5 * age5) + (b-age6 * age6) + (b-age7 * age7) + (b-age8 * age8) +
    (b-ocp1 * occupation1) + (b-ocp2 * occupation2) + (b-ocp3 * occupation3) + (b-ocp4 * occupation4)
  ]

  ask cars [
    ;; asking cars to count their car-side utility value
    set car_utility (b-fuelcost * fuelcost) + (b-co2 * co2) + (b-price * price) + (b-dimension * dimension) +
                    (b-gasoline * gasoline) + (b-manual * manual) + (b-kerbweight * kerbweight)
  ]

  ;; making the c_car table
  set mu_car table:make
  set c_car table:make

  ;; storing each car brand with its own mu coefficient
  table:put mu_car "Aston Martin" 1.0   table:put mu_car "Audi" 1.5
  table:put mu_car "Bentley" 2.0        table:put mu_car "BMW" 2.5
  table:put mu_car "Chevrolet" 1.0
  table:put mu_car "Daihatsu" 1.2       table:put mu_car "Datsun" 3.5
  table:put mu_car "DFSK" 3.0           table:put mu_car "Dodge" 3.5
  table:put mu_car "Fiat" 1.4
  table:put mu_car "Honda" 1.3          table:put mu_car "Hyundai" -2.5
  table:put mu_car "Isuzu" 2.3
  table:put mu_car "Jaguar" 1.9         table:put mu_car "Jeep" 1.7
  table:put mu_car "KIA" 1.1
  table:put mu_car "Lamborghini" 1.1    table:put mu_car "Land Rover" 1.6   table:put mu_car "Lexus" 1.7
  table:put mu_car "Mahindra" 1.9       table:put mu_car "Maserati" 1.2     table:put mu_car "Mazda" 2.3
  table:put mu_car "Mercedes-Benz" 4.0
  table:put mu_car "MG" 4.0             table:put mu_car "MINI" 2.0         table:put mu_car "Mitsubishi" 3.0
  table:put mu_car "Nissan" 3.1
  table:put mu_car "Peugeot" 3.3        table:put mu_car "Porsche" 3.4
  table:put mu_car "Renault" 2.9        table:put mu_car "Rolls Royce" 1.7
  table:put mu_car "Suzuki" 2.9
  table:put mu_car "Toyota" 1.3
  table:put mu_car "Volkswagen" 1       table:put mu_car "Volvo" 1
  table:put mu_car "Wuling" 1

  ;; storing each car brand with its own car constant
  table:put c_car "Aston Martin" 1      table:put c_car "Audi" 1
  table:put c_car "Bentley" 1           table:put c_car "BMW" 1
  table:put c_car "Chevrolet" 1
  table:put c_car "Daihatsu" 1          table:put c_car "Datsun" 1
  table:put c_car "DFSK" 1              table:put c_car "Dodge" 1
  table:put c_car "Fiat" 1
  table:put c_car "Honda" 1             table:put c_car "Hyundai" 1
  table:put c_car "Isuzu" 1
  table:put c_car "Jaguar" 1            table:put c_car "Jeep" 1
  table:put c_car "KIA" 1
  table:put c_car "Lamborghini" 1       table:put c_car "Land Rover" 1   table:put c_car "Lexus" 1
  table:put c_car "Mahindra" 1          table:put c_car "Maserati" 1     table:put c_car "Mazda" 1
  table:put c_car "Mercedes-Benz" 1
  table:put c_car "MG" 1                table:put c_car "MINI" 1         table:put c_car "Mitsubishi" 1
  table:put c_car "Nissan" 1
  table:put c_car "Peugeot" 1           table:put c_car "Porsche" 1
  table:put c_car "Renault" 1           table:put c_car "Rolls Royce" 1
  table:put c_car "Suzuki" 1
  table:put c_car "Toyota" 1
  table:put c_car "Volkswagen" 1        table:put c_car "Volvo" 1
  table:put c_car "Wuling" 1

  setup-plots
  update-plots
end 

to set-buyers    ;; buyers procedure.
  ifelse not file-exists? buyers_file [
    user-message "Tidak ditemukan file 'file_read_buyers.csv'. Mohon periksa kembali nama atau lokasi file tersebut."
    stop ]
  [ file-open buyers_file ]

  ;; Membaca seluruh data dalam satu loop
  while [ not file-at-end? ] [
    ; here the csv extension grabs a single line and puts the read data in a list
    let baca (csv:from-row file-read-line ";")
    ; now we can use that list to create a turtle with the saved properties

    create-buyers 1 [
      set hh-size item 0 baca

      ;; setting spending1 - spending5 value
      ( ifelse
        item 1 baca = 1
        [ set spending1 1  set spending2 0  set spending3 0  set spending4 0 ]
        item 1 baca = 2
        [ set spending1 0  set spending2 1  set spending3 0  set spending4 0 ]
        item 1 baca = 3
        [ set spending1 0  set spending2 0  set spending3 1  set spending4 0 ]
        item 1 baca = 4
        [ set spending1 0  set spending2 0  set spending3 0  set spending4 1 ]
        [ set spending1 0  set spending2 0  set spending3 0  set spending4 0 ])

      ;; setting occupation1 - occupation5 value
      ( ifelse
        item 2 baca = 1
        [ set occupation1 1  set occupation2 0  set occupation3 0  set occupation4 0 ]
        item 2 baca = 2
        [ set occupation1 0  set occupation2 1  set occupation3 0  set occupation4 0 ]
        item 2 baca = 3
        [ set occupation1 0  set occupation2 0  set occupation3 1  set occupation4 0 ]
        item 2 baca = 4
        [ set occupation1 0  set occupation2 0  set occupation3 0  set occupation4 1 ]
        [ set occupation1 0  set occupation2 0  set occupation3 0  set occupation4 0 ] )

      set sex item 3 baca

      ;; setting age1 - age9 value
      ( ifelse
        item 4 baca = 1
        [ set age1 1  set age2 0  set age3 0  set age4 0
          set age5 0  set age6 0  set age7 0  set age8 0 ]
        item 4 baca = 2
        [ set age1 0  set age2 1  set age3 0  set age4 0
          set age5 0  set age6 0  set age7 0  set age8 0 ]
        item 4 baca = 3
        [ set age1 0  set age2 0  set age3 1  set age4 0
          set age5 0  set age6 0  set age7 0  set age8 0 ]
        item 4 baca = 4
        [ set age1 0  set age2 0  set age3 0  set age4 1
          set age5 0  set age6 0  set age7 0  set age8 0 ]
        item 4 baca = 5
        [ set age1 0  set age2 0  set age3 0  set age4 0
          set age5 1  set age6 0  set age7 0  set age8 0 ]
        item 4 baca = 6
        [ set age1 0  set age2 0  set age3 0  set age4 0
          set age5 0  set age6 1  set age7 0  set age8 0 ]
        item 4 baca = 7
        [ set age1 0  set age2 0  set age3 0  set age4 0
          set age5 0  set age6 0  set age7 1  set age8 0 ]
        item 4 baca = 8
        [ set age1 0  set age2 0  set age3 0  set age4 0
          set age5 0  set age6 0  set age7 0  set age8 1 ]
        [ set age1 0  set age2 0  set age3 0  set age4 0
          set age5 0  set age6 0  set age7 0  set age8 0 ] )
    ]

    ask buyers [
      set shape        "person"
      set color        green
      let bound-buyers random -45 - 5
      set xcor         random-xcor
      set ycor         random-ycor ]
  ]

  file-close ;; make sure to close the file
end 

to set-cars  ;; cars procedure.

  ifelse not file-exists? cars_file [
    user-message "Tidak ditemukan file 'file_read_cars.csv'. Mohon periksa kembali nama atau lokasi file tersebut."
    stop ]
  [ file-open cars_file ]

  ;; Membaca seluruh data dalam satu loop
  while [ not file-at-end? ][
    ; here the csv extension grabs a single line and puts the read data in a list
    let baca (csv:from-row file-read-line ";")
    ; now we can use that list to create a turtle with the saved properties
    create-cars 1 [
      set variant    item 0 baca
      set brand      item 1 baca
      set fuelconsmp item 2 baca
      set fuelcost   item 3 baca / 10000
      set co2        item 4 baca / 100
      set price      item 5 baca / 1000
      set kerbweight item 6 baca / 1000
      set len        item 7 baca
      set width      item 8 baca
      set height     item 9 baca
      set gasoline   item 10 baca
      set manual     item 11 baca ]

    ask cars [
      set shape        "car"
      set color        yellow
      let bound-cars   random 45 + 5
      set xcor         random-xcor
      set ycor         random-ycor
      set dimension    (len * width) / 10000000 ]
  ]

  file-close ;; make sure to close the file
end 




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   G  0    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
  first-step
  second-step
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   F  I  R  S  T      S  T  E  P    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to first-step
  ask cars [
    set chosen? 0 ]

  first-stage

  ;; this is the equivalence to "tick" if you decide your model won't involve time frame.
  update-plots
end 

to first-stage  ;; buyers procedure.
  ask buyers [
    let a 4
    let b 1 / 6

    ;; set the number of cars choice set for each buyer
    ;; by the non-compensatory rule
    set cset-size round random-gamma a b

    set who-choice-set [who] of n-of cset-size cars ;; set buyers' car choice-set as an agentset
    set label cset-size
  ]
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  S  E  C  O  N  D      S  T  E  P    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to second-step
  ask buyers [
    ;; table for storing each car's exponentials of utility.
    set table_lefthalf table:make
    foreach who-choice-set [
      x ->
      let c table:get c_car [brand] of car x
      let value (buyer_utility + ([car_utility] of car x) + c)
      table:put table_lefthalf ([who] of car x) (exp (value * table:get mu_car [brand] of car x))
    ]

    set table_righthalf table:make

    ;; procedures to make the table_righthalf table, table for storing aggregate utility
    ;; value for cars within the same brand.
    ; first make a list of list containing [["brand" utility_value]["brand 2" utility_value2]...]
    set brand_list []
    foreach who-choice-set
    [ x -> set brand_list lput (list [brand] of car x table:get table_lefthalf x) brand_list ]
    ; next sort the list of list by the brand name alphabetically
    set brand_list sort-by [[a b] -> first a < first b] brand_list
    ; after that we are going to make aggregated list of car brands and the corresponding value
    set brand_list make-righthalf brand_list
    ; now we assign that list to table_righthalf
    set table_righthalf table:from-list brand_list
    ; we also need a table for the exp(1 / mu_j * ln total exp(mu_j * Vij))
    set table_exp_nested_sums table:from-list make-exp-nested-sums brand_list

    ; now we can compute each car's probability
    let car_probs []
    foreach who-choice-set [
      id -> set car_probs lput list id count-exp id car_probs
    ]
    ; sort the cars based on their probability descending
    set car_probs sort-by [[a b] -> last a > last b] car_probs
    set table_car_probs table:from-list car_probs

    ; after each car has its own probability, we ask buyer to choose the car that
    ; has the highest probability value
    let highestp car first first car_probs
    set car-choice highestp
    ask car-choice [
      set chosen? 1
    ]
  ]

  update-plots
end 

;this is my version to make the righthalf table
;to-report make-righthalf [li]
;  let result []
;  let first_item item 0 li     ; first_item is also a list
;  let li-length length li
;  let li-end li-length - 1
;  foreach (range 1 li-length) [
;    ? -> ifelse ? != li-end
;    [ ifelse item 0 item ? li = item 0 first_item
;      [ set first_item replace-item 1 first_item (item 1 first_item + item 1 item ? li) ]
;      [ set result lput first_item result
;        set first_item item ? li ]
;    ]
;    [ ifelse item 0 item ? li = item 0 first_item
;      [ set first_item replace-item 1 first_item (item 1 first_item + item 1 item ? li)
;        set result lput first_item result ]
;      [ set result lput first_item result
;        set result lput item li-end li result ]
;    ]
;  ]
;  report result
;end

;this is JenB's version

to-report make-righthalf [li]
  ; this procedure assumes that li has been sorted
  ; uncomment this following code if above assumption is not used
;  let sorted-list sort-by [[a b] -> first a < first b] li
  ; prime the loop
  let pair-to-add first li
  let active-letter first pair-to-add
  let result []

  ; loop from the second item to the end
  foreach but-first li [
    pair ->
    ifelse first pair = active-letter
    ; in same first letter so add
    [ set pair-to-add (list first pair-to-add (last pair-to-add + last pair)) ]
    ; in different first letter so output and use this item as start of next chain
    [ set result lput pair-to-add result
      set pair-to-add pair
      set active-letter first pair
    ]
  ]
  ; append the final pair
  set result lput pair-to-add result
  report result
end 

to-report make-exp-nested-sums [li]
  let result []
  foreach li [
    pair ->
    let the-brand first pair
    let cmu table:get mu_car the-brand
    set result lput list the-brand ((1 / cmu) * (ln last pair)) result
  ]
  report result
end 

to-report count-exp [id]
  let p_num_lefthalf table:get table_lefthalf id
  let p_num_righthalf exp (1 / table:get mu_car [brand] of car id) * ln (table:get table_righthalf [brand] of car id)
  let p_denom_lefthalf table:get table_righthalf [brand] of car id
  let p_denom_righthalf sum table:values table_exp_nested_sums

  report (p_num_lefthalf / p_denom_lefthalf) * (p_num_righthalf * p_denom_righthalf)
end 



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;   C   L   E   A   R     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to clear
  reset-ticks

  ask cars [
    set chosen? 0 ]

  ask buyers [
    set cset-size 0
    set label ""
  ]

  setup-plots
end 

There are 3 versions of this model.

Uploaded by When Description Download
Muhammad Abdul Mubdi Bindar over 3 years ago established a nested logit choice procedure Download this version
Muhammad Abdul Mubdi Bindar over 3 years ago hopefully solves the extensions issue Download this version
Muhammad Abdul Mubdi Bindar over 3 years ago Initial upload Download this version

Attached files

File Type Description Last updated
file_read_buyers.csv extension 'buyers' breed file over 3 years ago, by Muhammad Abdul Mubdi Bindar Download
file_read_cars.csv extension 'cars' breed file over 3 years ago, by Muhammad Abdul Mubdi Bindar Download

This model does not have any ancestors.

This model does not have any descendants.