Skip to content

Commit

Permalink
adding the full code base and changing the numbering of chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
sausheong committed Jul 7, 2012
1 parent 22984d5 commit 70c1ae4
Show file tree
Hide file tree
Showing 80 changed files with 634 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Chapter 3 - Offices and Restrooms/ex-3.13.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data <- read.table("simulation3-4.csv", header=TRUE, sep=",")
df <- data.frame(table(data$X400))
colnames(df) <- c("queue_size", "frequency")
percent_labels <- paste(df$frequency, '\n', round(df$frequency*100/540, 2), '%')

ggplot(data=df) + opts(legend.position = "none") +
geom_bar(aes(x = queue_size, y = frequency, fill = factor(queue_size))) +
geom_text(aes(x = queue_size, y = frequency, label = percent_labels, size=1)) +
scale_y_continuous("frequency") +
scale_x_discrete("queue size")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pdf("ch04_ex5_2.pdf")
data <- read.table("simulation3.csv", header=TRUE, sep=",")
df <- data.frame(table(data$X400))
colnames(df) <- c("queue_size", "frequency")
percent_labels <- paste(df$frequency, '(', round(df$frequency*100/540, 2), '%)')
percent_labels <- paste(df$frequency, '\n', round(df$frequency*100/540, 2), '%')

ggplot(data=df) + opts(legend.position = "none") +
geom_bar(aes(x = queue_size, y = frequency, fill = factor(queue_size))) +
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/s1_demand_supply.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library(ggplot2)
data <- read.table("demand_supply.csv", header=F, sep=",")

pdf("demand_supply.pdf")
ggplot(data = data) + scale_colour_grey(name="Legend", start=0, end=0.6) +
geom_line(aes(x = V1, y = V2, color = "demand")) +
geom_line(aes(x = V1, y = V3, color = "supply")) +
scale_y_continuous("amount") +
scale_x_continuous("time")
dev.off()
10 changes: 10 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/s1_price_demand.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library(ggplot2)
data <- read.table("price_demand.csv", header=F, sep=",")

pdf("price_demand.pdf")
ggplot(data = data) + scale_colour_grey(name="Legend", start=0, end=0.6) +
geom_line(aes(x = V1, y = V2, color = "price")) +
geom_line(aes(x = V1, y = log2(V3)-3, color = "demand")) +
scale_y_continuous("amount") +
scale_x_continuous("time")
dev.off()
10 changes: 10 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/s2_price_compare.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library(ggplot2)
data <- read.table("price_data.csv", header=F, sep=",")

pdf("price_compare.pdf")
ggplot(data = data) + scale_colour_grey(name="Average price", start=0, end=0.6) +
geom_line(aes(x = V1, y = V2, color = "chickens")) +
geom_line(aes(x = V1, y = V3, color = "ducks")) +
scale_y_continuous("price") +
scale_x_continuous("time")
dev.off()
10 changes: 10 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/s2_supply_compare.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library(ggplot2)
data <- read.table("supply_data.csv", header=F, sep=",")

pdf("supply_compare.pdf")
ggplot(data = data) + scale_colour_grey(name="Supply", start=0, end=0.6) +
geom_line(aes(x = V1, y = V2, color = "chickens")) +
geom_line(aes(x = V1, y = V3, color = "ducks")) +
scale_y_continuous("amount") +
scale_x_continuous("time")
dev.off()
127 changes: 127 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/simulation1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require 'csv'

SIMULATION_DURATION = 150
NUM_OF_PRODUCERS = 10
NUM_OF_CONSUMERS = 10

MAX_STARTING_SUPPLY = 20
SUPPLY_INCREMENT = 80

COST = 5
MAX_ACCEPTABLE_PRICE = COST * 10
MAX_STARTING_PROFIT = 5
PRICE_INCREMENT = 1.1
PRICE_DECREMENT = 0.9


class Producer
attr_accessor :supply, :price
def initialize
@supply, @price = 0, 0
end

def generate_goods
@supply += SUPPLY_INCREMENT if @price > COST
end

def produce
if @supply > 0
@price *= PRICE_DECREMENT unless @price < COST
else
@price *= PRICE_INCREMENT
generate_goods
end
end
end

class Consumer
attr_accessor :demands

def initialize
@demands = 0
end

def buy
until @demands <= 0 or Market.supply <= 0
cheapest_producer = Market.cheapest_producer
if cheapest_producer
@demands *= 0.5 if cheapest_producer.price > MAX_ACCEPTABLE_PRICE
cheapest_supply = cheapest_producer.supply
if @demands > cheapest_supply
@demands -= cheapest_supply
cheapest_producer.supply = 0
else
cheapest_producer.supply -= @demands
@demands = 0
end
end
end
end
end

class Market
def self.average_price
($producers.inject(0.0) { |memo, producer| memo + producer.price}/$producers.size).round(2)
end

def self.supply
$producers.inject(0) { |memo, producer| memo + producer.supply }
end

def self.demand
$consumers.inject(0) { |memo, consumer| memo + consumer.demands }
end

def self.cheapest_producer
producers = $producers.find_all {|f| f.supply > 0}
producers.min_by{|f| f.price}
end
end

def write(name,data)
CSV.open("#{name}.csv", 'w') do |csv|
data.each do |row|
csv << row
end
end
end

$producers = []
NUM_OF_PRODUCERS.times do
producer = Producer.new
producer.price = COST + rand(MAX_STARTING_PROFIT)
producer.supply = rand(MAX_STARTING_SUPPLY)
$producers << producer
end

$consumers = []
NUM_OF_CONSUMERS.times do
$consumers << Consumer.new
end

$generated_demand = []
SIMULATION_DURATION.times {|n| $generated_demand << ((Math.sin(n)+2)*20).round }

demand_supply, price_demand = [], []

SIMULATION_DURATION.times do |t|
$consumers.each do |consumer|
consumer.demands = $generated_demand[t]
end
demand_supply << [t, Market.demand, Market.supply]

$producers.each do |producer|
producer.produce
end

price_demand << [t, Market.average_price, Market.demand]

until Market.demand == 0 or Market.supply == 0 do
$consumers.each do |consumer|
consumer.buy
end
end
end

write("demand_supply", demand_supply)
write("price_demand", price_demand)
149 changes: 149 additions & 0 deletions Chapter 4 - How to Be an Armchair Economist/simulation2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
require 'csv'

SIMULATION_DURATION = 150
NUM_OF_PRODUCERS = 10
NUM_OF_CONSUMERS = 10

MAX_STARTING_SUPPLY = Hash.new
MAX_STARTING_SUPPLY[:ducks] = 20
MAX_STARTING_SUPPLY[:chickens] = 20
SUPPLY_INCREMENT = 60

COST = Hash.new
COST[:ducks] = 23
COST[:chickens] = 12

MAX_ACCEPTABLE_PRICE = Hash.new
MAX_ACCEPTABLE_PRICE[:ducks] = COST[:ducks] * 10
MAX_ACCEPTABLE_PRICE[:chickens] = COST[:chickens] * 10

MAX_STARTING_PROFIT = Hash.new
MAX_STARTING_PROFIT[:ducks] = 15
MAX_STARTING_PROFIT[:chickens] = 15
PRICE_INCREMENT = 1.1
PRICE_DECREMENT = 0.9

class Producer
attr_accessor :supply, :price
def initialize
@supply, @supply[:chickens], @supply[:ducks] = {}, 0, 0
@price, @price[:chickens], @price[:ducks] = {}, 0, 0
end

def change_pricing
@price.each do |type, price|
if @supply[type] > 0
@price[type] *= PRICE_DECREMENT unless @price[type] < COST[type]
else
@price[type] *= PRICE_INCREMENT
end
end
end

def generate_goods
to_produce = Market.average_price(:chickens) > Market.average_price(:ducks) ? :chickens : :ducks
@supply[to_produce] += (SUPPLY_INCREMENT) if @price[to_produce] > COST[to_produce]
end

def produce
change_pricing
generate_goods
end
end

class Consumer
attr_accessor :demands

def initialize
@demands = 0
end

def buy(type)
until @demands <= 0 or Market.supply(type) <= 0
cheapest_producer = Market.cheapest_producer(type)
if cheapest_producer
@demands *= 0.5 if cheapest_producer.price[type] > MAX_ACCEPTABLE_PRICE[type]
cheapest_supply = cheapest_producer.supply[type]
if @demands > cheapest_supply then
@demands -= cheapest_supply
cheapest_producer.supply[type] = 0
else
cheapest_producer.supply[type] -= @demands
@demands = 0
end
end
end
end
end

class Market
def self.average_price(type)
($producers.inject(0.0) { |memo, producer| memo + producer.price[type]}/$producers.size).round(2)
end

def self.supply(type)
$producers.inject(0) { |memo, producer| memo + producer.supply[type] }
end

def self.demands
$consumers.inject(0) { |memo, consumer| memo + consumer.demands }
end

def self.cheaper(a,b)
cheapest_a_price = $producers.min_by {|f| f.price[a]}.price[a]
cheapest_b_price = $producers.min_by {|f| f.price[b]}.price[b]
cheapest_a_price < cheapest_b_price ? a : b
end

def self.cheapest_producer(type)
producers = $producers.find_all {|producer| producer.supply[type] > 0}
producers.min_by{|producer| producer.price[type]}
end
end

def write(name,data)
CSV.open("#{name}.csv", 'w') do |csv|
data.each do |row|
csv << row
end
end
end

$producers = []
NUM_OF_PRODUCERS.times do
producer = Producer.new
producer.price[:chickens] = COST[:chickens] + rand(MAX_STARTING_PROFIT[:chickens])
producer.price[:ducks] = COST[:ducks] + rand(MAX_STARTING_PROFIT[:ducks])
producer.supply[:chickens] = rand(MAX_STARTING_SUPPLY[:chickens])
producer.supply[:ducks] = rand(MAX_STARTING_SUPPLY[:ducks])
$producers << producer
end

$consumers = []
NUM_OF_CONSUMERS.times do
$consumers << Consumer.new
end

$generated_demand = []
SIMULATION_DURATION.times {|n| $generated_demand << ((Math.sin(n)+2)*20).round }

price_data, supply_data = [], []
SIMULATION_DURATION.times do |t|
$consumers.each do |consumer|
consumer.demands = $generated_demand[t]
end
supply_data << [t, Market.supply(:chickens), Market.supply(:ducks)]
$producers.each do |producer|
producer.produce
end
cheaper_type = Market.cheaper(:chickens, :ducks)
until Market.demands == 0 or Market.supply(cheaper_type) == 0 do
$consumers.each do |consumer|
consumer.buy cheaper_type
end
end
price_data << [t, Market.average_price(:chickens), Market.average_price(:ducks)]
end

write("price_data", price_data)
write("supply_data", supply_data)
Loading

0 comments on commit 70c1ae4

Please sign in to comment.