Ejemplo n.º 1
0
    public void Parse(OpenPSO.Lib.IFunction function, int nSamples, Vector2 samplingInterval, float yScale = 1.0f)
    {
        nPoints        = new Vector2Int(nSamples, nSamples);
        functionValues = new float[nSamples, nSamples];

        float x1 = float.MaxValue;
        float x2 = -float.MaxValue;
        float y1 = float.MaxValue;
        float y2 = -float.MaxValue;

        minY = float.MaxValue;
        maxY = -float.MaxValue;

        for (int y = 0; y < nSamples; y++)
        {
            double py = samplingInterval.x + (samplingInterval.y - samplingInterval.x) * ((double)y / nSamples);

            y1 = Mathf.Min((float)py, y1);
            y2 = Mathf.Max((float)py, y2);

            for (int x = 0; x < nSamples; x++)
            {
                double px = samplingInterval.x + (samplingInterval.y - samplingInterval.x) * ((double)x / nSamples);

                float v = (float)function.Evaluate(new List <double> {
                    px, py
                }) * yScale;
                functionValues[y, x] = v;
                minY = Mathf.Min(v, minY);
                maxY = Mathf.Max(v, maxY);
                x1   = Mathf.Min((float)px, x1);
                x2   = Mathf.Max((float)px, x2);
            }
        }

        extents = new Rect(x1, y1, x2 - x1, y2 - y1);

        OnDataProcessed();
    }
Ejemplo n.º 2
0
    void Awake()
    {
        // Setup OpenPSO.NET
        if (algorithm == Algorithm.SSPSO)
        {
            throw new NotImplementedException();
        }

        OpenPSO.Lib.IFunction ifunction = null;
        switch (problem)
        {
        case Problem.Sphere:
            ifunction = new OpenPSO.Functions.Sphere();
            break;

        case Problem.Quadric:
            ifunction = new OpenPSO.Functions.Quadric();
            break;

        case Problem.Hyper:
            ifunction = new OpenPSO.Functions.HyperEllipsoid();
            break;

        case Problem.Rastrigin:
            ifunction = new OpenPSO.Functions.Rastrigin();
            break;

        case Problem.Griewank:
            ifunction = new OpenPSO.Functions.Griewank();
            break;

        case Problem.Schaffer:
            ifunction = new OpenPSO.Functions.Schaffer2();
            break;

        case Problem.Weierstrass:
            ifunction = new OpenPSO.Functions.Weierstrass();
            break;

        case Problem.Ackley:
            ifunction = new OpenPSO.Functions.Ackley();
            break;

        case Problem.ShiftedQuadricWithNoise:
            throw new NotImplementedException();

        case Problem.RotatedGriewank:
            throw new NotImplementedException();

        case Problem.PerlinLandscape:
            ifunction = new OpenPSO.Functions.PerlinLandscape(perlinOctaves, perlinAmplitude, perlinAmplitudePerOctave,
                                                              perlinFrequency.x, perlinFrequency.y, perlinFrequencyPerOctave,
                                                              perlinOffset.x, perlinOffset.y);
            break;

        case Problem.ImageValue:
            if (!image.isReadable)
            {
                throw new ArgumentException("Image must be readable to use with Image Value function!");
            }
            ifunction = new PSOImageValueEvaluator(image,
                                                   new Rect(initialX.x, initialX.x, initialX.y - initialX.x, initialX.y - initialX.x),
                                                   (invertImage) ?(-1.0):(1.0));
            break;

        case Problem.ImageSaturation:
            if (!image.isReadable)
            {
                throw new ArgumentException("Image must be readable to use with Image Saturation function!");
            }
            ifunction = new PSOImageSaturationEvaluator(image,
                                                        new Rect(initialX.x, initialX.x, initialX.y - initialX.x, initialX.y - initialX.x),
                                                        (invertImage) ? (-1.0) : (1.0));
            break;

        case Problem.RalphBellCurveMean:
        case Problem.RalphBellCurveVariance:
            if (!image.isReadable)
            {
                throw new ArgumentException("Image must be readable to use with Image Saturation function!");
            }
            ifunction = new PSOImageRalphBellCurve(image,
                                                   new Rect(initialX.x, initialX.x, initialX.y - initialX.x, initialX.y - initialX.x),
                                                   (invertImage) ? (-1.0) : (1.0),
                                                   sampleRadius, problem == Problem.RalphBellCurveMean, useStimulus);
            break;

        default:
            throw new NotImplementedException();
        }

        OpenPSO.Lib.ITopology itopology = null;
        switch (topology)
        {
        case Topology.StaticRing1D:
            throw new NotImplementedException();

        case Topology.StaticGrid2D:
            switch (neighborhoodType)
            {
            case NeightborhoodType.Moore:
                itopology = new OpenPSO.Lib.Topologies.MooreGridTopology(dimension.x, dimension.y);
                break;

            case NeightborhoodType.VN:
                itopology = new OpenPSO.Lib.Topologies.VonNeumannGridTopology(dimension.x, dimension.y);
                break;

            case NeightborhoodType.Ring:
                throw new NotImplementedException();

            default:
                break;
            }
            break;

        case Topology.StaticGraph:
            throw new NotImplementedException();

        case Topology.Global:
            itopology = new OpenPSO.Lib.Topologies.GlobalTopology(nParticles);
            break;

        default:
            break;
        }

        OpenPSO.Lib.PSO pso = new OpenPSO.Lib.PSO(
            p => true,
            p => omega,
            p => c1,
            p => c2,
            p => - xMax,
            p => xMax,
            p => vMax,
            initialX.x,
            initialX.y,
            ifunction,
            2,
            maxEvaluations,
            stopCriterion,
            continueAfterStop,
            itopology,
            seed);

        // Setup PSORender
        var psoRender = GetComponent <PSORender>();

        psoRender.enabled                  = true;
        psoRender.function                 = ifunction;
        psoRender.functionSamples          = functionSamplingSize;
        psoRender.functionSamplingInterval = new Vector2(initialX.x, initialX.y);
        psoRender.pso = pso;
        if (IsImage())
        {
            psoRender.texture = image;
        }
        else
        {
            psoRender.texture = null;
        }
    }