Exemplo n.º 1
0
 private HeuristicAction _tryHeuristic(TekHeuristic heuristic, TekBoard board)
 {
     if (heuristic != null)
     {
         try
         {
             temStoredResults.Add(new TekHeuristicResult(heuristic));
             heuristic.ExecuteAction(temMoves);
             return(HeuristicAction.haNone);
         }
         catch (ETekFieldInvalid)
         {
             return(HeuristicAction.haExcludeValue);
         }
     }
     else if (board.IsSolved())
     {
         foreach (TekHeuristicResult result in temStoredResults)
         {
             heuristics.PrecomputedResults.Add(new TekHeuristicResult(result));
         }
         return(HeuristicAction.haSetValue);
     }
     else
     {
         return(HeuristicAction.haImpossible);
     }
 }
Exemplo n.º 2
0
 public TekHeuristicResult(TekHeuristicResult result)
 {
     Heuristic       = result.Heuristic;
     HeuristicFields = new List <TekField>(result.HeuristicFields);
     AffectedFields  = new List <TekField>(result.AffectedFields);
     HeuristicValues = new List <int>(result.HeuristicValues);
 }
Exemplo n.º 3
0
 public TekHeuristicResult(TekHeuristic heuristic)
 {
     Heuristic       = heuristic;
     HeuristicFields = new List <TekField>(Heuristic.HeuristicFields);
     AffectedFields  = new List <TekField>(Heuristic.AffectedFields);
     HeuristicValues = new List <int>(heuristic.HeuristicValues);
 }
Exemplo n.º 4
0
 private bool AfterHeuristicExecution(TekHeuristic heuristic)
 {
     View.HighlightFields(heuristic.AffectedFields, false);
     View.Selector.ClearMultiSelect();
     View.Refresh();
     return(true);
 }
Exemplo n.º 5
0
 public void SaveConfiguration(StreamWriter sw)
 {
     sw.WriteLine(HEURISTICS);
     for (int i = 0; i < HeuristicIndex.Count; i++)
     {
         TekHeuristic heuristic = Heuristics[HeuristicIndex[i]];
         sw.WriteLine(HEURFORMAT, i, heuristic.Description, heuristic.Enabled?"":"(disabled)");
     }
 }
Exemplo n.º 6
0
        public void SetHeuristicEnabled(string description, bool value)
        {
            TekHeuristic heuristic = GetHeuristic(description);

            if (heuristic != null)
            {
                heuristic.Enabled = value;
            }
        }
Exemplo n.º 7
0
 private void AfterHeuristicFoundHandler(TekHeuristic heuristic)
 {
     heurFound++;
     HeuristicDescription = String.Format("{0}: {1}", heurFound, heuristic.AsString());
     listBox1.Items.Add(HeuristicDescription);
     listBox1.SelectedIndex = listBox1.Items.Count - 1;
     listBox1.Refresh();
     LogHeuristic(HeuristicDescription);
     ShowHeuristicFields(heuristic.HeuristicFields, heuristic.AffectedFields);
 }
Exemplo n.º 8
0
        public bool GetHeuristicEnabled(string description)
        {
            TekHeuristic heuristic = GetHeuristic(description);

            if (heuristic != null)
            {
                return(heuristic.Enabled);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 9
0
 public void SetHeuristicDescriptions(List <string> descriptions)
 {
     HeuristicIndex.Clear();
     for (int i = 0; i < descriptions.Count; i++)
     {
         string       description = descriptions[i];
         TekHeuristic heuristic   = GetHeuristic(description);
         if (heuristic != null)
         {
             HeuristicIndex.Add(Heuristics.IndexOf(heuristic));
         }
     }
 }
Exemplo n.º 10
0
 public void Dump(StreamWriter sw)
 {
     sw.WriteLine("heuristics (in order of application):");
     for (int i = 0; i < HeuristicIndex.Count; i++)
     {
         TekHeuristic heuristic = Heuristics[HeuristicIndex[i]];
         string       s         = String.Format("{0}: {1}", i + 1, heuristic.Description);
         if (!heuristic.Enabled)
         {
             s = s + "[disabled]";
         }
         sw.WriteLine(s);
     }
 }
Exemplo n.º 11
0
        public bool HeuristicSolve(TekBoard board, TekMoves moves)
        {
            TekHeuristic heuristic = FindHeuristic(board);
            bool         Paused    = false;

            while (heuristic != null && !Paused)
            {
                AfterHeuristicFoundHandler?.Invoke(heuristic);
                StoreResult(heuristic);
                if (BeforeExecutionHandler != null && !BeforeExecutionHandler(heuristic))
                {
                    return(false);
                }
                heuristic.ExecuteAction(moves);
                AfterExecutionHandler?.Invoke(heuristic);
                heuristic = FindHeuristic(board);
            }
            return(board.IsSolved());
        }
Exemplo n.º 12
0
 public TekHeuristic FindHeuristic(TekBoard board)
 {
     board.AutoNotes = true;
     if (PrecomputedResults.Count > 0)
     {
         TekHeuristic result = PrecomputedResults[0].AsHeuristic();
         PrecomputedResults.RemoveAt(0);
         return(result);
     }
     for (int i = 0; i < HeuristicIndex.Count; i++)
     {
         TekHeuristic heuristic = Heuristics[HeuristicIndex[i]];
         if (heuristic.Enabled && heuristic.Applies(board))
         {
             return(heuristic);
         }
     }
     return(null);
 }
Exemplo n.º 13
0
 private bool BeforeHeuristicExecution(TekHeuristic heuristic)
 {
     Application.DoEvents();
     if (checkBox1.Checked || Paused)
     {
         Paused = true;
         while (Paused)
         {
             Application.DoEvents(); // Delphi style, is supposed to be unsafe
             if (isFinished)
             {
                 return(false);
             }
         }
     }
     if (!isFinished)
     {
         Snapshots.TakeSnapshot(HeuristicDescription);
     }
     return(true);
 }
Exemplo n.º 14
0
 public int StoreResult(TekHeuristic heuristic)
 {
     StoredResults.Add(new TekHeuristicResult(heuristic));
     return(StoredResults.Count - 1);
 }