-1

I have a code that reads items in a text file. It reads them line-by-line. When an item is read it will be added to a list that prevents it from revisited again. When the list is full (max size), it will be cleared. However, there is a need to check for the items added to the list in order to prevent re-visiting this specific item for a predefined value even though if the list was cleared.

Please help me to figure out how to do in C# 2012.

namespace SearchTechniques.Algorithms
{
using System;
using System.Collections.Generic;

public abstract class TSBase : SearchTechniquesBase
{
    // if Tabu list reaches the size (MaximumTabuListSize), it will be cleared.
    private readonly int MaximumTabuListSize = 8;

    public TSBase()
    {
        _tabuList = new List<object>();
    }

    protected override void RunAlgorithm(List<object> solutions)
    {
        _solutions = new List<object>();
        _tabuList.Clear();
        var solution = solutions[0];
        solutions.RemoveAt(0);
        while (solution != null)
        {
            _logger.Log("\t" + solution.ToString() + " - considering as next best solution not in tabu list based on cost function\n");
            _solutions.Add(solution);
            UpdateTabuList(solution);
            solution = FindNextBestSolution(solution, solutions);
            if (null != solution)
            {
                solutions.Remove(solution);
            }
        }
    }

    // updating tabu list
    private void UpdateTabuList(object solution)
    {
        _tabuList.Add(solution);
        if (_tabuList.Count >= MaximumTabuListSize)
        {
            _logger.Log("clearing tabu list as already reached: " + MaximumTabuListSize.ToString() + "\n");
            _tabuList.Clear();
        }
    }

    // finding the next best solution
    protected abstract object FindNextBestSolution(object solution, List<object> solutions);

    // the _solutions are both the list of current solutions and the tabu list in our case
    protected abstract bool SolutionExistsInTabuList(object solution);

    protected List<object> _tabuList;
}
 }

Thanks

1 Answers1

0

Use a List<string> The .Contains() method will help determine if you've read that item already. If you want a list to check against even after you clear it, you'll need 2 lists.

tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • thank u...I used a list but I will need a counter to check how many items e.g. Deposit(1).Deposit(2).Deposit(3).Deposit(1).Deposit(4), I want to prevent visiting Deposit(1) before Deposit(4) – Ammar Sultan May 25 '13 at 11:25