Skip to content

Commit 510a28c

Browse files
committed
Small changes for script and scenario 3
1 parent 4b2bb20 commit 510a28c

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

R/ag_production_technology.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ AgProductionTechnology_initCalc <- function(aLandLeaf, aPeriod, aScenarioInfo, a
7171
# Set constraint cost for this period
7272
if( aLandConstraintString != "" & grepl(aLandConstraintString, aLandLeaf$mName[1]) ) {
7373
aLandLeaf$mConstraintCost[aPeriod] <- aLandConstraintCost
74-
} else {
74+
} else if (length(aLandLeaf$mConstraintCost) < aPeriod) {
7575
aLandLeaf$mConstraintCost[aPeriod] <- 0.0
7676
}
7777

R/generate_price_data.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ get_prices <- function(aScenType) {
2727
prices
2828
}
2929
} else {
30-
file <- paste("./scenario-data/AgPrices_", aScenType, ".csv", sep="")
31-
prices <- suppressMessages(read_csv(system.file("extdata", file, package = "gcamland"), skip = 1))
3230

3331
# Tidy data
3432
if(aScenType == "PCHES") {
33+
prices <- read_csv("./inst/extdata/scenario-data/AgPrices_PCHES.csv", skip=1)
3534
# PCHES scenarios have subregional price information
3635
prices %>%
3736
select(-scenario, -Units) %>%
3837
gather(year, price, -region, -sector, -subregion) %>%
39-
mutate(year = as.integer(year)) ->
38+
mutate(year = as.integer(substr(year, 2, 5))) ->
4039
prices
4140
} else {
41+
file <- paste("./scenario-data/AgPrices_", aScenType, ".csv", sep="")
42+
prices <- suppressMessages(read_csv(system.file("extdata", file, package = "gcamland"), skip = 1))
4243
prices %>%
4344
select(-scenario, -Units) %>%
4445
gather(year, price, -region, -sector) %>%

R/main.R

+12-3
Original file line numberDiff line numberDiff line change
@@ -576,17 +576,26 @@ run_model <- function(aScenarioInfo, aPeriods=NULL, aVerbose=FALSE, agData=NULL)
576576
while( LandAllocator_getLandAreaByCriteria(mLandAllocator, constraints$string[i], per) > constraints$value[i] ) {
577577
currValue <- LandAllocator_getLandAreaByCriteria(mLandAllocator, constraints$string[i], per)
578578
message("Current value: ", currValue, ", Target value:", constraints$value[i])
579-
landConstraintCost <- landConstraintCost + 1e7
579+
if ( currValue < 2*constraints$value[i] ) {
580+
# Use smaller increments when we get close to solution
581+
landConstraintCost <- landConstraintCost + 1e8
582+
} else if ( currValue > 20*constraints$value[i] ) {
583+
# Use smaller increments when we get close to solution
584+
landConstraintCost <- landConstraintCost + 1e10
585+
} else {
586+
landConstraintCost <- landConstraintCost + 1e9
587+
}
588+
580589
# Constraint not met. Adjust cost and recalculate
581590
Sector_initCalc(mLandAllocator, per, aScenarioInfo, landConstraintCost, constraints$string[i])
582591
LandAllocator_initCalc(mLandAllocator, per, aScenarioInfo)
583592
LandAllocator_calcFinalLandAllocation(mLandAllocator, per, aScenarioInfo)
584593

585594
# Check that we are making progress toward meeting constraint
586-
if( LandAllocator_getLandAreaByCriteria(mLandAllocator, constraints$string[i], per) > 0.999*currValue) {
595+
if( LandAllocator_getLandAreaByCriteria(mLandAllocator, constraints$string[i], per) > 0.999999*currValue) {
587596
message("Progress is not being made. New value (",
588597
LandAllocator_getLandAreaByCriteria(mLandAllocator, constraints$string[i], per),
589-
") is less than 0.1% smaller than old value (", currValue, ")")
598+
") is less than 0.0001% smaller than old value (", currValue, ")")
590599
break
591600
}
592601
} # End while loop to meet constraint

0 commit comments

Comments
 (0)