- Home /

# Particle Swarm Optimization

Hello Unity Community! I'm a bit new at Unity. I'm trying to do Particle Swarm Optimization. I've a cube and I want to optimize positions as particle's best position(pBest) and global best position(gBest). My cube produces the lowest cost (has the highest fitness). This value is called pBest (particle best). The other best value is the current best solution of the swarm, i.e., the best solution by any particle in the swarm. I'm following the simple formula :

v' = v + c1.r1.(pBest - x) + c2.r2.(gBest - x) x' = x + v'

v is the current velocity, v' the new velocity, x the current position, x' the new position, pBest and gBest as stated above, r1 and r2 are even distributed random numbers in the interval [0, 1], and c1 and c2 are acceleration coefficients. Where c1 is the factor that influences the cognitive behaviour, i.e., how much the particle will follow its own best solution, and c2 is the factor for social behaviour, i.e., how much the particle will follow the swarm's best solution.

I'll compare pBest and gBest in my manageRandom class and update the gBest value in my yeniclass.

Here is the manageRandom.cs script

```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class manageRandom : MonoBehaviour {
public GameObject[] Cube;
public Transform prefab;
public Rigidbody rb ;
public float radius ;
public Vector3 targetPosition ;
public Vector3 centerPoint ;
public Vector3 bestPosition ;
public float randomVelocity ;
public Vector3 newVel;
public int i=0;
public int count = 0;
public List<Transform> particles = new List<Transform>();
void Start () {
rb = GetComponent<Rigidbody>();
for(int i= 0; i < count; i++)
{
Transform t = (Transform)Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
particles.Add(t);
}
yeniclass.gBest = transform.position;
targetPosition = centerPoint + Random.insideUnitSphere * radius;
targetPosition.y =Terrain.activeTerrain.SampleHeight(targetPosition);
Debug.DrawRay(targetPosition, Vector3.up * 10f, Color.blue, 10f); }
void FixedUpdate()
{
Vector3 gBest; Vector3 pBest;
float r1 = Random.Range(0.0f,1.0f); float r2 = Random.Range(0.0f,1.0f);
float c1 =1.0f ; float c2 =1.0f ;
randomVelocity = Random.Range(0.0f,5.0f);
GetComponent<Rigidbody>().velocity = Random.onUnitSphere * randomVelocity;
gBest = yeniclass.gBest;
for(i = 0 ; i < count ; i++)
{
if ((targetPosition - transform.position).magnitude < (targetPosition - bestPosition).magnitude) {
pBest = transform.position; gBest = targetPosition;
newVel = rb.velocity + c1 * r1 * (pBest - transform.position) + c2 * r2 * (gBest - transform.position);
bestPosition = transform.position + newVel;
}
yeniclass.gBest = gBest;
//
}
rb.velocity = newVel;
rb.AddForce (newVel.normalized, ForceMode.VelocityChange);
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(centerPoint, radius);
}
void Update () {
}
}
```

and here is the yeniclass.cs

```
using UnityEngine;
using System.Collections;
public class yeniclass : MonoBehaviour {
public manageRandom script;
public Vector3 currentPosition;
public Vector3 pBest = Vector3.zero;
public static Vector3 gBest = Vector3.zero;
void Start()
{
}}
```

Any help would be appreciated. Thank you!

Looks like the math is for manually computing net movement of the objects? v-prime is the movement in the current "frame"? I think that sort of code is meant to produce the ending position of each particle? That would mean no rigidbodies and definitely no AddForce.

AddForce adds to the previous velocity, so is double-counting all previous forces (and applying fake friction.)

But, what's the question? How is it not working? I think this is a better forum Q (but, around these parts, an equation in 10 variables isn't regraded as simple. Generic math-heavy stuff tends to do better in non-Unity forums.)

I partially solved problem. I applied steps of particle swarm optimization algorithm. I think i couldn't explain my problem clearly.Now i'm only apply mouse-click options and moving this clicked position. Thanks your answer!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.