-1

我有一个读取文本文件中项目的代码。它逐行读取它们。当一个项目被读取时,它将被添加到一个列表中,以防止它再次被重新访问。当列表已满(最大大小)时,它将被清除。但是,需要检查添加到列表中的项目,以防止重新访问此特定项目以获得预定义值,即使列表已被清除。

请帮我弄清楚如何在 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;
}
 }

谢谢

4

1 回答 1

0

使用List<string> .Contains() 方法将有助于确定您是否已经阅读过该项目。如果您希望在清除列表后仍对其进行检查,则需要 2 个列表。

于 2013-05-25T07:16:55.803 回答