Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have at the moment a Genetic Algorithm oriented for Tsp Solving, and I whant to change some part's of the Genetic Algorithm to run in Multi-Thread.

The first function that I whant to change to Multi-Threading is the fitness function.
Can someone give me some tips about the changes needed for running the fitness function in multi thread?
C#
public void CalculateCityDistances( int numberOfCloseCities )
        {
            foreach (City city in this)
            {
                city.Distances.Clear();

                for (int i = 0; i < Count; i++)
                {
                    city.Distances.Add(Math.Sqrt(Math.Pow((double)(city.Location.X - this[i].Location.X), 2D) +
                                       Math.Pow((double)(city.Location.Y - this[i].Location.Y), 2D)));
                }
            }


 public void DetermineFitness(Cities cities)
        {
            Fitness = 0;

            int lastCity = 0;
            int nextCity = this[0].Connection1;

            foreach (Link link in this)
            {
                Fitness += cities[lastCity].Distances[nextCity];

                // figure out if the next city in the list is [0] or [1]
                if (lastCity != this[nextCity].Connection1)
                {
                    lastCity = nextCity;
                    nextCity = this[nextCity].Connection1;
                }
                else
                {
                    lastCity = nextCity;
                    nextCity = this[nextCity].Connection2;
                }
            }
        }

Thanks for reading my post.
Posted
Updated 13-Aug-13 5:02am
v2

This is not how threading works. It's not "changing method". You need to create or obtain a thread and call your method from the thread. The thread will work when you start it. But the major concern is the thread synchronization, in particular, sharing objects between threads. This should be reduced to necessary minimum, but in most cases unavoidable. It includes the lock statement, thread synchronization objects ("primitives"), invocation delegates to the UI thread (Invoke and BegingInvoke methods of System.Windows.Threading.Dispatcher or System.Windows.Forms.Control and more. I think you need to learn it all by yourself, as it would be a bit too much for a single Quick Answer.

Please see System.Threading.Thread:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].

To create or obtain an additional thread, you can
  1. Create a thread instance with its constructor:
    http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^].

    I want to discourage using ParametrizedThreadStart as it involves type cast. Much better approach involves creating a thread wrapper, where "this" parameter is implicitly used to access the whole wrapper instance. It also allows to transparently encapsulate thread synchronization and more. Please see my past answers:
    How to pass ref parameter to the thread[^],
    Change parameters of thread (producer) after it is started[^],
    MultiThreading in C#[^].
  2. You can obtain a thread from the thread pool. Please see:
    http://en.wikipedia.org/wiki/Thread_pool_pattern[^],
    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^].
  3. Also, you can use the class System.ComponentModel.BackgroundWorker:
    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]


As the alternative to threading, you can consider using .NET Parallel Extensions, which is based on threading, but threads are not used explicitly and the solutions may appear single-threaded, transparently.

Please see:
http://en.wikipedia.org/wiki/Parallel_Extensions[^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[^].

See also my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].



—SA
 
Share this answer
 
Problem solved .

The problem was the migration from one version to another.
 
Share this answer
 
Comments
Richard Deeming 11-Mar-15 15:00pm    
And it took you 19 months to solve it?!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900