【问题标题】:error CS0246: The type or namespace name `AForge' could not be found. Are you missing a using directive or an assembly reference?错误 CS0246:找不到类型或命名空间名称“AForge”。您是否缺少 using 指令或程序集引用?
【发布时间】:2015-11-30 18:39:41
【问题描述】:

我正在尝试在 Unity3D 中使用遗传算法制作一个 n-Queens,但每次都会出现此错误...

代码:

using UnityEngine;
using System;
using System.Collections;

using AForge.Genetic;
using AForge.Math;

namespace AlgoritmoGenetico
{
public class GA : MonoBehaviour {

    int populationSizeBox;
    int iterationsBox;
    int nRainhasBox;
    int crossoverRateBox;
    int motacaoRateBox;
    int paradaBox;
    //int selecao;

    private String log = "";
    private int nRainhas = 14;
    private int nPopulacao = 14;
    private int nGeracoes = 8000;
    private int nParada = 100;
    private double crossoverRate = 0.75;
    private double mutationRate = 0.01;


    // Use this for initialization
    void Start () {
        Iniciar ();
    }

    // Update is called once per frame
    void Update () {

    }

    public void Iniciar(){
        configuraAlgoritimo();
        int selecao = 0; // definimos para o metodo roleta
        ISelectionMethod metodoDeSelecao = (selecao == 0) ? (ISelectionMethod)new RouletteWheelSelection() :
            (selecao == 1) ? (ISelectionMethod)new EliteSelection() :
                (ISelectionMethod)new RankSelection();

        AvaliadorDeRainhas avaliador = new AvaliadorDeRainhas();
        Population populacao = new Population(nPopulacao, new ShortArrayChromosome(nRainhas, nRainhas - 1), avaliador, metodoDeSelecao);
        populacao.CrossoverRate = crossoverRate;
        populacao.MutationRate = mutationRate;


        int iteracao = 0;
        int pararEm = nParada;
        while (iteracao < nGeracoes)
        {
            populacao.RunEpoch();

            if (nParada > 0 && iteracao == pararEm)
            {
                atualizaDadosPara(iteracao, populacao);

                pararEm += nParada;
            }
            if (populacao.BestChromosome.Fitness == nRainhas)
                break;
            iteracao++;
        }

        atualizaDadosPara(iteracao,populacao);
    }

    private void atualizaDadosPara(int iteracao,Population populacao)
    {
        log = "Geração: " + iteracao +
            "\n Método de Seleção : " + populacao.SelectionMethod +
                "\n Avaliação Média: " + populacao.FitnessAvg +
                "\n Melhor Avaliação : " + populacao.FitnessMax +
                "\n Melhor indivíduo: " + populacao.BestChromosome.ToString();
        print (log);
    }

    private void configuraAlgoritimo(){
        try
        {
            nPopulacao = Math.Max(10, Math.Min(100, int.Parse(populationSizeBox)));
        }
        catch
        {
            nPopulacao = 8;
        }
        try
        {
            nGeracoes = Math.Max(0, int.Parse(iterationsBox));
        }
        catch
        {
            nGeracoes = 100;
        }
        try
        {
            nRainhas = Math.Max(4, int.Parse(nRainhasBox));
        }
        catch
        {
            nRainhas = 8;
        }
        try
        {
            crossoverRate = Math.Max(0.0, int.Parse(crossoverRateBox));
        }
        catch
        {
            crossoverRate = 0.75;
        }
        try
        {
            mutationRate = Math.Max(0.0, int.Parse(motacaoRateBox));
        }
        catch
        {
            mutationRate = 0.01;
        }
        try
        {
            nParada = Math.Max(0, int.Parse(paradaBox));
        }
        catch
        {
            nParada = 0;
        }
    }
}
}

【问题讨论】:

  • 您的项目似乎缺少对 AForge 库的引用。您可以在 ProjectName.CSharp 文件夹的 References 文件夹中找到它吗? Unity3D 项目也可能不支持此库。

标签: c# unity3d aforge n-queens genetic


【解决方案1】:

我已经重现了您的案例中出现的问题。 您在项目的 \Assets 文件夹中缺少 AForge.dll 文件。

您要查找的 DLL 应位于您可能已从 AForge.NET 站点下载压缩包的 AForge.NET Framework-x.x.x-(libs only)\Release 文件夹中。

如果您仍然难以找到它,请考虑从选择[ Download Libraries Only ] 重新下载整个软件包:

http://www.aforgenet.com/framework/downloads.html

我还解决了您在那里遇到的一些问题。如果已将 int 值声明为 int,则无需使用 int.Parse() 进行转换。 只需使用您正在使用的功能执行Math.Max(x, y) 等。 此外,您没有使用 AForge.Math 命名空间中的任何内容。如果这是有意为之,请考虑删除 using AForge.Math;,因为它未使用。

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 1970-01-01
    • 2021-08-20
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多