Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by emrano · Jul 20, 2015 at 10:59 AM · prefabarray

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 20, 2015 at 03:26 PM 0
Share

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.)

avatar image emrano · Jul 21, 2015 at 07:05 AM 0
Share

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!

avatar image sanizohra10 · Mar 22, 2019 at 06:49 AM 0
Share

Please share screenshots of this project @emrano

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to instantiate random enemy prefab from array 3 Answers

Resizeable gameObject array. 1 Answer

Add prefabs from a folder to array as GameObject 2 Answers

setactive doesn't appear to work on instanciated prefab gameobject array 2 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges