---
title: "LVDomo"
author: "Seong D. Yun"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{LVDemo}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(capn)
```

# Lotka-Volterra 2-D Demonstration

This vignette illustrates the use of V-approximation to approximate the
value function of a 2-D deterministic Lotka-Volterra (prey-predator) system.

This example, adapted from Joshua Abbot, is described in the `LV` dataset.

```{r example}
## 2-D Deterministic: Prey--Predator example
data("LV")

lvspace <- aproxdef(
  deg = c(20, 20),
  lb = c(0.1, 0.1),
  ub = c(1.5, 1.5),
  delta  = 0.03
)

vCLV <- vaprox(
  lvspace,
  LV$lvaproxdata[, c("xs", "ys")],
  LV$lvaproxdata[, c("xdot", "ydot")],
  LV$lvaproxdata[, "wval"]
)

lvsim <- vsim(vCLV,LV$lvsimdata[,c('xs','ys')])

# plot Biomass
plot(LV$lvsimdata[,"tseq"], LV$lvsimdata[,"xs"], type='l', lwd=2, col="blue",
     xlab="Time",
     ylab="Biomass")
lines(LV$lvsimdata[,"tseq"], LV$lvsimdata[,"ys"], lwd=2, col="red")
legend("topright", c("Prey", "Predator"), col=c("blue", "red"),
       lty=c(1,1), lwd=c(2,2), bty="n")

# plot shadow (accounting) prices
plot(LV$lvsimdata[,"tseq"],lvsim[["shadowp"]][,1],type='l', lwd=2, col="blue",
     ylim = c(-8,7),
     xlab="Time",
     ylab="Shadow price")
lines(LV$lvsimdata[,"tseq"],lvsim[["shadowp"]][,2], lwd=2, col="red")
legend("topright", c("Prey", "Predator"), col=c("blue", "red"),
       lty=c(1,1), lwd=c(2,2), bty="n")

# plot inclusive weath and value function
plot(LV$lvsimdata[,"tseq"],lvsim[["iw"]],type='l', lwd=2, col="blue",
     ylim = c(-0.5,2.5),
     xlab="Time",
     ylab="Inclusive Wealth / Value Function ($)")
lines(LV$lvsimdata[,"tseq"],lvsim[["vfun"]], lwd=2, col="red")
legend("topright", c("Inclusive Wealth", "Value Function"),
       col=c("blue", "red"), lty=c(1,1), lwd=c(2,2), bty="n")
```
