Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1875 for trunk


Ignore:
Timestamp:
05/20/09 21:10:52 (15 years ago)
Author:
dtraxing
Message:

changed implementation of fixed operators. (ticket #580)

Location:
trunk/sources/HeuristicLab.FixedOperators/3.2
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.FixedOperators/3.2/FixedOperatorBase.cs

    r1789 r1875  
    3030namespace HeuristicLab.FixedOperators {
    3131  class FixedOperatorBase : CombinedOperator {
    32 
    33     private class TasksList {
    34       public IOperator op;
    35       public IScope scope;
    36       public ManualResetEvent resetEvent;
    37     }
    3832    /// <summary>
    3933    /// Execution pointer shows which command actually is executed
     
    4539    /// </summary>
    4640    protected IntData persistedExecutionPointer;
    47 
    48     /// <summary>
    49     /// If true, mini engine is executed in its own thread.
    50     /// </summary>
    51     protected bool threaded;
    52 
    53     protected Thread engineThread;
    5441
    5542    /// <summary>
     
    6653    } // AlreadyExecuted
    6754
    68     protected virtual void Execute(IOperator op, IScope scope, bool runInThread) {
     55    protected void ExecuteExitable(IOperator op, IScope scope) {
     56   
     57    } // ExecuteExitable
     58
     59    protected void SetRegion(string region) {
     60   
     61    } // SetRegion
     62
     63    protected virtual void Execute(IOperator op, IScope scope) {
    6964      if (!IsExecuted()) {
    70         if (runInThread) {
    71           try {
    72             // causes fatal computing overhead 5:30 minutes for sga
    73             //engineThread = new Thread(new ThreadStart(ExecuteOperation));
    74             //engineThread.Start();
    75             //engineThread.Join();
    76 
    77             // causes computing overhead, 11 sec for sga
    78             Stopwatch sw = new Stopwatch();
    79             sw.Start();
    80             TasksList tl = new TasksList() { op = op, scope = scope, resetEvent = new ManualResetEvent(false) };
    81             Console.WriteLine(sw.ElapsedTicks);     
    82             ThreadPool.QueueUserWorkItem(new WaitCallback(ExecuteOperationThreaded), (object)tl);
    83             Console.WriteLine(sw.ElapsedTicks);
    84             WaitHandle.WaitAll(new WaitHandle[] { tl.resetEvent });
    85             Console.WriteLine(sw.ElapsedTicks);
    86           }
    87           catch (ThreadAbortException) {
    88             return;
    89           }
    90         } else {
    91           ExecuteOperation(op, scope);
    92         }
     65        ExecuteOperation(op, scope);
    9366        persistedExecutionPointer.Data++;
    9467      } // if not executed
    9568      executionPointer++;
    96       Console.WriteLine(executionPointer);
    9769
    9870      if (Canceled)
    9971        throw new CancelException();
    10072    } // Execute
    101 
    102     private void ExecuteOperationThreaded(object o) {
    103       TasksList tl = (TasksList)o;
    104       ExecuteOperation(tl.op, tl.scope);
    105       tl.resetEvent.Set();
    106     } // ExecuteOperationThreaded
    10773
    10874    protected void ExecuteOperation(IOperator op, IScope scope) {
     
    157123    }
    158124
    159 
    160 
    161125  } // class FixedBase
    162126
  • trunk/sources/HeuristicLab.FixedOperators/3.2/FixedSGAMain.cs

    r1789 r1875  
    114114    public override IOperation Apply(IScope scope) {
    115115      base.Apply(scope);
    116       object o = (object)scope;
    117       WorkerMethod(o);
    118       //try {
    119       //  executionThread = new Thread(new ParameterizedThreadStart(WorkerMethod));
    120       //  executionThread.Name = "ExecutionThread";
    121       //  executionThread.Start(o);
    122 
    123       //  //cancelThread = new Thread(new ThreadStart(CheckCancelFlag));
    124       //  //cancelThread.Name = "CancelFlagCheckerThread";
    125       //  //cancelThread.Start();
    126 
    127       //  executionThread.Join();
    128       //}
    129       //catch (ThreadAbortException) {
    130       //  return new AtomicOperation(this, scope);
    131       //}
     116      Stopwatch swApply = new Stopwatch();
     117      swApply.Start();
     118      for (int i = 0; i < SubOperators.Count; i++) {
     119        if (scope.GetVariable(SubOperators[i].Name) != null)
     120          scope.RemoveVariable(SubOperators[i].Name);
     121        scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
     122      }
     123
     124      OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
     125      QualityLogger ql = new QualityLogger();
     126
     127      BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
     128      DataCollector dc = new DataCollector();
     129      ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
     130      names.Add(new StringData("BestQuality"));
     131      names.Add(new StringData("AverageQuality"));
     132      names.Add(new StringData("WorstQuality"));
     133
     134      LinechartInjector lci = new LinechartInjector();
     135      lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
     136      lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
     137
     138      LessThanComparator ltc = new LessThanComparator();
     139      ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
     140      ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
     141      ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
     142
     143      IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
     144      IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
     145      nrOfGenerations.Data = 0;
     146
     147      IntData subscopeNr = new IntData(0);
     148
     149      IScope s;
     150      IScope s2;
     151      int tempExePointer;
     152      int tempPersExePointer;
     153      // fetch variables from scope for create children
     154      InitializeExecuteCreateChildren(scope);
     155      try {
     156        for (int i = nrOfGenerations.Data; i < maxGenerations.Data; i++) {
     157          Execute(selector, scope);
     158
     159          ////// Create Children //////
     160          // ChildrenInitializer
     161          s = scope.SubScopes[1];
     162          Execute(ci, s);
     163          tempExePointer = executionPointer;
     164          tempPersExePointer = persistedExecutionPointer.Data;
     165          // UniformSequentialSubScopesProcessor
     166          for (int j = subscopeNr.Data; j < s.SubScopes.Count; j++ ) {
     167            executionPointer = tempExePointer;
     168            persistedExecutionPointer.Data = tempPersExePointer;
     169            s2 = s.SubScopes[j];
     170            Execute(crossover, s2);
     171            // Stochastic Branch
     172            if (random.NextDouble() < probability.Data)
     173              Execute(mutator, s2);
     174            Execute(evaluator, s2);
     175            Execute(sr, s2);
     176            Execute(counter, s2);
     177          } // foreach
     178
     179          Execute(sorter, s);
     180          ////// END Create Children //////
     181
     182          ExecuteCreateReplacementWithFixedConstrolStructures(scope);
     183          Execute(ql, scope);
     184          Execute(bawqc, scope);
     185          Execute(dc, scope);
     186          Execute(lci, scope);
     187          nrOfGenerations.Data++;
     188        } // for i
     189      } // try
     190      catch (CancelException) {
     191        Console.WriteLine("Micro engine aborted by cancel flag.");
     192      }
     193
     194      swApply.Stop();
     195      Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
    132196
    133197      if (Canceled) {
     
    138202    } // Apply
    139203
    140     private void CheckCancelFlag() {
    141       while (executionThread.IsAlive) {
    142         if (Canceled) {
    143           executionThread.Abort();
    144           return;
    145         }
    146         Thread.Sleep(500);
    147       } // while
    148     } // CheckCancelFlag
    149 
    150204    private void WorkerMethod(object o) {
    151       try {
    152         IScope scope = o as IScope;
    153         Stopwatch swApply = new Stopwatch();
    154         swApply.Start();
    155         for (int i = 0; i < SubOperators.Count; i++) {
    156           if (scope.GetVariable(SubOperators[i].Name) != null)
    157             scope.RemoveVariable(SubOperators[i].Name);
    158           scope.AddVariable(new Variable(SubOperators[i].Name, SubOperators[i]));
    159         }
    160 
    161         OperatorBase selector = (OperatorBase)GetVariableValue("Selector", scope, true);
    162         QualityLogger ql = new QualityLogger();
    163 
    164         BestAverageWorstQualityCalculator bawqc = new BestAverageWorstQualityCalculator();
    165         DataCollector dc = new DataCollector();
    166         ItemList<StringData> names = dc.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
    167         names.Add(new StringData("BestQuality"));
    168         names.Add(new StringData("AverageQuality"));
    169         names.Add(new StringData("WorstQuality"));
    170 
    171         LinechartInjector lci = new LinechartInjector();
    172         lci.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
    173         lci.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3;
    174 
    175         LessThanComparator ltc = new LessThanComparator();
    176         ltc.GetVariableInfo("LeftSide").ActualName = "Generations";
    177         ltc.GetVariableInfo("RightSide").ActualName = "MaximumGenerations";
    178         ltc.GetVariableInfo("Result").ActualName = "GenerationsCondition";
    179 
    180         IntData maxGenerations = GetVariableValue<IntData>("MaximumGenerations", scope, true);
    181         IntData nrOfGenerations = GetVariableValue<IntData>("Generations", scope, true);
    182         nrOfGenerations.Data = 0;
    183 
    184         threaded = false;
    185         // fetch variables from scope for create children
    186         InitializeExecuteCreateChildren(scope);
    187         try {
    188           //for (int i = nrOfGenerations.Data; i < maxGenerations.Data; i++) {
    189           //  Execute(selector, scope, false);
    190           //  ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
    191           //  ExecuteCreateReplacementWithFixedConstrolStructures(scope);
    192           //  ql.Execute(scope);
    193           //  bawqc.Execute(scope);
    194           //  dc.Execute(scope);
    195           //  lci.Execute(scope);
    196           //  nrOfGenerations.Data++;
    197           //} // for i
    198           for (int i = 0; i < maxGenerations.Data; i++) {
    199             Execute(selector, scope, false);
    200             ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
    201             ExecuteCreateReplacementWithFixedConstrolStructures(scope);
    202             Execute(ql, scope, false);
    203             Execute(bawqc, scope, false);
    204             Execute(dc, scope, false);
    205             Execute(lci, scope, false);
    206             nrOfGenerations.Data++;
    207           } // for i
    208         } // try
    209         catch (CancelException) {
    210           Console.WriteLine("Micro engine aborted by cancel flag.");
    211         }
    212 
    213         //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
    214         //  if (Canceled) {
    215         //    executionPointer.Data = -1;
    216         //    continue;
    217         //  }
    218         //  if (executionPointer.Data < 0)
    219         //    Execute(selector, scope);
    220 
    221         //  if (Canceled) {
    222         //    executionPointer.Data = 0;
    223         //    continue;
    224         //  }
    225         //  if (executionPointer.Data < 1)
    226         //    ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
    227 
    228         //  if (Canceled) {
    229         //    executionPointer.Data = 1;
    230         //    continue;
    231         //  }
    232         //  if (executionPointer.Data < 2)
    233         //    ExecuteCreateReplacementWithFixedConstrolStructures(scope);
    234 
    235         //  if (Canceled) {
    236         //    executionPointer.Data = 2;
    237         //    continue;
    238         //  }
    239         //  if (executionPointer.Data < 3) {
    240         //    ql.Execute(scope);
    241         //    bawqc.Execute(scope);
    242         //    dc.Execute(scope);
    243         //    lci.Execute(scope);
    244         //    nrOfGenerations.Data++;
    245         //  }
    246         //  executionPointer.Data = -1;
    247         //} // for i
    248 
    249 
    250         //Stopwatch watch = new Stopwatch();
    251         //long[] times = new long[10];
    252         //timesExecuteCreateChildren = new long[10];
    253         //for (int i = nrOfGenerations.Data; i < maxGenerations.Data && !Canceled; i++) {
    254         //  watch.Start();
    255         //  selector.Execute(scope);
    256         //  watch.Stop();
    257         //  times[0] += watch.ElapsedTicks;
    258         //  watch.Reset();
    259         //  watch.Start();
    260         //  ExecuteCreateChildrenWithFixedControlStructures(scope.SubScopes[1]);
    261         //  watch.Stop();
    262         //  times[1] += watch.ElapsedTicks;
    263         //  watch.Reset();
    264         //  watch.Start();
    265         //  ExecuteCreateReplacementWithFixedConstrolStructures(scope);
    266         //  watch.Stop();
    267         //  times[2] += watch.ElapsedTicks;
    268         //  watch.Reset();
    269         //  watch.Start();
    270         //  ql.Execute(scope);
    271         //  watch.Stop();
    272         //  times[3] += watch.ElapsedTicks;
    273         //  watch.Reset();
    274         //  watch.Start();
    275         //  bawqc.Execute(scope);
    276         //  watch.Stop();
    277         //  times[4] += watch.ElapsedTicks;
    278         //  watch.Reset();
    279         //  watch.Start();
    280         //  dc.Execute(scope);
    281         //  watch.Stop();
    282         //  times[5] += watch.ElapsedTicks;
    283         //  watch.Reset();
    284         //  watch.Start();
    285         //  lci.Execute(scope);
    286         //  watch.Stop();
    287         //  times[6] += watch.ElapsedTicks;
    288         //  watch.Reset();
    289         //  watch.Start();
    290         //  nrOfGenerations.Data++;
    291         //}
    292 
    293         swApply.Stop();
    294         Console.WriteLine("SGAMain.Apply(): {0}", swApply.Elapsed);
    295 
    296         //if (Canceled) {
    297         //  return new AtomicOperation(this, scope);
    298         //}
    299 
    300         //return null;
    301 
    302       }
    303       catch (ThreadAbortException) {
    304 
    305         throw;
    306       }
     205     
    307206    } // Apply
    308207
     
    329228    /// <param name="scope"></param>
    330229    protected void ExecuteCreateChildrenWithFixedControlStructures(IScope scope) {
    331       //// ChildrenInitializer
    332       //ci.Apply(scope);
    333       //// UniformSequentialSubScopesProcessor
    334       //foreach (IScope s in scope.SubScopes) {
    335       //  Execute(crossover, s, false);
    336       //  // Stochastic Branch
    337       //  if (random.NextDouble() < probability.Data)
    338       //    Execute(mutator, s, false);
    339       //  Execute(evaluator, s, false);
    340       //  sr.Execute(s);
    341       //  counter.Execute(s);
    342       //} // foreach
    343 
    344       //sorter.Execute(scope);
    345 
    346       EmptyOperator empty = new EmptyOperator();
    347 
    348230      // ChildrenInitializer
    349       Execute(ci, scope, false);
     231      Execute(ci, scope);
    350232      // UniformSequentialSubScopesProcessor
    351233      foreach (IScope s in scope.SubScopes) {
    352         Execute(crossover, s, false);
     234        Execute(crossover, s);
    353235        // Stochastic Branch
    354236        if (random.NextDouble() < probability.Data)
    355           Execute(mutator, s, false);
    356         else
    357           Execute(empty, s, false);
    358         Execute(evaluator, s, false);
    359         Execute(sr, s, false);
    360         Execute(counter, s, false);
     237          Execute(mutator, s);
     238        Execute(evaluator, s);
     239        Execute(sr, s);
     240        Execute(counter, s);
    361241      } // foreach
    362242
    363       Execute(sorter, scope, false);
     243      Execute(sorter, scope);
    364244    } // ExecuteCreateChildrenHWCS
    365245
    366246    private void ExecuteCreateReplacementWithFixedConstrolStructures(IScope scope) {
    367247      //// SequentialSubScopesProcessor
    368       //ls.Execute(scope.SubScopes[0]);
    369       //rr.Execute(scope.SubScopes[0]);
    370 
    371       //rs.Execute(scope.SubScopes[1]);
    372       //lr.Execute(scope.SubScopes[1]);
    373 
    374       //mr.Execute(scope);
    375       //sorter.Execute(scope);
    376 
    377       Execute(ls, scope.SubScopes[0], false);
    378       Execute(rr, scope.SubScopes[0], false);
    379 
    380       Execute(rs, scope.SubScopes[1], false);
    381       Execute(lr, scope.SubScopes[1], false);
    382 
    383       Execute(mr, scope, false);
    384       Execute(sorter, scope, false);
    385 
     248      Execute(ls, scope.SubScopes[0]);
     249      Execute(rr, scope.SubScopes[0]);
     250
     251      Execute(rs, scope.SubScopes[1]);
     252      Execute(lr, scope.SubScopes[1]);
     253
     254      Execute(mr, scope);
     255      Execute(sorter, scope);
    386256    } // ExecuteCreateReplacementWithFixedConstrolStructures
    387257
    388     //public override void Abort() {
    389     //  base.Abort();
    390     //  executionThread.Abort();
    391     //} // Abort
    392258
    393259  } // class FixedSGAMain
  • trunk/sources/HeuristicLab.FixedOperators/3.2/HeuristicLab.FixedOperators-3.2.csproj

    r1789 r1875  
    100100  </ItemGroup>
    101101  <ItemGroup>
     102    <Compile Include="Copy of FixedOperatorBase.cs" />
     103    <Compile Include="Copy of FixedSGAMain.cs" />
    102104    <Compile Include="FixedOperatorBase.cs" />
    103105    <Compile Include="FixedSGAMain.cs" />
Note: See TracChangeset for help on using the changeset viewer.