-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

.Contains() メソッドを使用すると、List<string> そのアイテムを既に読んだかどうかを判断するのに役立ちます。リストをクリアした後もチェックする場合は、2 つのリストが必要です。

于 2013-05-25T07:16:55.803 に答える