错误CS0246:找不到类型或命名空间名称“AForge”。您是否缺少using指令或程序集引用?

时间:2015-11-30 18:39:42

标签: c# unity3d aforge n-queens genetic

我正在尝试使用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;
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

我重新创建了你案件中出现的问题。 您在项目的AForge.dll文件夹中缺少\Assets个文件。

您正在寻找的

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;命名空间中的任何内容。如果这是故意的,请考虑删除void setup(){ Serial.begin(9800); pinMode(13, OUTPUT); float previous_voltage_reading = 100; } void loop() { int sensorValue5 = analogRead(A5); float voltage5 = sensorValue5 * (5.0 / 1023.0); Serial.println(voltage5); if (voltage5 > 0.56 && voltage5 < 0.66 && voltage5 > previous_voltage_reading) { digitalWrite(13, HIGH); Serial.println("TURNING BULB ON!"); delay(200); digitalWrite(13, LOW); Serial.println("TURNING BULB OFF!"); } previous_voltage_reading = voltage5; } ,因为它未被使用。