summaryrefslogtreecommitdiff
path: root/sccomp/source/solver/ParticelSwarmOptimization.hxx
blob: ad1103309341889d490edb6b26948223ea76202d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#pragma once

#include <vector>
#include <random>
#include <limits>

struct Particle
{
    Particle(size_t nDimensionality)
        : mVelocity(nDimensionality)
        , mPosition(nDimensionality)
        , mCurrentFitness(std::numeric_limits<float>::lowest())
        , mBestPosition(nDimensionality)
        , mBestFitness(std::numeric_limits<float>::lowest())
    {
    }

    std::vector<double> mVelocity;

    std::vector<double> mPosition;
    double mCurrentFitness;

    std::vector<double> mBestPosition;
    double mBestFitness;
};

template <typename DataProvider> class ParticleSwarmOptimizationAlgorithm
{
private:
    // inertia
    static constexpr double constWeight = 0.729;
    // cognitive coefficient
    static constexpr double c1 = 1.49445;
    // social  coefficient
    static constexpr double c2 = 1.49445;

    static constexpr double constAcceptedPrecision = 0.000000001;

    DataProvider& mrDataProvider;

    size_t mnNumOfParticles;

    std::vector<Particle> maSwarm;

    std::random_device maRandomDevice;
    std::mt19937 maGenerator;
    size_t mnDimensionality;

    std::uniform_real_distribution<> maRandom01;

    std::vector<double> maBestPosition;
    double mfBestFitness;
    int mnGeneration;
    int mnLastChange;

public:
    ParticleSwarmOptimizationAlgorithm(DataProvider& rDataProvider, size_t nNumOfParticles)
        : mrDataProvider(rDataProvider)
        , mnNumOfParticles(nNumOfParticles)
        , maGenerator(maRandomDevice())
        , mnDimensionality(mrDataProvider.getDimensionality())
        , maRandom01(0.0, 1.0)
        , maBestPosition(mnDimensionality)
        , mfBestFitness(std::numeric_limits<float>::lowest())
        , mnGeneration(0)
        , mnLastChange(0)
    {
    }

    std::vector<double> const& getResult() { return maBestPosition; }

    int getGeneration() { return mnGeneration; }

    int getLastChange() { return mnLastChange; }

    void initialize()
    {
        mnGeneration = 0;
        mnLastChange = 0;
        maSwarm.clear();

        mfBestFitness = std::numeric_limits<float>::lowest();

        maSwarm.reserve(mnNumOfParticles);
        for (size_t i = 0; i < mnNumOfParticles; i++)
        {
            maSwarm.emplace_back(mnDimensionality);
            Particle& rParticle = maSwarm.back();

            mrDataProvider.initializeVariables(rParticle.mPosition, maGenerator);
            mrDataProvider.initializeVariables(rParticle.mVelocity, maGenerator);

            for (size_t k = 0; k < mnDimensionality; k++)
            {
                rParticle.mPosition[k] = mrDataProvider.clampVariable(k, rParticle.mPosition[k]);
            }

            rParticle.mCurrentFitness = mrDataProvider.calculateFitness(rParticle.mPosition);

            for (size_t k = 0; k < mnDimensionality; k++)
            {
                rParticle.mPosition[k] = mrDataProvider.clampVariable(k, rParticle.mPosition[k]);
            }

            rParticle.mBestPosition.insert(rParticle.mBestPosition.begin(),
                                           rParticle.mPosition.begin(), rParticle.mPosition.end());
            rParticle.mBestFitness = rParticle.mCurrentFitness;

            if (rParticle.mCurrentFitness > mfBestFitness)
            {
                mfBestFitness = rParticle.mCurrentFitness;
                maBestPosition.insert(maBestPosition.begin(), rParticle.mPosition.begin(),
                                      rParticle.mPosition.end());
            }
        }
    }

    bool next()
    {
        bool bBestChanged = false;

        for (Particle& rParticle : maSwarm)
        {
            double fRandom1 = maRandom01(maGenerator);
            double fRandom2 = maRandom01(maGenerator);

            for (size_t k = 0; k < mnDimensionality; k++)
            {
                rParticle.mVelocity[k]
                    = (constWeight * rParticle.mVelocity[k])
                      + (c1 * fRandom1 * (rParticle.mBestPosition[k] - rParticle.mPosition[k]))
                      + (c2 * fRandom2 * (maBestPosition[k] - rParticle.mPosition[k]));

                mrDataProvider.clampVariable(k, rParticle.mVelocity[k]);

                rParticle.mPosition[k] += rParticle.mVelocity[k];
                rParticle.mPosition[k] = mrDataProvider.clampVariable(k, rParticle.mPosition[k]);
            }

            rParticle.mCurrentFitness = mrDataProvider.calculateFitness(rParticle.mPosition);

            if (rParticle.mCurrentFitness > rParticle.mBestFitness)
            {
                rParticle.mBestFitness = rParticle.mCurrentFitness;
                rParticle.mBestPosition.insert(rParticle.mBestPosition.begin(),
                                               rParticle.mPosition.begin(),
                                               rParticle.mPosition.end());
            }

            if (rParticle.mCurrentFitness > mfBestFitness)
            {
                if (std::abs(rParticle.mCurrentFitness - mfBestFitness) > constAcceptedPrecision)
                {
                    bBestChanged = true;
                    mnLastChange = mnGeneration;
                }
                maBestPosition.insert(maBestPosition.begin(), rParticle.mPosition.begin(),
                                      rParticle.mPosition.end());
                mfBestFitness = rParticle.mCurrentFitness;
            }
        }
        mnGeneration++;
        return bBestChanged;
    }
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */