-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_identical_cost.Rmd
200 lines (151 loc) · 6.58 KB
/
model_identical_cost.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "identical cost runs"
author: "Kat Leigh"
date: "2/26/2021"
output: html_document
params:
tech_c1: 1
tech_c2: 1
tech_c3: 1
tech_c4: 1
tech_c5: 1
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(ggplot2)
library(here)
library(quadprog)
library(future)
library(gtools)
library(future.apply)
library(devtools)
source("C:/Users/kleigh/OneDrive/Documents/UCSB/quotabasket_model/working_model/functions/matrixCombn.R")
source("C:/Users/kleigh/OneDrive/Documents/UCSB/quotabasket_model/working_model/functions/every_basket_all_res.R")
source("C:/Users/kleigh/OneDrive/Documents/UCSB/quotabasket_model/working_model/functions/qb_stock_m.R")
source("C:/Users/kleigh/OneDrive/Documents/UCSB/quotabasket_model/working_model/functions/qb_stock_for_optim.R")
source("C:/Users/kleigh/OneDrive/Documents/UCSB/quotabasket_model/working_model/functions/matrix_nodup.R")
```
### Preparing the inputs and outputs for `qb_stock.r`:
For each species, we need:
* r - growth rate
* K - carrying capacity
* X - starting stock
* p - price
For each technology we need:
* Catchability per species
* Cost per unit effort
# Inputs
run this entire document for each scenario you want to test
## Step 1: Input parameters
```{r}
# Species:
# s = species, r, K, X, p
s1 <- c(s=1, r=0.2, K=100, X=50, p=20)
s2 <- c(s=2, r=0.2, K=100, X=50, p=20)
s3 <- c(s=3, r=0.2, K=100, X=50, p=20)
s4 <- c(s=4, r=0.2, K=100, X=50, p=20)
s5 <- c(s=5, r=0.2, K=100, X=50, p=20)
# t = tech, q1 = catchability species 1, q2 = catchability species 2...
t1 <- c(t=1, q1=0.04, q2=0.04, q3=0.04, q4=0.04, q5 = 0.04)
t2 <- c(t=2, q1=0.04, q2=0.04, q3=0.04, q4=0.04, q5 = 0.04)
t3 <- c(t=2, q1=0.04, q2=0.04, q3=0.04, q4=0.04, q5 = 0.04)
t4 <- c(t=2, q1=0.04, q2=0.04, q3=0.04, q4=0.04, q5 = 0.04)
t5 <- c(t=2, q1=0.04, q2=0.04, q3=0.04, q4=0.04, q5 = 0.04)
# Tech cost: t = tech, cost = cost per unit effort
c1 <- c(t=1, cost= params$tech_c1)
c2 <- c(t=2, cost= params$tech_c2)
c3 <- c(t=3, cost= params$tech_c3)
c4 <- c(t=4, cost= params$tech_c4)
c5 <- c(t=5, cost= params$tech_c5)
# Baskets: binary matrix of # species x # baskets, where 1 means that species is in that basket
# b1 <- c(s1 = 1, s2 = 1, s3 = 0, s4 = 0, s5 = 0)
# b2 <- c(s1 = 0, s2 = 0, s3 = 1, s4 = 1, s5 = 1)
# b3 <- c(s1 = 0, s2 = 0, s3 = 1, s4 = 0, s5 = 0)
# b4 <- c(s1 = 0, s2 = 0, s3 = 0, s4 = 1, s5 = 0)
# b5 <- c(s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 1)
# Quotas: position = basket, number is the harvest limit.
# We want this to be 0.1 of the preceding stock under constant mortality, so this will have to change to just input constant m.
# This is a guess of optimal QB caps for each bakset. A good starting point is r/2.
cap_guess = c(0.2, 0.2)
# Years
years = 30
```
# Step 2: make a list of the inputs
```{r}
# Change these to reflect the number of species and technologies above:
species = list(s1, s2, s3, s4, s5)
tech = list(t1, t2, t3, t4, t5)
cost = list(c1, c2, c3, c4, c5)
n_baskets = 2
```
# Step 3: get all possible basket arrangements (for 2 QBs) and their associated respective optimal QB caps
# Run this!
```{r}
# get every basket combo for your context, with the optimal mortality set up
output <- every_basket(species, tech, cost, n_baskets, cap_guess, years, cores = 2)
# get the data out of the list and into a workable list
extract <- laply(output[[1]], identity)
# create dataframes for each basket arrangement,
# add a column for total profits per year and total biomass per year,
# create objects for the total NPV, average profits, and average biomass for each basket arrangement
for(i in 1:length(extract)){
dfoutput <- cbind(as.data.frame(c(extract[[i]]$stock)),
as.data.frame(c(extract[[i]]$harvest)),
as.data.frame(c(extract[[i]]$effort)),
as.data.frame(c(extract[[i]]$profit_per_t)),
as.data.frame(c(extract[[i]]$mortality1)),
as.data.frame(c(extract[[i]]$mortality2)))
dfoutput <- dfoutput %>%
mutate(tot_profit = t_1+t_2+t_2.1+t_2.2+t_2.3,
tot_biomass = s_1+s_2+s_3+s_4+s_5)
assign(paste("basket",i, sep = "."),dfoutput)
assign(paste("NPV_basket",i, sep = "."),sum(dfoutput$tot_profit, na.rm = TRUE))
assign(paste("av_pft_basket",i, sep = "."),mean(dfoutput$tot_profit, na.rm = TRUE))
assign(paste("av_biomass",i, sep = "."),mean(dfoutput$tot_biomass))
}
# stick all the NVPs, profits, and biomasses into a df, arrange from highest to lowest to find the optimal arrangement for eac of these 3 criterion
NPVs <- mget(ls(pattern="NPV_basket")) %>%
unlist() %>%
as.data.frame() %>%
tibble::rownames_to_column("basket") %>%
arrange(-.)
av_pfts <- mget(ls(pattern="av_pft_basket")) %>%
unlist() %>%
as.data.frame() %>%
tibble::rownames_to_column("basket") %>%
arrange(-.)
av_stocks <- mget(ls(pattern="av_biomass[.]")) %>%
unlist() %>%
as.data.frame() %>%
tibble::rownames_to_column("basket") %>%
arrange(-.)
# identify the optimal basket arrangements
optimal_basket_NPV <-
head(NPVs,1)
optimal_basket_av_pft <-
head(av_pfts,1)
optimal_basket_stock <-
head(av_stocks,1)
optimals <- rbind(optimal_basket_av_pft, optimal_basket_NPV, optimal_basket_stock)
```
# Step 5: save results of the optimal basket arrangements and the summaries
```{r}
# this is designed to automatically extract the 3 optimal basket arrangements as well as their summary info.
path_store <- "working_model/price and cost explore/"
csvname <- paste(c1[2],".",c2[2],".",c3[2],".",c4[2],".",c5[2],".csv",sep="")
csvfile <- paste(path_store, "opt_NPV_r.2.2.2.3.3_c", csvname, sep="")
write.table(get(str_split(optimal_basket_NPV$basket, pattern = "_")[[1]][2]),csvfile, row.names=FALSE, sep=",")
path_store <- "working_model/price and cost explore/"
csvname <- paste(c1[2],".",c2[2],".",c3[2],".",c4[2],".",c5[2],".csv",sep="")
csvfile <- paste(path_store, "opt_av_pft_r.2.2.2.3.3_c", csvname, sep="")
write.table(get(str_split(optimal_basket_av_pft$basket, pattern = "_")[[1]][3]),csvfile, row.names=FALSE, sep=",")
path_store <- "working_model/price and cost explore/"
csvname <- paste(c1[2],".",c2[2],".",c3[2],".",c4[2],".",c5[2],".csv",sep="")
csvfile <- paste(path_store, "opt_stock_r.2.2.2.3.3_c", csvname, sep="")
write.table(get(paste("basket",str_split(optimal_basket_stock$basket, pattern = "[.]")[[1]][2], sep = '.')),csvfile, row.names=FALSE, sep=",")
path_store <- "working_model/price and cost explore/"
csvname <- paste(c1[2],".",c2[2],".",c3[2],".",c4[2],".",c5[2],".csv",sep="")
csvfile <- paste(path_store, "opt_summaries_r.2.2.2.3.3_c", csvname, sep="")
write.table(optimals,csvfile, row.names=FALSE, sep=",")
```