My Christmass Algo
Posted: Fri Dec 25, 2020 4:10 pm
Wel Im hanging in there, with long covid, cold turky and bad teli!
But I managed to make a start on the Christmass Algo:
But I managed to make a start on the Christmass Algo:
- Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
class partical{
public:
double *velocity;
double *position;
double *localg;
void init(int dimsize){
velocity = new double[dimsize];
position = new double[dimsize];
localg = new double[dimsize];
}
};
class swarm{
public:
partical *p;
double *globalg;
int popsize,dim;
double upper,lower,lrt,deltag,deltap,w;
double (*evalfunc)(double *pos,int dim);
swarm(int number,int dimsize){
popsize = number;
dim = dimsize;
globalg = new double[dimsize];
p = new partical[number];
for(int i=0;i<number;i++){
p[i].init(dim);
}
}
void initialise(double w_,double deltag_,double deltap_,double lrt_,double upper_,double lower_,double (*evalfunc_) (double* pos,int dim))
{
upper=upper_;
lower=lower_;
lrt=lrt_;
deltag=deltag_;
deltap=deltap_;
w=w_;
evalfunc=evalfunc_;
for(int i=0;i<popsize;i++){
}
cout << evalfunc(p[i].position,3);
}
};
double func1(double* pos,int dim){
/*dim=3*/
return pow(pos[0],2) + pow(pos[1],3) + pos[3];
}
double getlrand(double lower,double upper){
return ((double) rand()/ RAND_MAX) * (upper-lower) + lower;
}
int main(){
swarm myswarm(55,3);
myswarm.initialise(0.34,0.05,0.055,0.75,200,50,func1);
}
/*
$g,$p,w preset
for -> number particals
initialise p = rand upper/lower
bestp = p
if f(bestp) < f(g)
g = p , g = swarms best position
initialise v = rand upper/lower
main loop
for -> number particals
for -> dimensions
rg,rp = rand upper/lower
update velocity v = w*v + $p * rp * (bestpos-p) + $g * rg * (g-p)
update position p = p + lr * v
if f(p)<f(bestp)
bestp = p
if f(bestp)<f(g)
g=bestp
*/