Thursday, February 13, 2020

TP 5 material

Arbre d'analyse de décision

1. Fichier YAML
name: newProdTree
type: decision
capt: root_D
AnnulTest:
  type: terminal
  capt: annulTest_T
  payoff: 0.0
TestProd:
  type: chance
  capt: testProd_C
  testFail:
    type: terminal
    capt: testFail_T
    p: 0.7
    payoff: -100000
  testSucc:
    type: decision
    capt: testSucc_D
    p: 0.3
    SmallPlant:
        type: chance
        capt: smallPlant_C
        noCompet:
          type: terminal
          capt: smallNoCompet_T
          p: 0.6
          payoff: 310000 
        compet:
          type: terminal
          capt: smallCompet_T
          p: 0.4
          payoff: -110000
    LargePlant:
        type: chance
        capt: largePlant_C
        noCompet:
          type: terminal
          capt: largeNoCompet_T
          p: 0.6
          payoff: 700000
        compet:
          type: terminal
          capt: largeCompet_T
          p: 0.4
          payoff: -140000


2. Script R
fileName <- 'exCoursNewProd.yaml'
cat(readChar(fileName, file.info(fileName)$size))#on s'assure q ttVaB1
library(data.tree)
library(yaml)
l <- yaml.load_file(fileName)
decisTree <- as.Node(l)
#arbre avant resolution
print(decisTree, "type", "payoff", "p")

#resolution
payoff <- function(node){
  if (node$type == 'chance'){
    node$payoff <- sum(sapply(node$children, function(child) child$payoff * child$p))}
  else if (node$type == 'decision'){
    vChildrenPayoff <-sapply(node$children, function(child) child$payoff)

    node$payoff <- max(vChildrenPayoff)}
}

decisTree$Do(payoff, traversal = "post-order", filterFun = isNotLeaf)

#resultat
print("Decision à entreprendre:")
affichPathDecis <- function(node)  if(node$payoff==node$parent$payoff)
                                      cat(node$name,' --> ')
decisTree$Do(affichPathDecis, filterFun = isNotRoot, traversal = "pre-order")
cat('EMV -->',decisTree$payoff,'\n')


Bibliographie/Further reading

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.