|
|
(14 intermediate revisions by 2 users not shown) |
Line 1: |
Line 1: |
| ==Decision==
| | [[Category:Sandbox]] |
| ===Old R-code===
| |
| | |
| <rcode>
| |
| library(xtable)
| |
| library(OpasnetUtils)
| |
| | |
| var1 <- data.frame(c = c("A", "A", "C"), d = c("D", "E", "E"), Result = 4:6)
| |
| cat("var1\n")
| |
| print(xtable(var1), type = "html")
| |
| | |
| var2 <- data.frame(c = c("A", "A", "C"), d = c("D", "E", "E"), Result = 4:6)
| |
| cat("var2\n")
| |
| print(xtable(var2), type = "html")
| |
| | |
| dec <- data.frame(
| |
| Decision = rep(c("Decision 1", "Decision 2"), each = 2),
| |
| Option = c("OptA", "OptB"),
| |
| Variable = c("var1", "var1", "var1", "var2"),
| |
| Cell = c(" c: A; d: E", " d: D"),
| |
| Change = c("Replace", "Multiply"),
| |
| Result = 7:10)
| |
| | |
| cat("Decision\n")
| |
| print(xtable(dec), type = "html")
| |
| | |
| out <- decisions.apply(dec)
| |
| | |
| cat("Variable", names(out)[1], "output\n")
| |
| print(xtable(out[[1]]), type = 'html')
| |
| | |
| cat("Variable", names(out)[2], "output\n")
| |
| print(xtable(out[[2]]), type = 'html')
| |
| | |
| </rcode>
| |
| | |
| {{attack|# |Why does the var2 outcome show row 3 OptB A E 50, because the decision should be applied only for row 1 OptB A D 4 ? There is a bug somewhere. |--[[User:Jouni|Jouni]] 16:12, 16 May 2012 (EEST)}}
| |
| | |
| | |
| ===Test Code===
| |
| | |
| {{comment|# |Oletko saanut bugin selville? Luulen, että decisions.apply jossain vaiheessa sekoittaa rivit niin, että ensimmäiselle riville tarkoitettu kertolasku kohdistuukin kolmannelle riville. Mutta miksi?|--[[User:Jouni|Jouni]] 08:53, 29 June 2012 (EEST)}}
| |
| :{{comment|# |Itsekin ajattelin samaa, mutta en ole vielä ymmärtänyt miksi se toimii niin.. Minulla meni pari päivää jahdatessa tuon yhden virheilmoituksen syytä, kunnes Teemu selvensi minulle että se on tarkoituskin tulla, joskin virheilmoituksessa on kuulemma parantamisen varaa. Nyt vasta alan ymmärtää paremmin miten tuo decisions.apply oikein toimii.|--[[User:Mori|Mori]] 17:06, 3 July 2012 (EEST)}}
| |
| | |
| {{comment|# |No, nyt taisi selvitä mikä siinä on ongelma.. Huomasin, että Var2 output taulussa ei ole Decision1 saraketta, mutta kun datoja muuttelee hiukan ja antaa sille kyseisen sarakkeen, laskutoimitukset tuntuvat menevän oikein. Ilmeisesti sen sarakkeen puuttuminen hajottaa jonkun noista loopeista (ainakin Teemun mukaan) joka johtaa laskutoimituksen virheellisyyteen |--[[User:Mori|Mori]] 14:47, 9 July 2012 (EEST)}}
| |
| | |
| <rcode>
| |
| library(xtable)
| |
| library(OpasnetUtils)
| |
| | |
| var1 <- data.frame(c = c("A", "A", "C"), d = c("D", "E", "E"), Result = 4:6)
| |
| cat("var1\n")
| |
| print(xtable(var1), type = "html")
| |
| | |
| var2 <- data.frame(c = c("A", "A", "C"), d = c("D", "E", "E"), Result = 4:6)
| |
| cat("var2\n")
| |
| print(xtable(var2), type = "html")
| |
| | |
| dec <- data.frame(
| |
| Decision = rep(c("Decision 1", "Decision 2"), each = 2),
| |
| Option = c("OptA", "OptB"),
| |
| Variable = c("var2", "var1", "var1", "var2"),
| |
| Cell = c(" c: A; d: E", " d: D"),
| |
| Change = c("Replace", "Multiply"),
| |
| Result = 7:10)
| |
| | |
| cat("Decision\n")
| |
| print(xtable(dec), type = "html")
| |
| | |
| #out <- decisions.apply(dec)
| |
| | |
| assessment = NULL
| |
| out <- as.list(unique(dec$Variable))
| |
| names(out) <- as.character(unique(dec$Variable))
| |
| | |
| for(variables in unique(dec$Variable)) { # Take one variable at a time.
| |
| dec.var <- dec[dec$Variable == variables, ] # dec.var = variable-specific decisions
| |
| scenarios <- data.frame(temp = 1)
| |
| | |
| for(decisions in unique(dec.var$Decision)) { # Add BAU option to each decision and merge decisions.
| |
| temp <- as.character(dec.var[dec.var$Decision == decisions, "Option"])
| |
| temp <- data.frame(Options = c(temp, "BAU"))
| |
| colnames(temp) <- decisions
| |
| scenarios <- merge(scenarios, temp)
| |
| }
| |
| | |
| if(!is.null(assessment)) {
| |
| var <- assessment@vars[[as.character(dec.var$Variable[1])]]
| |
| } else {
| |
| if(exists(as.character(dec.var$Variable[1])))
| |
| {var <- get(as.character(dec.var$Variable[1]))
| |
| } else {
| |
| stop()
| |
| }
| |
| }
| |
| var <- merge(scenarios[colnames(scenarios) != "temp"], var)
| |
| | |
| #print(xtable(dec.var), type = 'html')
| |
| #print(xtable(var), type = 'html')
| |
| | |
| for(s in 1:nrow(dec.var)) {cat(s)}
| |
| for(s in 1:nrow(dec.var)) { # Each row in decision handled separately
| |
| cell <- gsub("^[ \t]+|[ \t]+$", "", as.character(dec$Cell[s])) # Leading and trailing whitespaces removed.
| |
| cell <- gsub(":[ \t]+", ":", cell) # Whitespaces removed after :
| |
| cell <- gsub(";[ \t]+", ";", cell) # Whitespaces removed after ;
| |
| cell <- gsub("[ \t]+:", ":", cell) # Whitespaces removed before :
| |
| cell <- gsub("[ \t]+;", ";", cell) # Whitespaces removed before ;
| |
| cell <- strsplit(cell, split = ";") # Separate cell identifiers (indices and locations)
| |
| cell <- strsplit(cell[[1]], split = ":")
| |
| cond <- as.character(dec.var$Decision[s])
| |
| cond <- var[, cond] == as.character(dec.var$Option[s]) # Only the rows with the relevant option.
| |
| for(r in 1:length(cell)) { # All Cell conditions extracted and combined with AND.
| |
| cond <- cond * (var[, cell[[r]][1]] == cell[[r]][2])
| |
| }
| |
| cond <- as.logical(cond)
| |
| if(dec.var$Change[s] == "Replace") {var[cond, "Result"] <- dec.var$Result[s]}
| |
| if(dec.var$Change[s] == "Add") {var[cond, "Result"] <- dec.var$Result[s] + var[cond, "Result"]}
| |
| if(dec.var$Change[s] == "Multiply") {var[cond, "Result"] <- dec.var$Result[s] * var[cond, "Result"]}
| |
| }
| |
| | |
| | |
| out[[variables]] <- var
| |
| }
| |
| | |
| cat("Variable", names(out)[1], "output\n")
| |
| print(xtable(out[[1]]), type = 'html')
| |
| | |
| cat("Variable", names(out)[2], "output\n")
| |
| print(xtable(out[[2]]), type = 'html')
| |
| | |
| | |
| </rcode>
| |
| | |
| | |
| ==emfBoilerit==
| |
| | |
| ===Vanha koodi===
| |
| <rcode>
| |
| library(OpasnetUtils)
| |
| library(xtable)
| |
| | |
| data <- op_baseGetData("opasnet_base", "Op_fi3016")
| |
| data <- tidy(data)
| |
| | |
| emfBoilerit <- new("ovariable",
| |
| name = "emfBoilerit",
| |
| data = data, direction = "wide", objname = "emfBoilerit")
| |
| )
| |
| | |
| objects.put(emfBoilerit)
| |
| | |
| print(xtable(emfBoilerit@data), type = 'html')
| |
| cat("Muuttuja alustettu. Kopioi ajosivun osoitteen avain talteen tulevaa käyttöä varten.\n")
| |
| </rcode>
| |
| | |
| ===Uusi koodi===
| |
| | |
| ==== KAAVA ====
| |
| <math>EF = EFs/Sa*S</math>
| |
| EF = Laskettu päästökerroin
| |
| EFs = Päästökerroin paikallaan olevien polttomoottorien päästökertoimet taulukossa
| |
| Sa = Päästökertoimen laskennassa käytetty vakiorikkipitoisuus
| |
| S = polttoaineen rikkipitoisuus
| |
| | |
| <rcode variables="
| |
| name:Polttoaine|description:Mikä polttoaine on kyseessä?|type:selection|options:'Masuunikaasu';Masuunikaasu;'Koksiuunikaasu';Koksiuunikaasu;'Kaatopaikkakaasu';Kaatopaikkakaasu;'Maakaasu';Maakaasu;'Nestekaasu - Butaani';Nestekaasu - Butaani;'Nestekaasu - Propaani';Nestekaasu - Propaani;'Raskas polttoöljy';Raskas polttoöljy;'Diesel';Diesel;'Jäteöljy';Jäteöljy|default:'Diesel'|
| |
| name:Sa|description:Päästökertoimen laskennassa käytetty vakiorikkipitoisuus|default:1|
| |
| name:S|description:Polttoaineen rikkipitoisuus|default:1
| |
| ">
| |
| | |
| library(OpasnetUtils)
| |
| library(xtable)
| |
| | |
| riippuvuudet <- data.frame(
| |
| Name = c("EFs", "Sa", "S")
| |
| )
| |
| | |
| funktio <- function(dependencies, ...) {
| |
| data <- op_baseGetData("opasnet_base", "Op_fi3016")
| |
| data <- tidy(data)
| |
| | |
| out$Result <- as.numeric(out$Result) * EFs / Sa * S
| |
| out <- dropall(out)
| |
| return(out)
| |
| }
| |
| | |
| emfBoilerit <- new("ovariable",
| |
| name = "emfBoilerit",
| |
| data = data,
| |
| dependencies = riippuvuudet,
| |
| formula = funktio
| |
| )
| |
| | |
| objects.put(emfBoilerit)
| |
| temp <- EvalOutput(emfBoilerit)
| |
| | |
| out <- temp@output
| |
| | |
| print(xtable(emfBoilerit@data), type = 'html')
| |
| cat("Muuttuja alustettu. Kopioi ajosivun osoitteen avain talteen tulevaa käyttöä varten.\n")
| |
| </rcode>
| |