File: otbDecisionTreeBuild.cxx

package info (click to toggle)
otb 5.8.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,496 kB
  • ctags: 40,282
  • sloc: cpp: 306,573; ansic: 3,575; python: 450; sh: 214; perl: 74; java: 72; makefile: 70
file content (113 lines) | stat: -rw-r--r-- 3,466 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "itkMacro.h"
#include "otbDecisionTree.h"

enum WheatTypes { WinterWheat, SummerWheat };

int otbDecisionTreeBuild(int itkNotUsed(argc), char* itkNotUsed(argv) [])
{
/** We build the following decision tree

                                         -------------
                  ( DOY LT 100  )
                   -------------
              Yes  -/    --   No
                --/      \--
           -------------  -------------
          ( NDVI GT 0.7 )    ( NDVI GT 0.7 )
           -------------  -------------
         / --           /\
        Yes  -/    \-  No         Yes-/   \- No
           -/         \     -/     \-
         WW           SW           SW        WW

  */
  typedef bool AttributeValueType;

  typedef otb::DecisionTree< AttributeValueType, WheatTypes > DecisionTreeType;

  DecisionTreeType::Pointer decisionTree = DecisionTreeType::New();
  DecisionTreeType::Pointer doyLT100Tree = DecisionTreeType::New();
  DecisionTreeType::Pointer doyGT100Tree = DecisionTreeType::New();


  decisionTree->SetAttribute( 0 );
  decisionTree->AddBranch( false, doyGT100Tree );
  decisionTree->AddBranch( true, doyLT100Tree );

  doyLT100Tree->SetAttribute( 1 );
  doyLT100Tree->AddBranch( false, SummerWheat );
  doyLT100Tree->AddBranch( true, WinterWheat );

  doyGT100Tree->SetAttribute( 1 );
  doyGT100Tree->AddBranch( false, WinterWheat );
  doyGT100Tree->AddBranch( true, SummerWheat );

  // Build some examples for testing

  DecisionTreeType::ExampleType ww_in_summer; //(DoY<100), (NDVI>0.7)
  ww_in_summer.push_back(false);
  ww_in_summer.push_back(false);

  DecisionTreeType::ExampleType ww_in_winter;
  ww_in_winter.push_back(true);
  ww_in_winter.push_back(true);

  DecisionTreeType::ExampleType sw_in_winter;
  sw_in_winter.push_back(true);
  sw_in_winter.push_back(false);

  DecisionTreeType::ExampleType sw_in_summer;
  sw_in_summer.push_back(false);
  sw_in_summer.push_back(true);

  std::cout << "ww_in_summer" << std::endl;
  if( decisionTree->Decide( ww_in_summer ) != WinterWheat )
    {
    std::cerr << "ww_in_summer yields " << decisionTree->Decide( ww_in_summer ) << std::endl;
    return EXIT_FAILURE;
    }

  std::cout << "sw_in_winter" << std::endl;
  if( decisionTree->Decide( sw_in_winter ) != SummerWheat )
    {
    std::cerr << "sw_in_winter yields " << decisionTree->Decide( sw_in_winter ) << std::endl;
    return EXIT_FAILURE;
    }

  std::cout << "sw_in_summer" << std::endl;
  if( decisionTree->Decide( sw_in_summer ) != SummerWheat )
    {
    std::cerr << "sw_in_summer yields " << decisionTree->Decide( sw_in_summer ) << std::endl;
    return EXIT_FAILURE;
    }


  std::cout << "ww_in_winter" << std::endl;
  if( decisionTree->Decide( ww_in_winter ) != WinterWheat )
    {
    std::cerr << "ww_in_winter yields " << decisionTree->Decide( ww_in_winter ) << std::endl;
    return EXIT_FAILURE;
    }




  return EXIT_SUCCESS;
}