---
title: "testing_constrained"
output: html_document
---

```{r}
library(data.table)
library(dplyr)
devtools::load_all(".")
```

# Test 1. Toy data from the spaital availability (2023) paper.
## TOT CONST
```{r}
# Toy travel matrix and land use data
travel_matrix <- expand.grid(from_id = c("A", "B", "C"),
                             to_id = c("1", "2", "3")) |>
  dplyr::mutate(travel_time = c(15, 30, 100, 30, 15, 100, 100, 100, 15)) |>
  data.table::data.table()

land_use_data <- data.table::data.table(
  id = c("A", "B", "C","1", "2", "3"),
  population = c(50000, 150000, 10000),   # supply
  jobs = c(100000, 100000, 10000)     # demand
)

# Decay function
decay <- decay_exponential(decay_value = 0.1)
```


```{r}
active_det_false <- constrained_accessibility(
  constraint = "total",
  travel_matrix, land_use_data,
  travel_cost = "travel_time",
  decay_function = decay,
  demand = NULL,
  supply = "jobs",
  detailed_results = FALSE,
  active = TRUE)

active_det_false
```


```{r}
passive_det_false <- constrained_accessibility(
  constraint = "total", 
  travel_matrix = travel_matrix, 
  land_use_data = land_use_data,
  travel_cost = "travel_time",
  decay_function = decay,
  demand = "population",
  supply = NULL,
  detailed_results = FALSE,
  active = FALSE
  )

passive_det_false
```


```{r}
active_det_true <- constrained_accessibility(
  constraint = "total", 
  travel_matrix, 
  land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay,
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE
  )

active_det_true
```


```{r}
passive_det_true <- constrained_accessibility(constraint = "total", travel_matrix, land_use_data,
                               travel_cost = "travel_time",
                               decay_function = decay,
                               demand = "population",
                               supply = NULL,
                               detailed_results = TRUE,
                               active = FALSE)
passive_det_true
```

```{r}
passive_det_true$demand |> sum() #POPULATION 210,000
active_det_true$supply |> sum() #jobs 210,000
passive_det_false$demand |> sum() #POPULATION 210,000
active_det_false$supply |> sum() #jobs 210,000

active_det_true$kappa_total |> sum()
passive_det_true$hatkappa_total |> sum()
```

## SINGLE CONST
```{r}
active_det_false <- constrained_accessibility(constraint = "singly", travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay,
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)
active_det_false
```
Should be: 
A 66833.466			
B	133203.363			
C	9963.171	

```{r}
passive_det_false <- constrained_accessibility(constraint = "singly", travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay,
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = FALSE)
passive_det_false
```
Should be:
1	68261.682			
2	131775.520			
3	9962.798	

```{r}
active_det_true <- constrained_accessibility(constraint = "singly", travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay,
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_true
```
e.g., for summing As... 59900.642536+6922.691048+10.132187 = 66833.47

```{r}
passive_det_true <- constrained_accessibility(constraint = "singly", travel_matrix, land_use_data,
                               travel_cost = "travel_time",
                               decay_function = decay,
                               demand = "population",
                                supply = "jobs",
                               detailed_results = TRUE,
                               active = FALSE)
passive_det_true
```

Checks!
```{r}
passive_det_true$demand |> sum() #POPULATION 210,000
active_det_true$supply |> sum() #jobs 210,000
passive_det_false$demand |> sum() #POPULATION 210,000
active_det_false$supply |> sum() #jobs 210,000

active_det_true$kappa_singly |> sum()
passive_det_true$hatkappa_singly |> sum()
```


```{r}
active_det_true$kappa_singly |> sum() #should be the number of destinations
active_det_true[, sum(kappa_singly), by = to_id]  #should all be 1

passive_det_true$hatkappa_singly |> sum() #should be the number of origins
passive_det_true[, sum(hatkappa_singly), by = from_id]  #should all be 1
```

# Test 2. Testing results with total/singly constrained toy example in 2025 paper
Data:
```{r test-data}
travel_matrix <- expand.grid(from_id = c("1", "2", "3"),
                  to_id = c("1", "2", "3")) |>
  mutate(travel_time = c(10, 30, 15, 30, 10, 25, 15, 25, 10)) |> data.table()

land_use_data <- data.table(
  id = c("1", "2", "3"),
  population = c(4, 10, 6),
  jobs = c(160, 150, 180)
)
```

## Total constrained--ACTIVE
Checking for decay_power scenario 3:  
```{r}
active_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_total |> sum()
```
Should be:
1	172.0653			
2	131.6267			
3	186.3080

Checking for decay_power scenario 2:  
```{r}
active_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_total |> sum()
```
Should be:
1	172.6721			
2	132.2474			
3	185.0805	

Checking for decay_power scenario 0.1:  
```{r}
active_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = NULL,
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_total |> sum()
```
Should be:
1	164.0802			
2	160.6921			
3	165.2277

## Total constrained--PASSIVE
Checking for decay_power scenario 3:  
```{r}
passive_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = NULL,
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = NULL,
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_total |> sum()
```
Should be:
1	5.017774			
2	8.595749			
3	6.386477	

Checking for decay_power scenario 2:  
```{r}
passive_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = NULL,
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = NULL,
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_total |> sum()
```
Should be:
1	5.446623			
2	7.986306			
3	6.567071	

Checking for decay_power scenario 0.1:  
```{r}
passive_det_false <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = NULL,
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="total",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = NULL,
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_total |> sum()
```
Should be:
1	6.598332			
2	6.717223			
3	6.684444	

## Singly constrained--ACTIVE
Checking for decay_power scenario 3:  
```{r}
active_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_singly |> sum()  #should be the number of destinations or origins

round(active_det_false$supply / land_use_data$population, 6)
```
Should be:
1	133.4687			
2	166.7813			
3	189.7499	

And the per capita should be:
33.36718 16.67813 31.62499

Checking for decay_power scenario 2:  
```{r}
active_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_singly|> sum()  #should be the number of destinations or origins

round(active_det_false$supply / land_use_data$population, 6)
```
Should be:
1	122.2546			
2	185.0957			
3	182.6497	

And the per capita should be:
30.56365 18.50957 30.44161

Checking for decay_power scenario 0.1:  
```{r}
active_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

active_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)
active_det_false
active_det_true

active_det_false$supply |> sum()
active_det_true$supply |> sum()
active_det_true$kappa_singly |> sum()  #should be the number of destinations or origins

round(active_det_false$supply / land_use_data$population, 6)
```
Should be:
1	98.84766			
2	241.87721			
3	149.27513	

And the per capita should be:
24.71192 24.18772 24.87919

## Singly constrained--PASSIVE
Checking for decay_power scenario 3:  
```{r}
passive_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_singly |> sum()
```


Checking for decay_power scenario 2:  
```{r}
passive_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_singly |> sum()
```

Checking for decay_power scenario 0.1:  
```{r}
passive_det_false <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = FALSE)

passive_det_true <- constrained_accessibility(constraint="singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = FALSE)
passive_det_false
passive_det_true

passive_det_false$demand |> sum()
passive_det_true$demand |> sum()
passive_det_true$hatkappa_singly |> sum()
```

# Test 3. Testing results with doubly constrained toy example in 2025 paper
```{r}
# Toy dataset where totals match (sum demand = sum supply = 20)
travel_matrix <- expand.grid(from_id = c("1", "2", "3"),
                             to_id = c("1", "2", "3")) |>
  dplyr::mutate(travel_time = c(10, 30, 15, 30, 10, 25, 15, 25, 10)) |>
  data.table()

land_use_data <- data.table(
  id = c("1", "2", "3"),
  population = c(4, 10, 6),
  jobs = c(7, 5, 8)
)
```


Checking for decay_power scenario 3:  
```{r}
active_det_true <- constrained_accessibility(constraint="doubly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 3),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)

active_det_true

active_det_true$flow |> sum()
active_det_true$kappa_doubly |> sum()  #should be the number of destinations or origins
```
Should be:
1	1	3.23585899	
1	2	0.01032226	
1	3	0.75565683	
2	1	2.13260167	
2	2	4.95932483	
2	3	2.90443915	
3	1	1.63153933	
3	2	0.03035291	
3	3	4.33990401	

So... this matches table 10, but I accidentally flipped the columns. like 2->1 in the table should be 1->2. 
 

Checking for decay_power scenario 2:  
```{r}
active_det_true <- constrained_accessibility(constraint="doubly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)

active_det_true

active_det_true$flow |> sum()
active_det_true$kappa_doubly |> sum()  #should be the number of destinations or origins
```

Checking for decay_power scenario 0.1:  
```{r}
active_det_true <- constrained_accessibility(constraint="doubly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 0.1),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = TRUE,
                                active = TRUE)

active_det_true

active_det_true$flow |> sum()
active_det_true$kappa_doubly |> sum()  #should be the number of destinations or origins
```


# Test 4. Testing singly constrained's equality to spatial_availbility() and 2SFCA(). 
```{r}
travel_matrix <- expand.grid(from_id = c("1", "2", "3"),
                  to_id = c("1", "2", "3")) |>
  mutate(travel_time = c(10, 30, 15, 30, 10, 25, 15, 25, 10)) |> data.table()

land_use_data <- data.table(
  id = c("1", "2", "3"),
  population = c(4, 10, 6),
  jobs = c(160, 150, 180)
)
```

Spatial_availability() equality:
```{r}
constrained_accessibility(constraint = "singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

spatial_availability(travel_matrix,
    land_use_data,
    opportunity = "jobs",
    travel_cost = "travel_time",
    demand = "population",
    decay_function = decay_power(decay_value = 2),
    alpha = 1
  )
```

floating_catchment_area(method="2sfca") equality:
```{r}
result_singly <- constrained_accessibility(constraint = "singly",
                          travel_matrix, land_use_data,
                                travel_cost = "travel_time",
                                decay_function = decay_power(decay_value = 2),
                                demand = "population",
                                supply = "jobs",
                                detailed_results = FALSE,
                                active = TRUE)

round(result_singly$supply / land_use_data$population, 6)
  
floating_catchment_area(
    travel_matrix,
    land_use_data,
    opportunity = "jobs",
    travel_cost = "travel_time",
    demand = "population",
    method = "2sfca",
    decay_function = decay_power(decay_value = 2)
  )

```
yay!
