Example #1
0
        public void Execute()
        {
            //    - Prerequisites:
            //	  - Graphstructure
            //    - Training Data
            //    - 2 Node Classifications
            TrainingData   = new List <GWGraph <CRFNodeData, CRFEdgeData, CRFGraphData> >();
            EvaluationData = new List <GWGraph <CRFNodeData, CRFEdgeData, CRFGraphData> >();

            // decision wether user wants to train or load pre-trained data
            var requestTraining = new UserDecision("Use Training.", "Load Training Result.");

            requestTraining.Request();

            if (requestTraining.Decision == 0) // use training
            {
                //    -n characteristics for each node
                {
                    var request = new UserInput(UserInputLookFor.Folder);
                    request.DefaultPath = "..\\..\\CRFToolApp\\bin\\Graphs";
                    request.TextForUser = "******";
                    request.Request();
                    GraphDataFolder = request.UserText;

                    foreach (var file in Directory.EnumerateFiles(GraphDataFolder))
                    {
                        var graph = JSONX.LoadFromJSON <GWGraph <CRFNodeData, CRFEdgeData, CRFGraphData> >(file);
                        TrainingData.Add(graph);
                    }
                }

                //   - Step 1:

                //   - discretize characteristics
                #region Use Training Pede
                {
                    // create features
                    CreateFeatures(Dataset, TrainingData);
                    InitCRFScores(TrainingData);

                    var request = new OLMRequest(OLMVariant.Ising, TrainingData);
                    request.BasisMerkmale.AddRange(Dataset.NodeFeatures);
                    request.BasisMerkmale.AddRange(Dataset.EdgeFeatures);

                    request.LossFunctionValidation = OLM.LossRatio;

                    request.Request();

                    // zugehörige Scores erzeugen für jeden Graphen (auch Evaluation)
                    CreateCRFScores(TrainingData, Dataset.NodeFeatures, request.Result.ResultingWeights);

                    // store trained Weights
                    Dataset.NumberIntervals    = NumberIntervals;
                    Dataset.Characteristics    = TrainingData.First().Data.Characteristics.ToArray();
                    Dataset.EdgeCharacteristic = "IsingEdgeCharacteristic";
                    Dataset.Weights            = request.Result.ResultingWeights;
                    Dataset.SaveAsJSON("results.json");
                }

                #endregion
            }
            else
            { // load pre-trained data
                var request = new UserInput();
                request.TextForUser = "******";
                request.Request();
                var file           = request.UserText;
                var trainingResult = JSONX.LoadFromJSON <WorkflowOneDataset>(file);
                Dataset = trainingResult;
            }
            //- Step2:

            // User Choice here
            {
                var request = new UserInput(UserInputLookFor.Folder);
                request.TextForUser = "******";
                request.Request();
                GraphDataFolder = request.UserText;

                foreach (var file in Directory.EnumerateFiles(GraphDataFolder))
                {
                    var graph = JSONX.LoadFromJSON <GWGraph <CRFNodeData, CRFEdgeData, CRFGraphData> >(file);
                    EvaluationData.Add(graph);
                }
            }

            // remove double edges
            {
                var edgesToRemove = new LinkedList <GWEdge <CRFNodeData, CRFEdgeData, CRFGraphData> >();
                foreach (var graph in EvaluationData)
                {
                    foreach (var edge in graph.Edges.ToList())
                    {
                        if (graph.Edges.Any(e => (e != edge) && ((e.Foot == edge.Head && e.Head == edge.Foot && e.GWId.CompareTo(edge.GWId) < 0) || (e.Foot == edge.Foot && e.Head == edge.Head && e.GWId.CompareTo(edge.GWId) < 0))))
                        {
                            edgesToRemove.Add(edge);
                            graph.Edges.Remove(edge);
                        }
                    }
                }
                foreach (var edge in edgesToRemove)
                {
                    edge.Foot.Edges.Remove(edge);
                    edge.Head.Edges.Remove(edge);
                }
            }


            //scores erzeugen
            CreateCRFScores(EvaluationData, Dataset.NodeFeatures, Dataset.Weights);

            //   - Create ROC Curve
            {
            }
            //   - Give Maximum with Viterbi
            {
                foreach (var graph in EvaluationData)
                {
                    var request = new SolveInference(graph, null, 2);
                    request.Request();
                    graph.Data.AssginedLabeling = request.Solution.Labeling;
                }

                //show results in 3D Viewer
                {
                    //var request = new ShowGraphs();
                    //request.Graphs = EvaluationData;
                    //request.Request();
                }
            }
            //   - Give Sample with MCMC
            {
                foreach (var graph in EvaluationData)
                {
                    SoftwareGraphLearningParameters parameters = new SoftwareGraphLearningParameters();
                    parameters.NumberOfGraphs          = 60;
                    parameters.NumberNodes             = 50;
                    parameters.NumberLabels            = 2;
                    parameters.NumberCategories        = 4;
                    parameters.IntraConnectivityDegree = 0.15;
                    parameters.InterConnectivityDegree = 0.01;


                    //sample parameters
                    var samplerParameters = new MHSamplerParameters();
                    var sglGraph          = graph.Convert((nodeData) => new SGLNodeData()
                    {
                    }, (edgeData) => new SGLEdgeData(), (graphData) => new SGLGraphData());

                    samplerParameters.Graph        = sglGraph;
                    samplerParameters.NumberChains = 1;

                    //sampler starten
                    var gibbsSampler = new MHSampler();
                    gibbsSampler.Do(samplerParameters);
                }
            }
        }
Example #2
0
        public static void Run()
        {
            new FileLogger("Log/");
            new ConsoleLoggerSafe();

            SoftwareGraphLearningParameters parameters = new SoftwareGraphLearningParameters();

            parameters.NumberOfGraphs          = 60;
            parameters.NumberNodes             = 50;
            parameters.NumberLabels            = 2;
            parameters.NumberCategories        = 4;
            parameters.IntraConnectivityDegree = 0.15;
            parameters.InterConnectivityDegree = 0.01;



            //new WorkCycle().Do(parameters);



            var graph = new ErdösGraphCreator().CreateGraph(new ErdösGraphCreationParameter(parameters));

            new CategoryGraphCreator().CreateCategoryGraph(graph);

            // Beobachtung für graph erzeugen
            foreach (var node in graph.Nodes)
            {
                var categoryGraph = graph.Data.CategoryGraph;
                var category      = categoryGraph.Nodes.ToList().Find(catNode => catNode.Data.Category == node.Data.Category);

                double probability = CodeBase.BaseProgram.Random.NextDouble();
                if (probability <= category.Data.ObservationProbabilty)
                {
                    node.Data.Observation = 0;
                }
                else
                {
                    node.Data.Observation = 1;
                }
            }

            //scores erzeugen
            var isingModel = new IsingModel(1.0, 0.7);

            isingModel.CreateCRFScore(graph);



            //sample parameters
            var samplerParameters = new MHSamplerParameters();

            samplerParameters.Graph        = graph;
            samplerParameters.NumberChains = 3;



            //sampler starten
            var gibbsSampler = new MHSampler();

            gibbsSampler.Do(samplerParameters);



            Console.Read();


            BaseProgram.Exit.Enter();
        }