Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Particle-Swarm Optimizer #1029

Merged
merged 4 commits into from
Jul 15, 2023
Merged

Added Particle-Swarm Optimizer #1029

merged 4 commits into from
Jul 15, 2023

Conversation

m4rs-mt
Copy link
Owner

@m4rs-mt m4rs-mt commented Jul 7, 2023

This PR adds a massively parallel particle-swarm based optimizer using the optimization engine API from PR #1028. It also includes full integration tests using simple objective functions.

The following code snippet shows a sample use case:

    public readonly struct Objective : IOptimizationFunction<Float32x2, float, float>
    {
        public float Evaluate(LongIndex1D index, Index1D dimension, SingleVectorView<Float32x2> positionView)
        {
            float result = 0;
            for (Index1D i = 0; i < dimension; ++i)
            {
                var vec = positionView[i];
                var dist = vec - new Float32x2(5.0f, 5.0f);
                result += dist.X * dist.X + dist.Y * dist.Y;
            }
            return result / dimension;
        }

        public bool CurrentIsBetter(float current, float proposed) => current <= proposed;
    }

    static void Main()
    {
        using var context = Context.CreateDefault();
        using var accel = context.CreateCPUAccelerator(0, CPUAcceleratorMode.Parallel);
        var stream = accel.DefaultStream;

        var random = new Random();

        var lowerBounds = new float[] { 0.0f, 0.0f, 0.0f, 0.0f };
        var upperBounds = new float[] { 10.0f, 10.0f, 10.0f, 10.0f };

        using var pso = new PSO<Float32x2, float, float, XorShift128Plus>(accel, 512, 4);
        using var optimizer = pso.CreateOptimizer(stream, random, new Objective());

        pso.LoadBounds(stream, lowerBounds, upperBounds);
        pso.LoadParameters(stream, PSO.DefaultFloatParameters);

        var bestPosition = new float[] { 0.0f, 0.0f, 0.0f, 0.0f };

        var run = optimizer.BeginOptimization(stream, bestPosition, float.MaxValue);
        
        for (int i = 0; i < 128; ++i)
            run.Step();

        var result = run.FinishToCPUAsync();
    }

Note that this PR requires #1028 to be merged.

Co-authored by @dfki-mako
Co-authored by @dfki-jugr

@m4rs-mt m4rs-mt added the feature A new feature (or feature request) label Jul 7, 2023
@m4rs-mt m4rs-mt added this to the v1.5 milestone Jul 7, 2023
@m4rs-mt m4rs-mt force-pushed the particle_swarm_opt branch 6 times, most recently from 9876a7a to 45cf816 Compare July 13, 2023 16:18
@m4rs-mt m4rs-mt force-pushed the particle_swarm_opt branch 2 times, most recently from 1b36a64 to fbf7625 Compare July 14, 2023 21:18
@m4rs-mt m4rs-mt merged commit de7c002 into master Jul 15, 2023
@m4rs-mt m4rs-mt deleted the particle_swarm_opt branch July 15, 2023 11:23
m4rs-mt added a commit that referenced this pull request Aug 30, 2023
* Added new Particle-Swam view implementation to track required views during optimization.
* Added new Particle-Swarm based optimization engine.
* Added new Particle-Swarm based optimizer.
* Added new PSO-based optimization tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature (or feature request)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants