8 Replies - 438 Views - Last Post: 30 April 2011 - 05:02 AM Rate Topic: -----

#1 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:17 AM

Thank you, Guys,

The code below is returning a zero value for the calculation of distance of selected route(array). I have been trying to debug it but to no avail. Additionally, I will like points or cities modeled as array, to be randomly generated from mouse clicks on a map in picBox. Can anyone please help or give some hints?


namespace TSA
{
    class Route
    {
        ArrayList Cities = new ArrayList();
        double travelCost;
        int dist;

      public  double RunGA(int numCities, int[,] distancematrix)
        {
            for (int i = 0; i < distancematrix.Length; i++) 
            {
                for (int j = 0; j < distancematrix.Length; j++)
                {
                    Cities.Add(distancematrix);
                }
                }
           // distancematrix = new int[numCities,numCities];
            int dX = 0;
            int dY = 0;
            
            for (int i = 0; i < numCities; i++)
            {
                for (int j = i; j < numCities; j++)
                {
                    if (i == j)
                        distancematrix[i, j] = 0;
                    else
                    {
                        dX = Math.Abs(((Point)Cities[i]).X - ((Point)Cities[j]).X);
                        dY = Math.Abs(((Point)Cities[i]).Y - ((Point)Cities[j]).Y);
                        dist = (int)(Math.Sqrt(dX * dX + dY * dY));
                        distancematrix[i, j] = dist;
                        distancematrix[j, i] = dist;
                    }

                }

            }
            travelCost = (double)dist;
            return travelCost;
        }
        
    }
    
}
namespace TSA
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRunGA_Click(object sender, EventArgs e)
        {
            
            int numcities = 6;
            Route r = new Route();
            double cost=r.RunGA(numcities, new int[,] {
				{0, 20, 25, 15, 45, 32 },
				{20, 0, 35, 17, 28, 39 },
				{25, 35, 0, 45, 35, 22 },
				{15, 17, 45, 0, 15, 42 },
				{45, 28, 35, 15, 0, 52 },
				{32, 39, 22, 42, 52, 0 }
			});
            MessageBox.Show("Cost for this Rout is:  " +"" + cost.ToString());
    
        
        }
    }
}



Thank you.

nanosystems

MOD EDIT: Fixed code tags. The RIGHT way to use code tags:

:code:

This post has been edited by JackOfAllTrades: 30 April 2011 - 05:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:24 AM

Please fix your code tags

:code:
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:30 AM

Right off the bat this seems screwy.

ArrayList Cities = new ArrayList();

public double RunGA(int numCities, int[,] distancematrix)
{
   for (int i = 0; i < distancematrix.Length; i++) 
   {
      for (int j = 0; j < distancematrix.Length; j++)
      {
         Cities.Add(distancematrix);
      }
   }


You add the entire matrix to an arraylist, every time you loop through both dimensions of the matrix?

So of the matrix is 10x10 you will add the entire matrix to the ArrayList 100 times?

I'm guessing you are not familiar with breakpoints or looking at your variables' values in the Locals and Autos pallet.

You need to become comfortable with placing a breakpoint at the start of a method and walking through the entire method with the F10 key, looking at the values. This is the start of how to debug your application.


Debugging tutorial
Debugging tips
Great debugging tips
Was This Post Helpful? 0
  • +
  • -

#4 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:31 AM

View PosttlhIn`toq, on 30 April 2011 - 04:24 AM, said:

Please fix your code tags

:code:


Thank you, Guys,

The code below is returning a zero value for the calculation of distance of selected route(array). I have been trying to debug it but to no avail. Additionally, I will like points or cities modeled as array, to be randomly generated from mouse clicks on a map in picBox. Can anyone please help or give some hints?


namespace TSA
{
class Route
{
ArrayList Cities = new ArrayList();
double travelCost;
int dist;

public double RunGA(int numCities, int[,] distancematrix)
{
for (int i = 0; i < distancematrix.Length; i++) 
{
for (int j = 0; j < distancematrix.Length; j++)
{
Cities.Add(distancematrix);
}
}
// distancematrix = new int[numCities,numCities];
int dX = 0;
int dY = 0;

for (int i = 0; i < numCities; i++)
{
for (int j = i; j < numCities; j++)
{
if (i == j)
distancematrix[i, j] = 0;
else
{
dX = Math.Abs(((Point)Cities[i]).X - ((Point)Cities[j]).X);
dY = Math.Abs(((Point)Cities[i]).Y - ((Point)Cities[j]).Y);
dist = (int)(Math.Sqrt(dX * dX + dY * dY));
distancematrix[i, j] = dist;
distancematrix[j, i] = dist;
}

}

}
travelCost = (double)dist;
return travelCost;
}

}

}
namespace TSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRunGA_Click(object sender, EventArgs e)
{

int numcities = 6;
Route r = new Route();
double cost=r.RunGA(numcities, new int[,] {
{0, 20, 25, 15, 45, 32 },
{20, 0, 35, 17, 28, 39 },
{25, 35, 0, 45, 35, 22 },
{15, 17, 45, 0, 15, 42 },
{45, 28, 35, 15, 0, 52 },
{32, 39, 22, 42, 52, 0 }
});
MessageBox.Show("Cost for this Rout is: " +"" + cost.ToString());


}
}
}




Thank you.

nanosystems
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:41 AM

I don't mind helping someone with logic problems, but your code doesn't even complete. You need to at least do your own basic debugging. We're not here to clean up everything for you.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:48 AM

I'm really pretty sure you didn't mean to have nearly 1300 cities. Did you?

Attached Image

Work on cleaning up everything around your calculation before worrying about the calculation itself. This code has many issues that need attention.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:54 AM

And the distance between your cities will always be zero if all of your cities are at the same location. All 1296 cities are at location 6,6

Attached Image


This is all basic debugging where you look at the values of your objects and see if they actually are what you expected them to be.

I think those tutorials I linked to you will help you a lot if you really work them and not just quick skim them.
Was This Post Helpful? 1
  • +
  • -

#8 nanosystems  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 04:57 AM

Thank you, I have been able to correct the appropriate number of cities(36), but how do I get the right cast for the Point Class, and how do I randomly select the cities from a map on picBox? Any hint or help, please?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: Select Points on Map in PicBox Randomly and Calculate Dist Per Route

Posted 30 April 2011 - 05:02 AM

Let's see your updated code please.
Let's also see the values for each city. I don't want to waste my time or yours if they are all still 6,6.

You are so far away from having this working that you really don't need to worry about random selection yet.

Don't try to cast to a point. Make a new point using the values you have

point tempPoint = new Point(x, y);

then work with that point.

Don't worry about condensing everything to the least number of lines right now.

First make it work. Use lots of lines that have one function each so you can confirm each works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1