-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjective_function.R
69 lines (60 loc) · 2.09 KB
/
objective_function.R
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
rm(list=ls())
setwd("/home/tomay/r_work/ga-marxan")
## get functions
source("marxan_inits.R")
source("ga/init.R")
library(data.table)
## get marxan inputs data-set
#data.path <- "inputs/bigData"
data.path <- "inputs/MarxanData/input"
## small data-set
## data.path <- "small"
pu <- read.csv(file.path(data.path, "pu.dat"),
header=TRUE, na.strings="NA",
dec=".", strip.white=TRUE, as.is=TRUE)
bnd <- read.csv(file.path(data.path, "bound.dat"),
header=TRUE, na.strings="NA",
dec=".", strip.white=TRUE)
puv_df <- read.csv(file.path(data.path, "puvspr.dat"),
header=TRUE, na.strings="NA",
dec=".", strip.white=TRUE)
species <- read.csv(file.path(data.path,"spec.dat"),
header=TRUE, na.strings="NA",
dec=".", strip.white=TRUE)
## BLM is user defined for entire run
blm <- 1 # user defined
puv <- data.table(puv_df)
## sum species total distribution across planning region
## only necessary once per system
spp_sums <- aggregate(puv$amount, by=list(species=puv$species), FUN=sum)
model.score <- function(m) {
m <- which(m)
## construct objective function scores
cost_penalty <- cost(m, pu)
boundary_penalty <- boundary(m, bnd)
species_cost <- species_penalty(m, puv, species)
# debug #print (cost_penalty)
# debug #print (boundary_penalty)
# debug #print (species_cost)
## calc total objective function score
-(cost_penalty + (blm * boundary_penalty) + species_cost)
}
num.total.pu <- nrow(pu)
## key function for running GA
find.model <- function(f) {
run.ga(n.pu=50, n.pu.tot=num.total.pu,
N=100, n.gens=50, s=15, p.mutate=0.05,
p.sex=0.5, p.rec=0.5, fitness=f)
}
result <- find.model(model.score)
## to profile the code:
set.seed(1)
## **************************************************
## ************* to profile simulation **************
## **************************************************
Rprof(filename = "Rprof.out")
set.seed(1)
res <- find.model(model.score)
Rprof(NULL)
summaryRprof("Rprof.out")
## **************************************************