Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12781


Ignore:
Timestamp:
07/20/15 16:45:08 (9 years ago)
Author:
aballeit
Message:

#2283 stable GUI; ThreadPool for runs; improved TreeAnalysis

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization
Files:
58 added
1 deleted
25 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization

    • Property svn:ignore
      •  

        old new  
        33TestResults
        44_ReSharper.GrammaticalOptimization
         5EvaluationResults
  • branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay

    • Property svn:ignore set to
      bin
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation

    • Property svn:ignore set to
      bin
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/Evaluation.csproj

    r12762 r12781  
    5454      <SubType>Designer</SubType>
    5555    </ApplicationDefinition>
    56     <Compile Include="EvaluationStat.cs" />
     56    <Compile Include="FoundSolution.cs" />
    5757    <Compile Include="Run.cs" />
    5858    <Compile Include="ViewModel\EvaluationViewModel.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/MainWindow.xaml

    r12762 r12781  
    3939                          DisplayMemberPath="Name"
    4040                          SelectedValue="{Binding SelectedAlgorithm}"
    41                           Width="200"
     41                          Width="280"
    4242                          SelectionChanged="ComboBoxAlgorithms_OnSelectionChanged"></ComboBox>
    4343                <ComboBox Name="ComboBoxPolicies"
     
    7575                <TextBlock Margin="2" Grid.Column="2" Grid.Row="0" VerticalAlignment="Center">MaxLen:</TextBlock>
    7676                <TextBox Name="TextBoxMaxLen" Margin="2" Grid.Column="3" Grid.Row="0" Width="100" VerticalAlignment="Center" TextAlignment="Right" Text="{Binding MaxLen}"></TextBox>
    77                 <TextBlock Margin="2" Grid.Column="2" Grid.Row="1" VerticalAlignment="Center">Threads:</TextBlock>
    78                 <TextBox Name="TextBoxThreads" Margin="2" Grid.Column="3" Grid.Row="1" Width="100" VerticalAlignment="Center" TextAlignment="Right" Text="{Binding Threads}"></TextBox>
    7977            </Grid>
    8078        </GroupBox>
     
    125123                                        <ColumnDefinition Width="*"></ColumnDefinition>
    126124                                    </Grid.ColumnDefinitions>
    127                                     <Grid Name="MyGrid" Grid.Column="0" DataContext="{Binding CurrentRun.TreeInfos}">
     125                                    <Grid Name="MyGrid" Grid.Column="0" DataContext="{Binding SelectedRun.TreeInfos}">
    128126                                        <Grid.RowDefinitions>
    129                                             <RowDefinition Height="Auto"></RowDefinition>
    130                                             <RowDefinition Height="Auto"></RowDefinition>
    131127                                            <RowDefinition Height="Auto"></RowDefinition>
    132128                                            <RowDefinition Height="Auto"></RowDefinition>
     
    148144                                        <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding LeaveNodes}" TextAlignment="Right"></TextBlock>
    149145                                        <TextBlock Grid.Row="4" Grid.Column="0" Text="DeepestLevel:"></TextBlock>
    150                                         <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding DeepestLevel}" TextAlignment="Right"></TextBlock>
    151                                         <TextBlock Grid.Row="5" Grid.Column="0" Text="AverageLevel:"></TextBlock>
    152                                         <TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding AverageLevel}" TextAlignment="Right"></TextBlock>
    153                                         <TextBlock Grid.Row="6" Grid.Column="0" Text="AverageChildren:"></TextBlock>
    154                                         <TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding AverageChildren}" TextAlignment="Right"></TextBlock>
     146                                        <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding DeepestLevel}" TextAlignment="Right"></TextBlock>                                       
    155147                                    </Grid>
    156148                                    <Frame Grid.Column="1" Name="treeDrawing" Source="pack://application:,,,/WpfTestSvgSample;component/DrawingPage.xaml"/>
     
    162154
    163155                <TabItem Header="Run-Comparison">
    164                     <TabControl Name="TabControlRunComparison" SelectionChanged="TabControlRunComparison_OnSelectionChanged">
     156                    <TabControl Name="TabControlRunComparison">
    165157                        <TabItem Name="TabItemChartRunComparison" Header="Chart">
    166158                            <d3:ChartPlotter x:Name="ComparisonChartPlotter" LegendVisible="False" EnableSmoothPanningForNumericAxes="True">
     
    182174                                                            Header="BestKnownQuality"/>
    183175                                            <GridViewColumn DisplayMemberBinding="{Binding Path=BestSolutionQuality}"
    184                                                             Header="BestFoundQuality"/>
     176                                                            Header="BestQuality"/>
    185177                                            <GridViewColumn DisplayMemberBinding="{Binding Path=EvaluationsPerSecond}"
    186178                                                            Header="EvaluationsPerSecond"/>
     
    211203                    <RowDefinition Height="Auto" />
    212204                    <RowDefinition Height="Auto" />
     205                    <RowDefinition Height="Auto" />
    213206                </Grid.RowDefinitions>
    214207                <Grid.ColumnDefinitions>
     
    216209                    <ColumnDefinition Width="*" />
    217210                </Grid.ColumnDefinitions>
    218                 <TextBlock Grid.Row="0" Grid.Column="0">Run:</TextBlock>
    219                 <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding CurrentRunString}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    220                 <TextBlock Grid.Row="1" Grid.Column="0">Evaluations:</TextBlock>
    221                 <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Evaluations}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    222                 <TextBlock Grid.Row="2" Grid.Column="0">MaxEvaluations:</TextBlock>
    223                 <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding MaxEvaluations}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    224                 <TextBlock Grid.Row="3" Grid.Column="0">CurrentBestQuality:</TextBlock>
    225                 <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding CurrentBestQuality}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    226                 <TextBlock Grid.Row="4" Grid.Column="0">BestKnownQuality:</TextBlock>
    227                 <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding BestKnownQuality}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    228                 <TextBlock Grid.Row="5" Grid.Column="0">MaxLen:</TextBlock>
    229                 <TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding MaxLen}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    230                 <TextBlock Grid.Row="6" Grid.Column="0">Evaluations/sec:</TextBlock>
    231                 <TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding EvaluationsPerSec}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    232                 <TextBlock Grid.Row="7" Grid.Column="0">BestSolutionFoundAt:</TextBlock>
    233                 <TextBlock Grid.Row="7" Grid.Column="1" Text="{Binding BestSolutionFoundAt}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
    234                 <TextBlock Grid.Row="8" Grid.Column="0">BestSolution:</TextBlock>
    235                 <TextBlock Grid.Row="8" Grid.Column="1" Text="{Binding BestSolution}" Margin="5,0,0,0" HorizontalAlignment="Right" MaxWidth="100"/>
     211                <TextBlock Grid.Row="0" Grid.Column="0">CompletedRuns:</TextBlock>
     212                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding CompletedRuns}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     213                <TextBlock Grid.Row="1" Grid.Column="0">RunState:</TextBlock>
     214                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding SelectedRun.RunState}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     215                <TextBlock Grid.Row="2" Grid.Column="0">Evaluations:</TextBlock>
     216                <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding SelectedRun.Evaluations}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     217                <TextBlock Grid.Row="3" Grid.Column="0">MaxEvaluations:</TextBlock>
     218                <TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding SelectedRun.MaxEvaluations}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     219                <TextBlock Grid.Row="4" Grid.Column="0">BestQuality:</TextBlock>
     220                <TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding SelectedRun.BestQuality}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     221                <TextBlock Grid.Row="5" Grid.Column="0">BestKnownQuality:</TextBlock>
     222                <TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding SelectedRun.BestKnownQuality}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     223                <TextBlock Grid.Row="6" Grid.Column="0">MaxLen:</TextBlock>
     224                <TextBlock Grid.Row="6" Grid.Column="1" Text="{Binding SelectedRun.MaxLen}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     225                <TextBlock Grid.Row="7" Grid.Column="0">Evaluations/sec:</TextBlock>
     226                <TextBlock Grid.Row="7" Grid.Column="1" Text="{Binding SelectedRun.EvaluationsPerSecond}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     227                <TextBlock Grid.Row="8" Grid.Column="0">BestSolutionFoundAt:</TextBlock>
     228                <TextBlock Grid.Row="8" Grid.Column="1" Text="{Binding SelectedRun.BestSolutionFoundAt}" Margin="5,0,0,0" HorizontalAlignment="Right"/>
     229                <TextBlock Grid.Row="9" Grid.Column="0">BestSolution:</TextBlock>
     230                <TextBlock Grid.Row="9" Grid.Column="1" Text="{Binding SelectedRun.BestSolution}" TextWrapping="Wrap" Margin="5,0,0,0" HorizontalAlignment="Right" MaxWidth="100"/>
    236231            </Grid>
    237232        </Grid>
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/MainWindow.xaml.cs

    r12762 r12781  
    3030    public partial class MainWindow : Window
    3131    {
    32         private BackgroundWorker worker = new BackgroundWorker();
    3332        private EvaluationViewModel vm;
    3433
     
    4241            CenterWindowOnScreen();
    4342            this.DataContext = vm = new EvaluationViewModel();
    44             vm.MaxLen = 50;
    45             vm.MaxEvaluations = 250000;
    46             vm.NrRuns = 1;
    47             vm.CurrentRunString = "0/1";
    48             vm.Threads = 6;
     43            vm.MaxLen = 100;
     44            vm.MaxEvaluations = 1000000;
     45            vm.NrRuns = 10;
    4946            vm.VerticalAxisString = "SolutionQuality";
    5047            vm.HorizontalAxisString = "Iteration";
    51             this.worker.WorkerSupportsCancellation = true;
    52             this.worker.DoWork += worker_DoWork;
    53             this.worker.ProgressChanged += worker_ProgressChanged;
    54             this.worker.RunWorkerCompleted += worker_RunWorkerCompleted;
    55 
    56             ////updateCollectionTimer = new DispatcherTimer();
    57             ////updateCollectionTimer.Interval = TimeSpan.FromMilliseconds(100);
    58             ////updateCollectionTimer.Tick += updateCollectionTimer_Tick;
    59             ////updateCollectionTimer.Start();
    6048        }
    6149
     
    7058        }
    7159
    72         ////void updateCollectionTimer_Tick(object sender, EventArgs e)
    73         ////{
    74         ////    while (newStats.Count > 0)
    75         ////    {
    76         ////        EvaluationStat stat = newStats.Pop();
    77         ////        stats.Add(stat);
    78         ////    }
    79         ////}
    80 
    81         void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
     60        private void DrawLineChart(Run run)
     61        {
     62            List<FoundSolution> solutions = new List<FoundSolution>(run.FoundSolutions);
     63
     64            if (run.BestSolutionFoundAt < run.Evaluations)
     65            {
     66                solutions.Add(new FoundSolution(run.EndTime, run.Evaluations, run.BestQuality, run.BestSolution));
     67            }
     68
     69            var ds = new EnumerableDataSource<FoundSolution>(solutions);
     70
     71
     72            ds.SetXMapping(x => x.Iteration);
     73            ds.SetYMapping(y => y.Quality);
     74
     75            LineGraph graph = new LineGraph(ds);
     76
     77            graph.StrokeThickness = 2;
     78            graph.AddToPlotter(ChartPlotter);
     79        }
     80
     81        private void DrawTreeChart(Run run)
     82        {
     83            if (!string.IsNullOrEmpty(run.SvgFile))
     84            {
     85                treeDrawingPage.LoadDocument(run.SvgFile);
     86            }
     87        }
     88
     89        private void DoRun(Object threadContext)
     90        {
     91            Run run = (Run)threadContext;
     92            run.RunState = RunState.Running;
     93            run.StartTime = DateTime.Now;
     94
     95            run.Solver.Run(run.MaxEvaluations);
     96
     97            run.EndTime = DateTime.Now;
     98            run.RunState = RunState.Analyzing;
     99
     100            if (run.FoundSolutions.Count > 0)
     101            {
     102                if (run.Solver is MonteCarloTreeSearch)
     103                {
     104                    MonteCarloTreeSearch mctsSolver = (MonteCarloTreeSearch)run.Solver;
     105
     106                    run.TreeInfos = mctsSolver.GetTreeInfos();
     107
     108                    //byte[] output = mctsSolver.GenerateSvg();
     109                    //if (output != null && output.Length > 0)
     110                    //{
     111                    //    run.SvgFile = string.Format("MCTS_SVG_#{0}_{1}.svg", run.RunNumber, DateTime.Now.Ticks);
     112                    //    File.WriteAllBytes(run.SvgFile, mctsSolver.GenerateSvg());
     113                    //}
     114                }
     115            }
     116            run.RunState = RunState.Finished;
     117            lock (vm.CompletedRuns)
     118            {
     119                int completed = 0;
     120                foreach (Run r in vm.Runs)
     121                {
     122                    if (r.RunState == RunState.Finished)
     123                    {
     124                        completed++;
     125                    }
     126                }
     127                vm.CompletedRuns = string.Format("{0}/{1}", completed, vm.Runs.Count);
     128                if (completed == vm.NrRuns)
     129                {
     130                    Dispatcher.Invoke(AllRunsFinished);
     131                }
     132            }
     133        }
     134
     135        private void AllRunsFinished()
    82136        {
    83137            ButtonRun.IsEnabled = true;
     
    85139            TextBoxMaxLen.IsEnabled = true;
    86140            TextBoxRuns.IsEnabled = true;
    87             TextBoxThreads.IsEnabled = true;
    88         }
    89 
    90         private void DrawCharts(Run run)
    91         {
    92             ClearChart();
    93 
    94             var ds = new EnumerableDataSource<EvaluationStat>(run.EvaluationStats);
    95             ds.SetXMapping(x => x.Iteration);
    96             ds.SetYMapping(y => y.CurrentBestQuality);
    97 
    98             LineGraph graph = new LineGraph(ds);
    99 
    100             graph.StrokeThickness = 2;
    101             graph.AddToPlotter(ChartPlotter);
    102 
    103             DrawTreeChart(run);
    104         }
    105 
    106         private void DrawTreeChart(Run run)
    107         {
    108             if (!string.IsNullOrEmpty(run.SvgFile))
    109             {
    110                 treeDrawingPage.LoadDocument(run.SvgFile);
    111             }
    112         }
    113 
    114 
    115         void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    116         {
    117             Debug.WriteLine(e.UserState);
    118         }
    119 
    120         void worker_DoWork(object sender, DoWorkEventArgs e)
    121         {
     141        }
     142
     143        private void StartRuns()
     144        {
     145            vm.CompletedRuns = string.Format("0/{0}", vm.NrRuns);
    122146
    123147            Type algorithmType = vm.SelectedAlgorithm;
     
    143167            }
    144168
    145             vm.BestKnownQuality = problem.BestKnownQuality(vm.MaxLen);
    146 
    147169            for (int i = 0; i < vm.NrRuns; i++)
    148170            {
    149                 vm.CurrentRunString = string.Format("{0}/{1}", i + 1, vm.NrRuns);
    150                 vm.Evaluations = 0;
    151                 vm.CurrentBestQuality = 0;
    152 
    153                 List<EvaluationStat> stats = new List<EvaluationStat>();
    154 
    155171                ISolver solver = null;
    156172
    157                 if (algorithmType == typeof(MonteCarloTreeSearch))
     173                if (algorithmType == typeof(MonteCarloTreeSearch_PruneLeaves))
     174                {
     175                    solver = new MonteCarloTreeSearch_PruneLeaves(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
     176                }
     177                else if (algorithmType == typeof(MonteCarloTreeSearch))
    158178                {
    159179                    solver = new MonteCarloTreeSearch(problem, vm.MaxLen, random, policyInstance, new RandomSimulation(problem, random, vm.MaxLen));
     
    177197                }
    178198
    179                 solver.FoundNewBestSolution += (sentence, quality) =>
    180                 {
    181                     vm.BestSolutionFoundAt = vm.Evaluations;
    182                     vm.BestSolution = sentence;
    183                 };
    184                 solver.SolutionEvaluated += (sentence, quality) =>
    185                 {
    186                     vm.Evaluations++;
    187                     if (vm.CurrentBestQuality < quality)
    188                     {
    189                         vm.CurrentBestQuality = quality;
    190                     }
    191                     stats.Add(new EvaluationStat(DateTime.Now, vm.Evaluations, quality, vm.CurrentBestQuality));
    192                 };
    193 
    194                 solver.Run(vm.MaxEvaluations);
    195 
    196 
    197                 if (stats.Count > 0)
    198                 {
    199                     TimeSpan totalTimeNeeded = stats[stats.Count - 1].Time - stats[0].Time;
    200                     vm.EvaluationsPerSec = Math.Round(vm.Evaluations / totalTimeNeeded.TotalSeconds, 2);
    201 
    202                     TimeSpan timeForBestSolution = stats[vm.BestSolutionFoundAt - 1].Time - stats[0].Time;
    203 
    204                     Run run = new Run(i, vm.Evaluations, vm.CurrentBestQuality, vm.BestKnownQuality,
    205                         vm.BestSolutionFoundAt, vm.BestSolution, vm.EvaluationsPerSec,
    206                         totalTimeNeeded, timeForBestSolution, stats);
    207 
    208 
    209                     MonteCarloTreeSearch mctsSolver = null;
    210                     if (algorithmType == typeof(MonteCarloTreeSearch))
    211                     {
    212                         mctsSolver = (MonteCarloTreeSearch)solver;
    213 
    214                         run.TreeInfos = mctsSolver.GetTreeInfos();
    215 
    216                         byte[] output = mctsSolver.GenerateSvg();
    217                         if (output.Length > 0)
    218                         {
    219                             run.SvgFile = string.Format("MCTS_SVG_#{0}_{1}.svg", i, DateTime.Now.Ticks);
    220                             File.WriteAllBytes(run.SvgFile, mctsSolver.GenerateSvg());
    221                         }
    222                     }
    223 
    224                     Dispatcher.BeginInvoke(new Action(() => vm.Runs.Add(run)));
    225                 }
    226             }
    227         }
    228 
    229         private void ClearChart()
     199                Run run = new Run(problem, policyInstance, solver, i + 1, vm.MaxEvaluations, vm.MaxLen);
     200
     201                vm.Runs.Add(run);
     202
     203                ThreadPool.QueueUserWorkItem(DoRun, run);
     204            }
     205        }
     206
     207        private void ClearLineChart()
    230208        {
    231209            ChartPlotter.Children.RemoveAll<LineGraph>();
    232210        }
    233211
     212        private void ClearComparisonChart()
     213        {
     214            ComparisonChartPlotter.Children.RemoveAll<LineGraph>();
     215        }
     216
    234217        private void ButtonRun_OnClick(object sender, RoutedEventArgs e)
    235218        {
    236             ClearChart();
     219            ClearLineChart();
     220            ClearComparisonChart();
     221
    237222            vm.Runs.Clear();
    238             vm.CurrentRun = null;
     223            vm.SelectedRun = null;
    239224            ButtonRun.IsEnabled = false;
    240225            TextBoxMaxEvaluations.IsEnabled = false;
    241226            TextBoxMaxLen.IsEnabled = false;
    242227            TextBoxRuns.IsEnabled = false;
    243             TextBoxThreads.IsEnabled = false;
    244             worker.RunWorkerAsync();
     228            StartRuns();
    245229        }
    246230
     
    264248        private void ButtonStop_OnClick(object sender, RoutedEventArgs e)
    265249        {
    266             worker.CancelAsync();
     250            //worker.CancelAsync();
    267251        }
    268252
    269253        private void ComboBoxAlgorithms_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    270254        {
    271             if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch))
     255            if (vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch) || vm.SelectedAlgorithm == typeof(MonteCarloTreeSearch_PruneLeaves))
    272256            {
    273257                ComboBoxPolicies.IsEnabled = true;
     
    293277        private void ListBoxRuns_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    294278        {
     279            if (vm.SelectedRun != null)
     280            {
     281                vm.SelectedRun.Solver.FoundNewBestSolution -= SelectedRun_FoundNewBestSolution;
     282            }
    295283            if (ListBoxRuns.SelectedItem != null)
    296284            {
    297                 vm.CurrentRun = (Run)ListBoxRuns.SelectedItem;
    298                 DrawCharts(vm.CurrentRun);
    299             }
     285                vm.SelectedRun = (Run)ListBoxRuns.SelectedItem;
     286
     287                vm.SelectedRun.Solver.FoundNewBestSolution += SelectedRun_FoundNewBestSolution;
     288
     289                ClearLineChart();
     290                DrawLineChart(vm.SelectedRun);
     291                //DrawTreeChart(vm.SelectedRun);
     292            }
     293            else
     294            {
     295                vm.SelectedRun = null;
     296            }
     297        }
     298
     299        void SelectedRun_FoundNewBestSolution(string arg1, double arg2)
     300        {
     301            Dispatcher.BeginInvoke(new Action(() =>
     302            {
     303                ClearLineChart();
     304                DrawLineChart(vm.SelectedRun);
     305            }));
    300306        }
    301307
     
    349355        private void LoadButton_OnClick(object sender, RoutedEventArgs e)
    350356        {
    351            LoadFromFile();
     357            LoadFromFile();
    352358        }
    353359
     
    355361        {
    356362            SaveToFile();
    357         }
    358 
    359         private void TabControlRunComparison_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    360         {
    361             ////if (TabControlRunComparison.SelectedItem == TabItemChartRunComparison)
    362             ////{
    363             ////    ComparisonChartPlotter.Children.RemoveAll<LineGraph>();
    364 
    365             ////    var ds = new EnumerableDataSource<EvaluationStat>(run.EvaluationStats);
    366             ////    ds.SetXMapping(x => x.Iteration);
    367             ////    ds.SetYMapping(y => y.CurrentBestQuality);
    368 
    369             ////    LineGraph graph = new LineGraph(ds);
    370 
    371             ////    graph.StrokeThickness = 2;
    372             ////    graph.AddToPlotter(ChartPlotter);
    373 
    374             ////}
    375363        }
    376364    }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/Run.cs

    r12762 r12781  
    1 using System;
     1using HeuristicLab.Algorithms.Bandits;
     2using HeuristicLab.Algorithms.GrammaticalOptimization;
     3using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
     4using HeuristicLab.Problems.GrammaticalOptimization;
     5using System;
    26using System.Collections.Generic;
    3 using HeuristicLab.Algorithms.MonteCarloTreeSearch.Base;
     7using System.ComponentModel;
    48
    59namespace Evaluation
    610{
    7     public class Run
     11
     12    public enum RunState
     13    {
     14        NotStarted,
     15        Running,
     16        Analyzing,
     17        Finished
     18    }
     19
     20    public class Run : INotifyPropertyChanged
    821    {
    922        public Run()
     
    1225        }
    1326
    14         public Run(int runNumber, int evaluations, double bestSolutionQuality, double bestKnownQuality,
    15             int bestSolutionFoundAt, string bestSolution,
    16             double evaluationsPerSecond, TimeSpan totalTime, TimeSpan bestSolutionTime,
    17             List<EvaluationStat> evaluationStats)
    18         {
     27        public Run(ISymbolicExpressionTreeProblem problem, IBanditPolicy banditPolicy, ISolver solver, int runNumber,
     28            int maxEvaluations, int maxLen)
     29        {
     30            Problem = problem;
     31            BanditPolicy = banditPolicy;
     32            Solver = solver;
    1933            RunNumber = runNumber;
    20             Evaluations = evaluations;
    21             BestSolutionQuality = bestSolutionQuality;
    22             BestKnownQuality = bestKnownQuality;
    23             BestSolutionFoundAt = bestSolutionFoundAt;
    24             BestSolution = bestSolution;
    25             EvaluationsPerSecond = evaluationsPerSecond;
    26             TotalTime = totalTime;
    27             BestSolutionTime = bestSolutionTime;
    28             EvaluationStats = evaluationStats;
    29         }
    30 
    31         public int RunNumber { get; set; }
    32         public int Evaluations { get; set; }
    33         public double BestSolutionQuality { get; set; }
    34 
    35         public string BestSolution { get; set; }
    36 
    37         public double BestKnownQuality { get; set; }
    38         public int BestSolutionFoundAt { get; set; }
    39 
    40         public double EvaluationsPerSecond { get; set; }
    41         public TimeSpan TotalTime { get; set; }
    42         public TimeSpan BestSolutionTime { get; set; }
    43         public List<EvaluationStat> EvaluationStats { get; set; }
     34            MaxEvaluations = maxEvaluations;
     35            MaxLen = maxLen;
     36
     37            Evaluations = 0;
     38            BestQuality = double.MinValue;
     39            RunState = RunState.NotStarted;
     40            FoundSolutions = new List<FoundSolution>();
     41
     42            BestKnownQuality = problem.BestKnownQuality(maxLen);
     43            solver.SolutionEvaluated += (sentence, quality) =>
     44            {
     45                Evaluations++;
     46                if (quality > BestQuality)
     47                {
     48                    BestQuality = quality;
     49                    BestSolution = sentence;
     50                    BestSolutionFoundAt = evaluations;
     51                    FoundSolutions.Add(new FoundSolution(DateTime.Now, Evaluations, quality, sentence));
     52                }
     53            };
     54        }
     55
     56        private ISymbolicExpressionTreeProblem problem;
     57
     58        public ISymbolicExpressionTreeProblem Problem
     59        {
     60            get { return this.problem; }
     61            set
     62            {
     63                this.problem = value;
     64                this.OnPropertyChanged("Problem");
     65            }
     66        }
     67
     68        private IBanditPolicy banditPolicy;
     69
     70        public IBanditPolicy BanditPolicy
     71        {
     72            get { return this.banditPolicy; }
     73            set
     74            {
     75                this.banditPolicy = value;
     76                this.OnPropertyChanged("BanditPolicy");
     77            }
     78        }
     79
     80        private ISolver solver;
     81
     82        public ISolver Solver
     83        {
     84            get { return this.solver; }
     85            set
     86            {
     87                this.solver = value;
     88                this.OnPropertyChanged("Solver");
     89            }
     90        }
     91
     92        private int runNumber;
     93
     94        public int RunNumber
     95        {
     96            get { return this.runNumber; }
     97            set
     98            {
     99                this.runNumber = value;
     100                this.OnPropertyChanged("RunNumber");
     101            }
     102        }
     103
     104        private int maxEvaluations;
     105
     106        public int MaxEvaluations
     107        {
     108            get { return this.maxEvaluations; }
     109            set
     110            {
     111                this.maxEvaluations = value;
     112                this.OnPropertyChanged("MaxEvaluations");
     113            }
     114        }
     115
     116        private int maxLen;
     117
     118        public int MaxLen
     119        {
     120            get { return this.maxLen; }
     121            set
     122            {
     123                this.maxLen = value;
     124                this.OnPropertyChanged("MaxLen");
     125            }
     126        }
     127
     128        private int evaluations;
     129
     130        public int Evaluations
     131        {
     132            get { return this.evaluations; }
     133            set
     134            {
     135                this.evaluations = value;
     136                this.OnPropertyChanged("Evaluations");
     137                if (endTime > startTime)
     138                {
     139                    // endTime is set...
     140                    EvaluationsPerSecond = Math.Round(evaluations / TotalTime.TotalSeconds, 2);
     141                }
     142                else if (startTime > endTime)
     143                {
     144                    // only startTime is set...
     145                    TimeSpan currentTimeNeeded = DateTime.Now - startTime;
     146                    EvaluationsPerSecond = Math.Round(evaluations / currentTimeNeeded.TotalSeconds, 2);
     147                }
     148            }
     149        }
     150
     151        private double bestQuality;
     152
     153        public double BestQuality
     154        {
     155            get { return this.bestQuality; }
     156            set
     157            {
     158                this.bestQuality = value;
     159                this.OnPropertyChanged("BestQuality");
     160            }
     161        }
     162
     163        private string bestSolution;
     164
     165        public string BestSolution
     166        {
     167            get { return this.bestSolution; }
     168            set
     169            {
     170                this.bestSolution = value;
     171                this.OnPropertyChanged("BestSolution");
     172            }
     173        }
     174
     175        private double bestKnownQuality;
     176
     177        public double BestKnownQuality
     178        {
     179            get { return this.bestKnownQuality; }
     180            set
     181            {
     182                this.bestKnownQuality = value;
     183                this.OnPropertyChanged("BestKnownQuality");
     184            }
     185        }
     186
     187        private int bestSolutionFoundAt;
     188
     189        public int BestSolutionFoundAt
     190        {
     191            get { return this.bestSolutionFoundAt; }
     192            set
     193            {
     194                this.bestSolutionFoundAt = value;
     195                this.OnPropertyChanged("BestSolutionFoundAt");
     196            }
     197        }
     198
     199        private double evaluationsPerSecond;
     200
     201        public double EvaluationsPerSecond
     202        {
     203            get { return this.evaluationsPerSecond; }
     204            set
     205            {
     206                this.evaluationsPerSecond = value;
     207                this.OnPropertyChanged("EvaluationsPerSecond");
     208            }
     209        }
     210
     211        private DateTime startTime;
     212
     213        public DateTime StartTime
     214        {
     215            get { return this.startTime; }
     216            set
     217            {
     218                this.startTime = value;
     219                this.OnPropertyChanged("StartTime");
     220            }
     221        }
     222
     223        private DateTime endTime;
     224
     225        public DateTime EndTime
     226        {
     227            get { return this.endTime; }
     228            set
     229            {
     230                this.endTime = value;
     231                this.OnPropertyChanged("EndTime");
     232                TotalTime = EndTime - StartTime;
     233            }
     234        }
     235
     236        private TimeSpan totalTime;
     237
     238        public TimeSpan TotalTime
     239        {
     240            get { return this.totalTime; }
     241            set
     242            {
     243                this.totalTime = value;
     244                this.OnPropertyChanged("TotalTime");
     245            }
     246        }
     247
     248        private TimeSpan bestSolutionTime;
     249
     250        public TimeSpan BestSolutionTime
     251        {
     252            get { return this.bestSolutionTime; }
     253            set
     254            {
     255                this.bestSolutionTime = value;
     256                this.OnPropertyChanged("BestSolutionTime");
     257            }
     258        }
     259
     260        public List<FoundSolution> FoundSolutions { get; set; }
    44261
    45262        public string SvgFile { get; set; }
    46263
    47         public TreeInfos TreeInfos { get; set; }
     264        private TreeInfos treeInfos;
     265
     266        public TreeInfos TreeInfos
     267        {
     268            get { return this.treeInfos; }
     269            set
     270            {
     271                this.treeInfos = value;
     272                this.OnPropertyChanged("TreeInfos");
     273            }
     274        }
     275
     276        private RunState runState;
     277
     278        public RunState RunState
     279        {
     280            get { return this.runState; }
     281            set
     282            {
     283                this.runState = value;
     284                this.OnPropertyChanged("RunState");
     285            }
     286        }
    48287
    49288        public override string ToString()
     
    51290            return string.Format("Run #{0}", this.RunNumber);
    52291        }
     292
     293        #region INotifyPropertyChanged members
     294
     295        public event PropertyChangedEventHandler PropertyChanged;
     296
     297        protected void OnPropertyChanged(string propertyName)
     298        {
     299            if (PropertyChanged != null)
     300                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     301        }
     302
     303        #endregion INotifyPropertyChanged members
    53304    }
    54305}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Evaluation/ViewModel/EvaluationViewModel.cs

    r12762 r12781  
    1 using System.Collections.ObjectModel;
    2 using HeuristicLab.Algorithms.Bandits.BanditPolicies;
     1using HeuristicLab.Algorithms.Bandits.BanditPolicies;
    32using HeuristicLab.Algorithms.GeneticProgramming;
    43using HeuristicLab.Algorithms.GrammaticalOptimization;
     
    76using System;
    87using System.Collections.Generic;
     8using System.Collections.ObjectModel;
    99using System.ComponentModel;
    1010using System.Linq;
    11 using System.Text;
    12 using System.Threading.Tasks;
    13 using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
    1411
    1512namespace Evaluation.ViewModel
     
    8582        }
    8683
    87         private double bestKnownQuality;
    88 
    89         public double BestKnownQuality
    90         {
    91             get { return this.bestKnownQuality; }
    92             set
    93             {
    94                 this.bestKnownQuality = value;
    95                 this.OnPropertyChanged("BestKnownQuality");
    96             }
    97         }
    98 
    99         private double currentBestQuality;
    100 
    101         public double CurrentBestQuality
    102         {
    103             get { return this.currentBestQuality; }
    104             set
    105             {
    106                 this.currentBestQuality = value;
    107                 this.OnPropertyChanged("CurrentBestQuality");
    108             }
    109         }
    110 
    111         private int evaluations;
    112 
    113         public int Evaluations
    114         {
    115             get { return this.evaluations; }
    116             set
    117             {
    118                 this.evaluations = value;
    119                 this.OnPropertyChanged("Evaluations");
    120             }
    121         }
    122 
    12384        private int nrRuns;
    12485
     
    12990        }
    13091
    131         private int threads;
    132 
    133         public int Threads
    134         {
    135             get { return this.threads; }
    136             set { this.threads = value; this.OnPropertyChanged("Threads"); }
    137         }
    138 
    13992        private int maxLen;
    14093
     
    193146        }
    194147
    195         private string currentRunString;
    196 
    197         public string CurrentRunString
    198         {
    199             get { return this.currentRunString; }
    200             set
    201             {
    202                 currentRunString = value;
    203                 this.OnPropertyChanged("CurrentRunString");
    204             }
    205         }
    206 
    207         private double evaluationsPerSec;
    208 
    209         public double EvaluationsPerSec
    210         {
    211             get { return this.evaluationsPerSec; }
    212             set
    213             {
    214                 evaluationsPerSec = value;
    215                 this.OnPropertyChanged("EvaluationsPerSec");
    216             }
    217         }
    218 
    219         private int bestSolutionFoundAt;
    220 
    221         public int BestSolutionFoundAt
    222         {
    223             get { return this.bestSolutionFoundAt; }
    224             set
    225             {
    226                 bestSolutionFoundAt = value;
    227                 this.OnPropertyChanged("BestSolutionFoundAt");
    228             }
    229         }
    230 
    231         private string bestSolution;
    232 
    233         public string BestSolution
    234         {
    235             get { return this.bestSolution; }
    236             set
    237             {
    238                 bestSolution = value;
    239                 this.OnPropertyChanged("BestSolution");
    240             }
    241         }
    242 
    243148        private ObservableCollection<Run> runs = new ObservableCollection<Run>();
    244149
     
    253158        }
    254159
    255         private Run currentRun;
    256 
    257         public Run CurrentRun
    258         {
    259             get { return this.currentRun; }
    260             set
    261             {
    262                 currentRun = value;
    263                 if (currentRun != null)
    264                 {
    265                     Evaluations = currentRun.Evaluations;
    266                     BestKnownQuality = currentRun.BestKnownQuality;
    267                     CurrentBestQuality = currentRun.BestSolutionQuality;
    268                     EvaluationsPerSec = currentRun.EvaluationsPerSecond;
    269                 }
    270                 this.OnPropertyChanged("CurrentRun");
     160        private Run selectedRun;
     161
     162        public Run SelectedRun
     163        {
     164            get { return this.selectedRun; }
     165            set
     166            {
     167                selectedRun = value;
     168                this.OnPropertyChanged("SelectedRun");
     169            }
     170        }
     171
     172        private string completedRuns;
     173
     174        public string CompletedRuns
     175        {
     176            get { return this.completedRuns; }
     177            set
     178            {
     179                completedRuns = value;
     180                this.OnPropertyChanged("CompletedRuns");
    271181            }
    272182        }
     
    305215            this.algorithms.Add(typeof(SequentialSearch));
    306216            this.algorithms.Add(typeof(MonteCarloTreeSearch));
     217            this.algorithms.Add(typeof(MonteCarloTreeSearch_PruneLeaves));
    307218            this.algorithms.Add(typeof(StandardGP));
    308219            this.algorithms.Add(typeof(OffspringSelectionGP));
    309220
    310             this.selectedAlgorithm = typeof(MonteCarloTreeSearch);
     221            this.selectedAlgorithm = typeof(MonteCarloTreeSearch_PruneLeaves);
    311222        }
    312223
  • branches/HeuristicLab.Problems.GrammaticalOptimization/GraphVizWrapper

    • Property svn:ignore set to
      bin
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Graphviz2.38

    • Property svn:ignore set to
      etc
      fonts
      gtk-2.0
      include
      lib
      pango
      share
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Graphviz2.38/bin

    • Property svn:global-ignores set to
      *.exe
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/ISolver.cs

    r11846 r12781  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    62
    73namespace HeuristicLab.Algorithms.GrammaticalOptimization {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch

    • Property svn:global-ignores set to
      obj
    • Property svn:ignore set to
      bin
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/Base/TreeInfos.cs

    r12762 r12781  
    1313           
    1414        }
    15         public TreeInfos(int totalNodes, int unexpandedNodes, int expandedNodes, int leaveNodes, int deepestLevel,
    16             double averageLevel, double averageChildren)
     15        public TreeInfos(int totalNodes, int unexpandedNodes, int expandedNodes, int leaveNodes, int deepestLevel)
    1716        {
    1817            TotalNodes = totalNodes;
     
    2120            LeaveNodes = leaveNodes;
    2221            DeepestLevel = deepestLevel;
    23             AverageLevel = averageLevel;
    24             AverageChildren = averageChildren;
    2522        }
    2623
     
    3027        public int LeaveNodes { get; set; }
    3128        public int DeepestLevel { get; set; }
    32         public double AverageLevel { get; set; }
    33         public double AverageChildren { get; set; }
    3429    }
    3530}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/HeuristicLab.Algorithms.MonteCarloTreeSearch.csproj

    r12762 r12781  
    4242  <ItemGroup>
    4343    <Compile Include="Base\TreeInfos.cs" />
     44    <Compile Include="MonteCarloTreeSearch.cs" />
    4445    <Compile Include="Simulation\ISimulation.cs" />
    45     <Compile Include="MonteCarloTreeSearch.cs" />
     46    <Compile Include="MonteCarloTreeSearch_PruneLeaves.cs" />
    4647    <Compile Include="Properties\AssemblyInfo.cs" />
    4748    <Compile Include="Base\TreeNode.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.MonteCarloTreeSearch/MonteCarloTreeSearch.cs

    r12762 r12781  
    2020    public class MonteCarloTreeSearch : SolverBase
    2121    {
    22         private readonly int maxLen;
    23         private readonly IProblem problem;
    24         private readonly IGrammar grammar;
    25         private readonly Random random;
    26         private readonly IBanditPolicy behaviourPolicy;
    27         private readonly ISimulation simulation;
    28         private TreeNode rootNode;
    29         private bool isPaused = false;
    30         private object pauseLock = new object();
     22        protected readonly int maxLen;
     23        protected readonly IProblem problem;
     24        protected readonly IGrammar grammar;
     25        protected readonly Random random;
     26        protected readonly IBanditPolicy behaviourPolicy;
     27        protected readonly ISimulation simulation;
     28        protected TreeNode rootNode;
     29        protected bool isPaused = false;
     30        protected object pauseLock = new object();
    3131
    3232        public MonteCarloTreeSearch(IProblem problem, int maxLen, Random random, IBanditPolicy behaviourPolicy,
     
    7171                string phrase = currentNode.phrase;
    7272
    73                 if (phrase.Length <= maxLen)
    74                 {
    75                     // Version 2:
    76                     if (currentNode.children != null && !currentNode.children.Any())
    77                     {
    78                         // already removed all child nodes so remove it too..
    79                         currentNode.parent.children.Remove(currentNode);
    80                         continue;
    81                     }
     73                if (!grammar.IsTerminal(phrase) && phrase.Length <= maxLen)
     74                {
    8275                    ExpandTreeNode(currentNode);
    83                     if (currentNode.children.Any())
    84                     {
    85                         currentNode =
    86                             currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
    87                                 ];
    88                     }
    89                     else
    90                     {
    91                         // Version 2:
    92                         // remove currentNode from tree..
    93                         currentNode.parent.children.Remove(currentNode);
    94                     }
     76                    currentNode =
     77                        currentNode.children[behaviourPolicy.SelectAction(random, currentNode.GetChildActionInfos())
     78                            ];
    9579                }
    9680                if (currentNode.phrase.Length <= maxLen)
     
    10185
    10286                    Propagate(currentNode, quality);
    103 
    104                 }
    105             }
    106         }
    107 
    108         private bool HasNonTerminal(string phrase)
    109         {
    110             foreach (char symbol in phrase)
    111             {
    112                 if (grammar.IsNonTerminal(symbol))
    113                 {
    114                     return true;
    115                 }
    116             }
    117 
    118             return false;
    119         }
    120 
    121         private void ExpandTreeNode(TreeNode treeNode)
     87                }
     88            }
     89        }
     90
     91        protected void ExpandTreeNode(TreeNode treeNode)
    12292        {
    12393            // create children on the first visit
     
    150120        }
    151121
    152         private void Reset()
     122        protected void Reset()
    153123        {
    154124            StopRequested = false;
     
    157127        }
    158128
    159         private void Propagate(TreeNode node, double quality)
     129        protected void Propagate(TreeNode node, double quality)
    160130        {
    161131            var currentNode = node;
     
    167137        }
    168138
     139        private void GetTreeInfosRek(TreeInfos treeInfos, List<TreeNode> children)
     140        {
     141            treeInfos.TotalNodes += children.Count;
     142            foreach (TreeNode child in children)
     143            {
     144                if (child.children != null)
     145                {
     146                    treeInfos.ExpandedNodes++;
     147                    if (treeInfos.DeepestLevel <= child.level)
     148                    {
     149                        treeInfos.DeepestLevel = child.level + 1;
     150                    }
     151                    GetTreeInfosRek(treeInfos, child.children);
     152                }
     153                else
     154                {
     155                    if (grammar.IsTerminal(child.phrase))
     156                    {
     157                        treeInfos.LeaveNodes++;
     158                    }
     159                    else
     160                    {
     161                        treeInfos.UnexpandedNodes++;
     162                    }
     163                }
     164            }
     165        }
     166
    169167        public TreeInfos GetTreeInfos()
    170168        {
    171             int totalNodes = 0;
    172             int unexpandedNodes = 0;
    173             int expandedNodes = 0;
    174             int leaveNodes = 0;
    175             int deepestLevel = 0;
    176             int totalLevel = 0;
    177             int totalChildren = 0;
    178 
    179             List<TreeNode> toDoNodes = new List<TreeNode>();
     169            TreeInfos treeInfos = new TreeInfos();
     170
    180171            if (rootNode != null)
    181172            {
    182                 toDoNodes.Add(rootNode);
    183             }
    184             while (toDoNodes.Any())
    185             {
    186                 TreeNode currentNode = toDoNodes[0];
    187                 toDoNodes.RemoveAt(0);
    188                 totalNodes++;
    189                 if (currentNode.level > deepestLevel)
    190                 {
    191                     deepestLevel = currentNode.level;
    192                 }
    193                 totalLevel += currentNode.level;
    194 
    195                 if (currentNode.children != null)
    196                 {
    197                     totalChildren += currentNode.children.Count;
    198                     toDoNodes.AddRange(currentNode.children);
    199                     expandedNodes++;
     173                treeInfos.TotalNodes++;
     174                if (rootNode.children != null)
     175                {
     176                    treeInfos.ExpandedNodes++;
     177                    treeInfos.DeepestLevel = rootNode.level + 1;
     178                    GetTreeInfosRek(treeInfos, rootNode.children);
    200179                }
    201180                else
    202181                {
    203                     if (!HasNonTerminal(currentNode.phrase))
    204                     {
    205                         leaveNodes++;
     182                    treeInfos.DeepestLevel = rootNode.level;
     183                    if (grammar.IsTerminal(rootNode.phrase))
     184                    {
     185                        treeInfos.LeaveNodes++;
    206186                    }
    207187                    else
    208188                    {
    209                         unexpandedNodes++;
    210                     }
    211                 }
    212             }
    213             return new TreeInfos(totalNodes, unexpandedNodes, expandedNodes, leaveNodes, deepestLevel,
    214                 totalLevel / totalNodes, totalChildren / expandedNodes);
     189                        treeInfos.UnexpandedNodes++;
     190                    }
     191                }
     192            }
     193            return treeInfos;
    215194        }
    216195
    217196        public byte[] GenerateSvg()
    218197        {
    219             IGetStartProcessQuery getStartProcessQuery = new GetStartProcessQuery();
    220             IGetProcessStartInfoQuery getProcessStartInfoQuery = new GetProcessStartInfoQuery();
    221             IRegisterLayoutPluginCommand registerLayoutPluginCommand =
    222                 new RegisterLayoutPluginCommand(getProcessStartInfoQuery, getStartProcessQuery);
    223 
    224             GraphGeneration wrapper = new GraphGeneration(getStartProcessQuery,
    225                 getProcessStartInfoQuery,
    226                 registerLayoutPluginCommand);
    227             wrapper.GraphvizPath = @"../../../Graphviz2.38/bin";
    228             StringBuilder dotFile = new StringBuilder("digraph {");
    229             dotFile.AppendLine();
    230             dotFile.AppendLine("splines=ortho;");
    231             dotFile.AppendLine("concentrate=true;");
    232             dotFile.AppendLine("ranksep=1.2;");
    233 
    234             List<TreeNode> toDoNodes = new List<TreeNode>();
    235             if (rootNode != null)
    236             {
    237                 toDoNodes.Add(rootNode);
    238                 // declare node
    239                 string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, rootNode.actionInfo.Value);
    240 
    241                 dotFile.AppendLine(string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]",
    242                     rootNode.GetHashCode(),
    243                     rootNode.phrase, rootNode.actionInfo.Value, rootNode.actionInfo.Tries, hexColor));
    244             }
    245 
    246             // to put nodes on the same level in graph
    247             Dictionary<int, List<TreeNode>> levelMap = new Dictionary<int, List<TreeNode>>();
    248 
    249             List<TreeNode> sameLevelNodes;
    250 
    251             while (toDoNodes.Any())
    252             {
    253                 TreeNode currentNode = toDoNodes[0];
    254                 toDoNodes.RemoveAt(0);
    255                 // put currentNode into levelMap
    256                 if (levelMap.TryGetValue(currentNode.level, out sameLevelNodes))
    257                 {
    258                     sameLevelNodes.Add(currentNode);
    259                 }
    260                 else
    261                 {
    262                     sameLevelNodes = new List<TreeNode>();
    263                     sameLevelNodes.Add(currentNode);
    264                     levelMap.Add(currentNode.level, sameLevelNodes);
    265                 }
    266 
    267                 // draw line from current node to all its children
    268                 if (currentNode.children != null)
    269                 {
    270                     foreach (TreeNode childNode in currentNode.children)
    271                     {
    272                         toDoNodes.Add(childNode);
    273                         // declare node
    274 
    275                         string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, childNode.actionInfo.Value);
    276                         dotFile.AppendLine(
    277                             string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]",
    278                                 childNode.GetHashCode(),
    279                                 childNode.phrase, childNode.actionInfo.Value, childNode.actionInfo.Tries, hexColor));
    280                         // add edge
    281                         dotFile.AppendLine(string.Format("{0} -> {1}", currentNode.GetHashCode(),
    282                             childNode.GetHashCode()));
    283                     }
    284                 }
    285             }
    286 
    287             // set same level ranks..
    288             foreach (KeyValuePair<int, List<TreeNode>> entry in levelMap)
    289             {
    290                 dotFile.Append("{rank = same;");
    291                 foreach (TreeNode node in entry.Value)
    292                 {
    293                     dotFile.Append(string.Format(" {0};", node.GetHashCode()));
    294                 }
    295                 dotFile.AppendLine("}");
    296             }
    297 
    298             dotFile.Append(" }");
    299             byte[] output = wrapper.GenerateGraph(dotFile.ToString(), Enums.GraphReturnType.Svg);
    300             return output;
    301         }
    302 
    303         private String HexConverter(Color c)
     198            if (GetTreeInfos().TotalNodes < 1000)
     199            {
     200                IGetStartProcessQuery getStartProcessQuery = new GetStartProcessQuery();
     201                IGetProcessStartInfoQuery getProcessStartInfoQuery = new GetProcessStartInfoQuery();
     202                IRegisterLayoutPluginCommand registerLayoutPluginCommand =
     203                    new RegisterLayoutPluginCommand(getProcessStartInfoQuery, getStartProcessQuery);
     204
     205                GraphGeneration wrapper = new GraphGeneration(getStartProcessQuery,
     206                    getProcessStartInfoQuery,
     207                    registerLayoutPluginCommand);
     208                wrapper.GraphvizPath = @"../../../Graphviz2.38/bin";
     209                StringBuilder dotFile = new StringBuilder("digraph {");
     210                dotFile.AppendLine();
     211                dotFile.AppendLine("splines=ortho;");
     212                dotFile.AppendLine("concentrate=true;");
     213                dotFile.AppendLine("ranksep=1.2;");
     214
     215                List<TreeNode> toDoNodes = new List<TreeNode>();
     216                if (rootNode != null)
     217                {
     218                    toDoNodes.Add(rootNode);
     219                    // declare node
     220                    string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, rootNode.actionInfo.Value);
     221
     222                    dotFile.AppendLine(
     223                        string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]",
     224                            rootNode.GetHashCode(),
     225                            rootNode.phrase, rootNode.actionInfo.Value, rootNode.actionInfo.Tries, hexColor));
     226                }
     227
     228                // to put nodes on the same level in graph
     229                Dictionary<int, List<TreeNode>> levelMap = new Dictionary<int, List<TreeNode>>();
     230
     231                List<TreeNode> sameLevelNodes;
     232
     233                while (toDoNodes.Any())
     234                {
     235                    TreeNode currentNode = toDoNodes[0];
     236                    toDoNodes.RemoveAt(0);
     237                    // put currentNode into levelMap
     238                    if (levelMap.TryGetValue(currentNode.level, out sameLevelNodes))
     239                    {
     240                        sameLevelNodes.Add(currentNode);
     241                    }
     242                    else
     243                    {
     244                        sameLevelNodes = new List<TreeNode>();
     245                        sameLevelNodes.Add(currentNode);
     246                        levelMap.Add(currentNode.level, sameLevelNodes);
     247                    }
     248
     249                    // draw line from current node to all its children
     250                    if (currentNode.children != null)
     251                    {
     252                        foreach (TreeNode childNode in currentNode.children)
     253                        {
     254                            toDoNodes.Add(childNode);
     255                            // declare node
     256
     257                            string hexColor = GetHexNodeColor(Color.White, Color.OrangeRed, childNode.actionInfo.Value);
     258                            dotFile.AppendLine(
     259                                string.Format("{0} [label=\"{1}\\n{2:0.00}/{3}\", style=filled, fillcolor=\"{4}\"]",
     260                                    childNode.GetHashCode(),
     261                                    childNode.phrase, childNode.actionInfo.Value, childNode.actionInfo.Tries, hexColor));
     262                            // add edge
     263                            dotFile.AppendLine(string.Format("{0} -> {1}", currentNode.GetHashCode(),
     264                                childNode.GetHashCode()));
     265                        }
     266                    }
     267                }
     268
     269                // set same level ranks..
     270                foreach (KeyValuePair<int, List<TreeNode>> entry in levelMap)
     271                {
     272                    dotFile.Append("{rank = same;");
     273                    foreach (TreeNode node in entry.Value)
     274                    {
     275                        dotFile.Append(string.Format(" {0};", node.GetHashCode()));
     276                    }
     277                    dotFile.AppendLine("}");
     278                }
     279
     280                dotFile.Append(" }");
     281                byte[] output = wrapper.GenerateGraph(dotFile.ToString(), Enums.GraphReturnType.Svg);
     282                return output;
     283            }
     284            return null;
     285        }
     286
     287        protected String HexConverter(Color c)
    304288        {
    305289            return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
    306290        }
    307291
    308         private String GetHexNodeColor(Color weakColor, Color strongColor, double quality)
     292        protected String GetHexNodeColor(Color weakColor, Color strongColor, double quality)
    309293        {
    310294            // convert quality to value between 0 and 1
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorConverters

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCore

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorCss

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorDom

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorModel

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRenderingGdi

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRenderingWpf

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRuntime

    • Property svn:global-ignores set to
      obj
  • branches/HeuristicLab.Problems.GrammaticalOptimization/WpfTestSvgSample

    • Property svn:global-ignores set to
      obj
    • Property svn:ignore set to
      Output
Note: See TracChangeset for help on using the changeset viewer.