【问题标题】:Check for items visited in text file检查文本文件中访问的项目
【发布时间】:2013-05-25 05:48:02
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 尝试 google .. 关键字流阅读器 .. 列表添加 ..
  • 谢谢...我使用了一个列表,但我需要一个计数器来检查有多少项目,例如Deposit(1).Deposit(2).Deposit(3).Deposit(1).Deposit(4),我想防止在Deposit(4)之前访问Deposit(1)
  • 将代码添加到问题中......希望我能帮助你(实际上我在 VB 但我会尝试......)
  • 谢谢你@matzone....我已经编辑了...请回复我

标签: c# text-files redundancy tabu-search


【解决方案1】:

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

【讨论】:

  • 谢谢...我使用了一个列表,但我需要一个计数器来检查有多少项目,例如Deposit(1).Deposit(2).Deposit(3).Deposit(1).Deposit(4),我想防止在Deposit(4)之前访问Deposit(1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多