DecisionVariable

We found 10 examples in language CSharp for this search. You will see 21 fragments of code.

        public DecisionVariable getDecisionVarById(string dId)
        {
            foreach (DecisionVariable item in dvList)
            {
                if (item.ID == dId)
                {
                    return item;
                }
            }
            return null;
        }
        public void addDV(DecisionVariable newDV)
        {
            dvList.Add(newDV);
        }
        public bool removeDecisionVarById(string dId)
        {
            DecisionVariable aim = getDecisionVarById(dId);
            if (aim != null)
            {
                return dvList.Remove(aim);
            }
            return false;
        }
        public bool removeDecisionVar(DecisionVariable aim)
        {
            return dvList.Remove(aim);
        }

// ***********************************************************************
// Assembly         : GFS.ClassificationBLL
// Author           : Ricker Yan
// Created          : 09-01-2017
//
// Last Modified By : Ricker Yan
// Last Modified On : 09-01-2017
// ***********************************************************************
// <copyright file="DecisionVariable.cs" company="BNU">
//     Copyright (c) BNU. All rights reserved.
// </copyright>
// <summary>决策变量类(已弃用)</summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFS.ClassificationBLL
{
    public class DecisionVariable
    {
        public string variableName;
        public string fileName;
        public int bandIndex=-1;
        public DecisionVariable()
        { }
        public DecisionVariable(string variable,string file,int bandIndex)
        {
            this.variableName = variable;
            this.fileName = file;
            this.bandIndex = bandIndex;
        }
        public DecisionVariable(string variable)
        {
            this.variableName = variable; 
        }
    }
}


        public void TrainAlgorithm(List<Item_Weather> itemWeather)
        {
            foreach (var item in itemWeather)
            {
                object[] info =
                {
                    item.Weather.Weather.Icon,
                    item.Weather.Temperature.Value,
                    item.Weather.Humidity.Value,
                    item.Weather.Wind.Speed.Value,
                    item.Weather.Pressure.Value,
                    item.Projection
                };
                Data.Rows.Add(info);
            }

            Codebook = new Codification(Data);
            
            DataTable symbols = Codebook.Apply(Data, "Weather", "Temperature", "Humidity", "Wind", "Pressure","Projection");
            int[][] inputs = symbols.ToArray<int>("Weather", "Temperature", "Humidity", "Wind", "Pressure");
            int[] outputs = symbols.ToArray<int>("Projection");

            ID3Learning learning = new ID3Learning(Tree);
            learning.Learn(inputs, outputs);
        }

        public string CheckItemProjection(Item_Weather itemToCheck)
        {
            var score = itemToCheck.WeatherScore;
            var newWeather = Algorithms.WeatherFromScore(score);

            var translation = Codebook.Translate(newWeather.Weather.Icon,
                newWeather.Temperature.Value, newWeather.Humidity.Value,
                newWeather.Wind.Speed.Value, newWeather.Pressure.Value);

            var decision = Tree.Decide(translation);

            var answer = Codebook.Translate("Projection", decision) ?? "false";
            return answer;
        }

        public static void AddMemoryItem(string message, string user, string userName)
        {

            MemoryNode MemoryInput = new MemoryNode();
            MemoryInput.UserID = user;
            MemoryInput.UserName = userName;
            MemoryInput.MemoryString = message;

            Utils.SetMemoryAccessType("Memory");
            Utils.ReadMemory();
            Utils.WriteMemory();
        }

        public static void TestAccord()
        {

            /*
             * http://crsouza.com/2012/01/decision-trees-in-c/
             * */


            DataTable data = new DataTable("Memory");

            /*add People names/ID to columns dynamically*/
            data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");

            /*possibly add sentences to this?
            maybe keywords*/
            data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
            data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
            data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
            data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
            data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
            data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
            data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
            data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
            data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
            data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
            data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");

            // Create a new codification codebook to
            // convert strings into integer symbols
            Codification codebook = new Codification(data,"Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");

            /* NO IDEA FOR THIS */
            DecisionVariable[] attributes =
            {
                new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain)
                new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool)  
                new DecisionVariable("Humidity",    2), // 2 possible values (High, normal)    
                new DecisionVariable("Wind",        2)  // 2 possible values (Weak, strong) 
            };


            /* For possible values, make it one so it narrows to one individual fact about a word*/
            int classCount = 2; // 2 possible output values for playing tennis: yes or no


            DecisionTree tree = new DecisionTree(attributes, classCount);

            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(tree);

            // Translate our training data into integer symbols using our codebook:
            DataTable symbols = codebook.Apply(data);
            int[][] inputs = symbols.ToArray<int>("Outlook", "Temperature", "Humidity", "Wind");
            int[] outputs = symbols.ToIntArray("PlayTennis").GetColumn(0);

            // Learn the training instances!
            id3learning.Run(inputs, outputs);

            /*This is how we will query the memory*/
            int[] query = codebook.Translate("Sunny", "Hot", "High", "Strong");
            int output = tree.Compute(query);


            /*Respond to user*/
            string answer = codebook.Translate("PlayTennis", output); // answer will be "No".
            Console.WriteLine(answer);
        }

        // this is the sequence y follows
        int[] ysequence = new int[] { 1, 2, 3, 2 };
        // this generates the correct Y for a given X
        int CalcY(int x) => ysequence[(x - 1) % 4];
        // this generates some inputs - just a few differnt mod of x
        int[] CalcInputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 };
        // for http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example
        [TestMethod]
        public void AccordID3TestStackOverFlowQuestion2()
        {
            // build the training data set
            int numtrainingcases = 12;
            int[][] inputs = new int[numtrainingcases][];
            int[] outputs = new int[numtrainingcases];
            Console.WriteLine("\t\t\t\t x \t y");
            for (int x = 1; x <= numtrainingcases; x++)
            {
                int y = CalcY(x);
                inputs[x-1] = CalcInputs(x);
                outputs[x-1] = y;
                Console.WriteLine("TrainingData \t " +x+"\t "+y);
            }
            // define how many values each input can have
            DecisionVariable[] attributes =
            {
                new DecisionVariable("Mod2",2),
                new DecisionVariable("Mod3",3),
                new DecisionVariable("Mod4",4),
                new DecisionVariable("Mod5",5),
                new DecisionVariable("Mod6",6)
            };
            // define how many outputs (+1 only because y doesn't use zero)
            int classCount = outputs.Max()+1;
            // create the tree
            DecisionTree tree = new DecisionTree(attributes, classCount);
            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(tree);
            // Learn the training instances! Populates the tree
            id3learning.Learn(inputs, outputs);
            Console.WriteLine();
            // now try to predict some cases that werent in the training data
            for (int x = numtrainingcases+1; x <= 2* numtrainingcases; x++)
            {
                int[] query = CalcInputs(x);
                int answer = tree.Decide(query); // makes the prediction
                Assert.AreEqual(CalcY(x), answer); // check the answer is what we expected - ie the tree got it right
                Console.WriteLine("Prediction \t\t " + x+"\t "+answer);
            }
        }

        // this is the sequence y follows
        int[] ysequence = new int[] { 1, 2, 3, 2 };
        // this generates the correct Y for a given X
        int CalcY(int x) => ysequence[(x - 1) % 4];
        // this generates some inputs - just a few differnt mod of x
        int[] CalcInputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 };
        // for http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example
        [TestMethod]
        public void AccordID3TestStackOverFlowQuestion2()
        {
            // build the training data set
            int numtrainingcases = 12;
            int[][] inputs = new int[numtrainingcases][];
            int[] outputs = new int[numtrainingcases];
            Console.WriteLine("\t\t\t\t x \t y");
            for (int x = 1; x <= numtrainingcases; x++)
            {
                int y = CalcY(x);
                inputs[x-1] = CalcInputs(x);
                outputs[x-1] = y;
                Console.WriteLine("TrainingData \t " +x+"\t "+y);
            }
            // define how many values each input can have
            DecisionVariable[] attributes =
            {
                new DecisionVariable("Mod2",2),
                new DecisionVariable("Mod3",3),
                new DecisionVariable("Mod4",4),
                new DecisionVariable("Mod5",5),
                new DecisionVariable("Mod6",6)
            };
            // define how many outputs (+1 only because y doesn't use zero)
            int classCount = outputs.Max()+1;
            // create the tree
            DecisionTree tree = new DecisionTree(attributes, classCount);
            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(tree);
            // Learn the training instances! Populates the tree
            id3learning.Learn(inputs, outputs);
            Console.WriteLine();
            // now try to predict some cases that werent in the training data
            for (int x = numtrainingcases+1; x <= 2* numtrainingcases; x++)
            {
                int[] query = CalcInputs(x);
                int answer = tree.Decide(query); // makes the prediction
                Assert.AreEqual(CalcY(x), answer); // check the answer is what we expected - ie the tree got it right
                Console.WriteLine("Prediction \t\t " + x+"\t "+answer);
            }
        }


        #endregion

        #region methods

        public override void LoadDetails()
        {
            AllTasks.Clear();
            var client = ClientSession.Instance.RelaxoClient;

            // Load all tasks
            var tasks = client.GetTasks(ClientSession.Instance.CurrentWorkflow);
            tasks.ForEach(t => AllTasks.Add(t));

            // Load decision
            var decision = client.GetDecision(Id);
            NextItemTrue = decision.NextItemTrue;
            Condition = decision.Condition;
            
            // get decision variables
            DecisionVariable.Clear();
            decision.Variables.ForEach(v=>DecisionVariable.Add(new PythonVariableViewModel(v)));

        }

        /// <summary>
        /// Methode die aufgerufen wird, wenn sich die aktuell ausgewählte Variable geändert hat,
        /// damit die entsprechenden Werte der Variable (Task, TaskComponent) in den DropDowns gesetzt werden können.
        /// </summary>
        private void VariableChanged()
        {
            if(selectedVariable == null) return;

            SelectedTask = AllTasks.FirstOrDefault(task => task.Id == selectedVariable.TaskId);
        }

        /// <summary>
        /// Methode die aufgerufen wird, wenn der aktuell ausgewählter Task geändert wird.
        /// Wird gebraucht um die entsprechenden TaskComponents des Tasks in die Dropdown zu laden.
        /// </summary>
        private void SelectedTaskChanged()
        {
            if (SelectedTask == null)
            {
                SelectedComponent = null;
                return;
            }

            if (SelectedVariable == null)
            {
                SelectedTask = null;
                SelectedComponent = null;
                return;
            }

            // merke die ausgewählte TaksComponent, damit die nach dem laden wieder gesetzt werden kann.
            var tmpComponentId = selectedVariable.TaskComponentId;

            var task = ClientSession.Instance.RelaxoClient.GetTask(SelectedTask.Id);
            var components = new List<TaskComponent>();

            ComponentsFromTask.Clear();
            task.FormGroups.ForEach(group => components.AddRange(group.Components));
            components.ForEach(comp => ComponentsFromTask.Add(comp));

            SelectedComponent = ComponentsFromTask.FirstOrDefault(component => component.Id == tmpComponentId);
        }

        public override void SaveDetails()
        {
            var client = ClientSession.Instance.RelaxoClient;
            var decision = new Decision();
            decision.Id = Id;
            decision.Condition = Condition;
            decision.NextItemTrue = NextItemTrue;            
            decision.Variables.AddRange(from variable
                                        in DecisionVariable
                                        select new PythonVariable { Name = variable.Name,                                                                      
                                                                      TaskComponentId = variable.TaskComponentId,
                                                                      TaskId = variable.TaskId});

            client.UpdateDecision(decision);
        }


        public bool classifierDT(data d)
        {
            int D = d.d;
            double[] input = new double[D];
            int output;
            for (int i = 0; i < D; ++i)
                input[i] = d.msg[i];

            output = tree.Decide(input);

            return output == 1;
        }

        public void DecisionTreeBuild(List<train> datalist)
        {
            int length = datalist.Count;
            int d = datalist[0].d;
            double[][] inputs;
            int[] outputs;
            GetData(out inputs, out outputs, datalist);

            DecisionVariable[] variables = new DecisionVariable[d];
            for (int i = 0; i < d; ++i)
                variables[i] = new DecisionVariable("attribute" + (i + 1), DecisionVariableKind.Continuous);

            // Create the C4.5 learning algorithm
            var c45 = new C45Learning(variables);
         
            // Learn the decision tree using C4.5
            tree = c45.Learn(inputs, outputs);
            

            log("The decision tree model has been trained");
        }

      
        public void DecisionTreeBuild(string Filename)
        {
            StreamReader sr = new StreamReader(Filename);
            string str;

            irislist = new List<Iris>();
            
            do
            {
                str = sr.ReadLine();
                if (str == null || str == "")
                    break;
                string[] ss = str.Split(',');
                int ss_length = ss.Length;
                for (int i = 0; i < ss_length; ++i)
                    ss[i] = ss[i].Trim();
                Iris iris = new Iris(ss,ss_length);
                irislist.Add(iris);
              
            }
            while (str != null && str != "");

            sr.Close();

            double[][] inputs;int[] outputs;
            GetData(out inputs, out outputs);
            
            // Specify the input variables
            DecisionVariable[] variables =
            {
                new DecisionVariable("SepalLength", DecisionVariableKind.Continuous),
                new DecisionVariable("SepalWidth", DecisionVariableKind.Continuous),
                new DecisionVariable("PetalLength", DecisionVariableKind.Continuous),
                new DecisionVariable("PetalWidth", DecisionVariableKind.Continuous),
            };

            // Create the C4.5 learning algorithm
            var c45 = new C45Learning(variables);

            // Learn the decision tree using C4.5
            tree = c45.Learn(inputs, outputs);

            
        }

        public void TrainAndTestByIrisData()
        {
            DecisionTreeBuild("iris.data");
            int length = irislist.Count;
            int attribute_number = irislist[0].attribute_count;
            int right = 0;
            for(int i= 135; i<length;++i)
            {
                double[] input = new double[attribute_number];
                for (int j = 0; j < attribute_number; ++j)
                    input[j] = irislist[i].attributes[j];
                int output = tree.Decide(input);
                Console.WriteLine(output);
                if (output == irislist[i].type)
                    right++;
            }
            MessageBox.Show("正确率 : " + right / ((double)length-135) * 100 + " %");
        }

        static void Main(string[] args)
        {

            // Exercise program to demonstrate usage of do while loop.

            // If this user enters "Prateek" or either "Shriram," it will return Mr. firstName Bansal, whereas if the first name is not either "Prateek" or "Shriram," it will say Hello, firstName. Also, after the first entry, the program will ask you to make another entry. This program has the usage of an if-else loop and a do-while loop. 

            string decisionVariable = "";

            do
            {
                Console.Write("What is your name: ");
                string firstName = Console.ReadLine();

                if (firstName.ToLower() == "prateek" || firstName.ToLower() == "shriram")
                {
                    Console.WriteLine($"Hello Mr. {firstName} Bansal");
                }
                else
                {
                    Console.WriteLine($"Hello, {firstName}");
                }

                Console.Write("Do you want to make another entry press enter, if not then type 'exit': ");
                decisionVariable = Console.ReadLine();

            } while (decisionVariable.ToLower() != "exit");


            Console.ReadLine();
        }

            // this is the sequence y follows
        int[] ysequence = new int[] { 1, 2, 3, 2 };
        // this generates the correct Y for a given X
        int CalcY(int x) => ysequence[(x - 1) % 4];
        // this generates some inputs - just a few differnt mod of x
        int[] CalcInputs(int x) => new int[] { CalcY(x-1), CalcY(x-2), CalcY(x-3), CalcY(x-4), CalcY(x - 5) };
        //int[] CalcInputs(int x) => new int[] { x % 2, x % 3, x % 4, x % 5, x % 6 };
        // for http://stackoverflow.com/questions/40573388/simple-accord-net-machine-learning-example
        [TestMethod]
        public void AccordID3TestTestStackOverFlowQuestion2()
        {
            // build the training data set
            int numtrainingcases = 12;
            int starttrainingat = 9;
            int[][] inputs = new int[numtrainingcases][];
            int[] outputs = new int[numtrainingcases];
            Console.WriteLine("\t\t\t\t x \t y");
            for (int x = starttrainingat; x < numtrainingcases + starttrainingat; x++)
            {
                int y = CalcY(x);
                inputs[x- starttrainingat] = CalcInputs(x);
                outputs[x- starttrainingat] = y;
                Console.WriteLine("TrainingData \t " +x+"\t "+y);
            }
            // define how many values each input can have
            DecisionVariable[] attributes =
            {
                new DecisionVariable("y-1",4),
                new DecisionVariable("y-2",4),
                new DecisionVariable("y-3",4),
                new DecisionVariable("y-4",4),
                new DecisionVariable("y-5",4)
            };
            // define how many outputs (+1 only because y doesn't use zero)
            int classCount = outputs.Max()+1;
            // create the tree
            DecisionTree tree = new DecisionTree(attributes, classCount);
            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(tree);
            // Learn the training instances! Populates the tree
            id3learning.Learn(inputs, outputs);
            Console.WriteLine();
            // now try to predict some cases that werent in the training data
            for (int x = starttrainingat+numtrainingcases; x <= starttrainingat + 2 * numtrainingcases; x++)
            {
                int[] query = CalcInputs(x);
                int answer = tree.Decide(query); // makes the prediction
                Assert.AreEqual(CalcY(x), answer); // check the answer is what we expected - ie the tree got it right
                Console.WriteLine("Prediction \t\t " + x+"\t "+answer);
            }
        }

Accord.MachineLearning.DecisionTrees.DecisionVariable : Object

Constructors :

public DecisionVariable(String name = , DoubleRange range = )
public DecisionVariable(String name = , DecisionVariableKind nature = )
public DecisionVariable(String name = , IntRange range = )
public DecisionVariable(String name = , Int32 symbols = )

Methods :

public String get_Name()
public Void set_Name(String value = )
public DecisionVariableKind get_Nature()
public Void set_Nature(DecisionVariableKind value = )
public DoubleRange get_Range()
public Void set_Range(DoubleRange value = )
public static DecisionVariable Continuous(String name = )
public static DecisionVariable Continuous(String name = , DoubleRange range = )
public static DecisionVariable Discrete(String name = , IntRange range = )
public static DecisionVariable Discrete(String name = , Int32 symbols = )
public String ToString()
public static DecisionVariable[] FromDictionary(OrderedDictionary<String, String[]> columns = )
public static DecisionVariable[] FromCodebook(Codification<String> codebook = , String[] columns = )
public static DecisionVariable[] FromData(Double[][] inputs = )
public static DecisionVariable[] FromData(Int32[][] inputs = )
public static DecisionVariable[] FromData(Nullable inputs = )
public Type GetType()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()