r.r
r.r

Reputation: 7153

how to implement c# code/logic based on decision tree?

i have big decision tree with many Yes/No questions.

i need implement some code with c# and go trough this tree and get unique id as return.

if-> 1=true, A=true, C=true -> return 111;

if -> 1=true, A=true, C=false -> return 110;

how can i implement such a logic without if else? my decision tree is much, much bigger as this sample and if/else are not good solution. i think to do it in 2 methods. first Get QuestionID, and second method will get Unique ID of answer based on questions. please help me with some code ideas..

decision tree: enter image description here

Upvotes: 2

Views: 3029

Answers (2)

Amir Ismail
Amir Ismail

Reputation: 3883

if this tree is fixed you can set all alternatives in a decision table(somewhat like truth table) and save it in a dictionary then use this dictionary to return the corresponding outcome.

Dictionary["1,A"]=OutCome_1;
Dictionary["1,A,C,true"]=OutCome_2;
Dictionary["1,A,C,false"]=OutCome_3;

etc

Upvotes: 1

Dan
Dan

Reputation: 1959

You have to define a decision matrix. Your code will then look like: discriminant = decision_matrix( 1, A, B, C, 2 ) switch (discriminant ) { case outcome_1: ... break;

case outcome_2: ... break;

default: throw ... break; }

Upvotes: 0

Related Questions