private void SetupParallel(List <City> cities, List <Link> links)
        {
            D = InitializeWeightParallel(cities, links);
            P = new City[cities.Count, cities.Count];

            for (var k = 0; k < cities.Count; k++)
            {
                Parallel.For(0, cities.Count, i =>
                {
                    for (var j = 0; j < cities.Count; j++)
                    {
                        if (D[i, k] != Double.MaxValue && D[k, j] != Double.MaxValue &&
                            D[i, k] + D[k, j] < D[i, j])
                        {
                            //1) here i!=k && j!=k holds
                            // => for all x: D[x,k] and D[k,x] are both unchanged during this iteration of k
                            // => every D[x,k], D[k,x] is guaranteed to be a valid value (valid meaning: there are no cached but not written values from other threads, which are not visible)
                            //2) D[i,j] is valid (same def of valid as above) because in this iteration of k the pair (i,j) is only processed by one thread
                            //3) Because of 2 it is also safe to write D[i,j], P[i,j]
                            D[i, j] = D[i, k] + D[k, j];
                            P[i, j] = cities[k];
                        }
                    }
                });
            }
        }
Beispiel #2
0
 public City[,] makeMap(Country[] countries)
 {
     City[,] grid = makeGrid(ref countries);
     fillByNull(ref grid);
     buildCityies(ref grid, ref countries);
     return(grid);
 }
        private void Setup(List <City> cities, List <Link> links)
        {
            if (ExecuteParallel)
            {
                SetupParallel(cities, links);
            }
            else
            {
                D = InitializeWeight(cities, links);
                P = new City[cities.Count, cities.Count];

                for (var k = 0; k < cities.Count; k++)
                {
                    for (var i = 0; i < cities.Count; i++)
                    {
                        for (var j = 0; j < cities.Count; j++)
                        {
                            if (D[i, k] != Double.MaxValue && D[k, j] != Double.MaxValue &&
                                D[i, k] + D[k, j] < D[i, j])
                            {
                                D[i, j] = D[i, k] + D[k, j];
                                P[i, j] = cities[k];
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
 private void fillByNull(ref City[,] grid)
 {
     for (int i = 0; i < grid.GetLength(0); i++)
     {
         for (int j = 0; j < grid.GetLength(1); j++)
         {
             grid[i, j] = null;
         }
     }
 }
Beispiel #5
0
 public void runCase()
 {
     if (countries.Length != 1)
     {
         day = 0;
         MapMaker maker = new MapMaker();
         grid = maker.makeMap(countries);
         MoneyExchangeSimulator simulator = new MoneyExchangeSimulator();
         simulator.simulate(ref grid);
     }
     else
     {
         day = 1;
     }
 }
Beispiel #6
0
 private void buildCityies(ref City[,] grid, ref Country[] countries)
 {
     for (int i = 0; i < countries.GetLength(0); i++)
     {
         for (int j = countries[i].getX_L(); j <= countries[i].getX_H(); j++)
         {
             for (int k = countries[i].getY_L(); k <= countries[i].getY_H(); k++)
             {
                 int numOfMotif  = countries.Length;
                 int x_positon   = j;
                 int y_position  = k;
                 int countryIndx = i;
                 grid[j, k] = new City(x_positon, y_position, countryIndx, numOfMotif);
             }
         }
     }
 }
        /// <summary>
        /// defining an array of cities
        /// </summary>
        private void InitArrayOfCity()
        {
            int maxX = 0, maxY = 0;

            foreach (Country item in this.Countries)
            {
                if (item.Xh > maxX)
                {
                    maxX = item.Xh;
                }
                if (item.Yh > maxY)
                {
                    maxY = item.Yh;
                }
            }
            this.cities = new City[maxX + 1, maxY + 1];
        }
        private void Setup(List <City> cities, List <Link> links)
        {
            D = InitializeWeight(cities, links);
            P = new City[cities.Count, cities.Count];

            for (int k = 0; k < cities.Count; k++)
            {
                for (int i = 0; i < cities.Count; i++)
                {
                    for (int j = 0; j < cities.Count; j++)
                    {
                        if (D[i, k] != Double.MaxValue &&
                            D[k, j] != Double.MaxValue &&
                            D[i, k] + D[k, j] < D[i, j])
                        {
                            D[i, j] = D[i, k] + D[k, j];
                            P[i, j] = cities[k];
                        }
                    }
                }
            }
        }
        private void SetupParallel(List <City> cities, List <Link> links)
        {
            D = InitializeWeightParallel(cities, links);
            P = new City[cities.Count, cities.Count];

            Parallel.For(0, cities.Count, k =>
            {
                for (var i = 0; i < cities.Count; i++)
                {
                    for (var j = 0; j < cities.Count; j++)
                    {
                        if (D[i, k] != Double.MaxValue &&
                            D[k, j] != Double.MaxValue &&
                            D[i, k] + D[k, j] < D[i, j])
                        {
                            D[i, j] = D[i, k] + D[k, j];
                            P[i, j] = cities[k];
                        }
                    }
                }
            });
        }
        public City[] EvolutionaryHillClimber(
            int N, int maxGenerations, int population,
            City[] city, out double minFitness,
            ref City[,] chromosome, ref double[] fitness)
        {
            City[] best  = new City[N];
            City[] child = null;

            minFitness = int.MaxValue;

            if (fitness == null)
            {
                fitness    = new double[population];
                chromosome = new City[population, N];

                // calculate initial population and fitnesses

                for (int i = 0; i < population; i++)
                {
                    InitializeCityArray(N, city, out child);

                    for (int j = 0; j < N; j++)
                    {
                        chromosome[i, j] = child[j];
                    }

                    fitness[i] = TourLength(N, child);

                    if (fitness[i] < minFitness)
                    {
                        minFitness = fitness[i];

                        for (int j = 0; j < N; j++)
                        {
                            best[j] = child[j];
                        }
                    }
                }
            }

            else
            {
                for (int i = 0; i < population / 2; i++)
                {
                    InitializeCityArray(N, city, out child);

                    for (int j = 0; j < N; j++)
                    {
                        chromosome[i, j] = child[j];
                    }

                    fitness[i] = TourLength(N, child);
                }

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] < minFitness)
                    {
                        minFitness = fitness[i];

                        for (int j = 0; j < N; j++)
                        {
                            best[j] = chromosome[i, j];
                        }
                    }
                }
            }

            for (int g = 0; g < maxGenerations; g++)
            {
                // tournament selection with a tounament size of 2

                int parent1 = random.Next(population);
                int parent2 = random.Next(population);
                int parent  = fitness[parent1] < fitness[parent2] ? parent1 : parent2;

                for (int i = 0; i < N; i++)
                {
                    child[i] = chromosome[parent, i];
                }

                // mutate the child

                Swap(N, ref child);

                // replace maximum fitness with child

                double childFitness = TourLength(N, child);
                double maxFitness   = int.MinValue;

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] > maxFitness)
                    {
                        maxFitness = fitness[i];
                    }
                }

                List <int> indices = new List <int>();

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] == maxFitness)
                    {
                        indices.Add(i);
                    }
                }

                int index = indices[random.Next(indices.Count)];

                for (int i = 0; i < N; i++)
                {
                    chromosome[index, i] = child[i];
                }

                fitness[index] = childFitness;

                if (childFitness < minFitness)
                {
                    minFitness = childFitness;

                    for (int i = 0; i < N; i++)
                    {
                        best[i] = child[i];
                    }
                }
            }

            return(best);
        }
        private void SetupParallel(List<City> cities, List<Link> links)
        {
            D = InitializeWeightParallel(cities, links);
            P = new City[cities.Count, cities.Count];

            for (var k = 0; k < cities.Count; k++)
                for (var i = 0; i < cities.Count; i++)
                    for (var j = 0; j < cities.Count; j++)
                        if (D[i, k] != Double.MaxValue
                         && D[k, j] != Double.MaxValue
                         && D[i, k] + D[k, j] < D[i, j])
                        {
                            D[i, j] = D[i, k] + D[k, j];
                            P[i, j] = cities[k];
                        }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            button2.Enabled = false;
            button3.Enabled = false;

            if (randomTSP)
            {
                City[] initial = algos.GenerateRandomInitial(N, seed);

                lCity = new List <City>(initial);
            }

            bool   saga = false;
            double fS = 0, bestfS = double.MaxValue;

            double[] fitness  = null;
            int      method   = comboBox1.SelectedIndex;
            int      restarts = (int)numericUpDown1.Value;

            City[] best, result;
            City[,] chromosome = null;
            List <City> S  = new List <City>(lCity);
            List <City> SS = null;

            textBox1.Text += N.ToString() + "\r\n";
            textBox1.Text += (string)comboBox1.SelectedItem + "\r\n";

            switch (method)
            {
            case 0:
                for (int i = 0; i < 40000 * restarts; i++)
                {
                    SS = algos.SDLS(N, S, out fS);
                    S  = new List <City>(SS);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 1:
                for (int i = 0; i < restarts; i++)
                {
                    SS = algos.TabuSearch(N, 1000 * N, S, out fS);
                    S  = new List <City>(SS);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 2:
                for (int i = 0; i < restarts; i++)
                {
                    result = algos.EvolutionaryHillClimber(N, 10000 * N, 100 * N,
                                                           S.ToArray(), out fS, ref chromosome, ref fitness);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 3:
                for (int i = 0; i < restarts; i++)
                {
                    result = algos.GeneticAlgortihm(0.10, 1.00, N, 10000 * N, 100 * N,
                                                    S.ToArray(), out fS, ref chromosome, ref fitness);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 4:
                int count1, count2, count3, count4;

                for (int i = 0; i < restarts; i++)
                {
                    algos.SimulatedAnnealing(N, 10000, 10000 * N, N, S.ToArray(),
                                             out result, out fS, out count1, out count2, out count3, out count4);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                if (saga)
                {
                    goto case 3;
                }

                break;

            case 5:
                saga = true;
                goto case 4;

            case 6:
                for (int i = 0; i < restarts; i++)
                {
                    algos.SimulatedAnnealing(N, 10000, 100000, 20, S.ToArray(),
                                             out result, out fS, out count1, out count2, out count3, out count4);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }

                    result = algos.GeneticAlgortihm(0.10, 1.00, N, 10000 * N, 100 * N,
                                                    S.ToArray(), out fS, ref chromosome, ref fitness);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 7:
                for (int i = 0; i < restarts; i++)
                {
                    algos.SASDLS(N, 10000, 5000, 10, S.ToArray(),
                                 out result, out fS, out count1, out count2, out count3, out count4);
                    S = new List <City>(result);

                    if (fS < bestfS)
                    {
                        bestfS = fS;
                        best   = S.ToArray();
                    }
                }

                break;

            case 8:
                if (randomTSP)
                {
                    best = algos.BruteForce(N, lCity.ToArray(), out bestfS);
                }

                else
                {
                    MessageBox.Show("Random must be checked", "Warning Message",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                break;
            }

            textBox1.Text += bestfS.ToString("F2") + "\r\n";
            sw.Stop();
            PrintTime(sw);

            if (!randomTSP)
            {
                button1.Enabled = true;
            }

            button2.Enabled = true;
            button3.Enabled = true;
        }
Beispiel #13
0
 public CityMap(Tilemap tm)
 {
     cmap=new City[tm.NumX, tm.NumY];
 }
Beispiel #14
0
 public CityMap(int w, int h)
 {
     cmap=new City[w,h];
 }
Beispiel #15
0
        public void load(String path)
        {
            if (!VersionSys.match(path, vi))
                throw new Exception("File is not a Forgotten Schism Citymap file v1.0");

            FileStream fin = new FileStream(path, FileMode.Open);

            fin.Seek(12, SeekOrigin.Begin);

            String[] ownls = Gen.rstra(fin);

            int w=fin.ReadByte();
            int h=fin.ReadByte();

            cmap=new City[w, h];

            int x;
            int y;
            String n;
            int on;
            int s;
            int f;

            while(fin.Position<fin.Length-1)
            {
                x = fin.ReadByte();
                y = fin.ReadByte();
                n = Gen.rstr(fin);
                on = fin.ReadByte();
                s = fin.ReadByte();
                f = fin.ReadByte();

                if(on==0)
                    cmap[x, y] = new City(n, (City.CitySide)s, "", f);
                else
                    cmap[x, y] = new City(n, (City.CitySide)s, ownls[on - 1], f);
            }

            fin.Close();
        }
        public City[] GeneticAlgortihm(
            double crate, double mrate,
            int N, int maxGenerations, int population,
            City[] city, out double minFitness,
            ref City[,] chromosome, ref double[] fitness)
        {
            City[] best  = new City[N];
            City[] child = null;

            minFitness = double.MaxValue;

            if (fitness == null)
            {
                fitness    = new double[population];
                chromosome = new City[population, N];

                // calculate initial population and fitnesses

                for (int i = 0; i < population; i++)
                {
                    InitializeCityArray(N, city, out child);

                    for (int j = 0; j < N; j++)
                    {
                        chromosome[i, j] = child[j];
                    }

                    fitness[i] = TourLength(N, child);

                    if (fitness[i] < minFitness)
                    {
                        minFitness = fitness[i];

                        for (int j = 0; j < N; j++)
                        {
                            best[j] = child[j];
                        }
                    }
                }
            }

            else
            {
                for (int i = 0; i < population / 2; i++)
                {
                    InitializeCityArray(N, city, out child);

                    for (int j = 0; j < N; j++)
                    {
                        chromosome[i, j] = child[j];
                    }

                    fitness[i] = TourLength(N, child);
                }

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] < minFitness)
                    {
                        minFitness = fitness[i];

                        for (int j = 0; j < N; j++)
                        {
                            best[j] = chromosome[i, j];
                        }
                    }
                }
            }

            for (int g = 0; g < maxGenerations; g++)
            {
                bool cross = false, mutate = false;

                // tournament selection with a tounament size of 2

                int parent1 = random.Next(population);
                int parent2 = random.Next(population);
                int parentA = fitness[parent1] < fitness[parent2] ? parent1 : parent2;

                parent1 = random.Next(population);
                parent2 = random.Next(population);

                int parentB = fitness[parent1] < fitness[parent2] ? parent1 : parent2;

                if (random.NextDouble() < crate)
                {
                    // crossover

                    int   i;
                    int[] p1 = new int[N];
                    int[] p2 = new int[N];
                    int[] o1 = new int[N];
                    int[] o2 = new int[N];

                    for (i = 0; i < N; i++)
                    {
                        p1[i] = chromosome[parent1, i].Id;
                        p2[i] = chromosome[parent2, i].Id;
                    }

                    int cp1 = -1, cp2 = -1;

                    do
                    {
                        cp1 = random.Next(N);
                        cp2 = random.Next(N);
                    } while (cp1 == cp2 || cp1 > cp2);

                    PMX(N, cp1, cp2, p1, p2, o1, o2);

                    City[] o1City = new City[N];
                    City[] o2City = new City[N];

                    for (i = 0; i < N; i++)
                    {
                        o1City[i] = chromosome[parent1, o1[i]];
                        o2City[i] = chromosome[parent2, o2[i]];
                    }

                    double o1Fitness = TourLength(N, o1City);
                    double o2Fitness = TourLength(N, o2City);

                    if (o1Fitness < fitness[parent1])
                    {
                        for (i = 0; i < N; i++)
                        {
                            chromosome[parent1, i] = o1City[i];
                        }
                    }

                    if (o2Fitness < fitness[parent2])
                    {
                        for (i = 0; i < N; i++)
                        {
                            chromosome[parent2, i] = o2City[i];
                        }
                    }

                    for (int p = 0; p < population; p++)
                    {
                        if (fitness[p] < minFitness)
                        {
                            minFitness = fitness[p];

                            for (int n = 0; n < N; n++)
                            {
                                best[n] = chromosome[p, n];
                            }
                        }
                    }
                }

                else
                {
                    for (int i = 0; i < N; i++)
                    {
                        child[i] = chromosome[parentA, i];
                    }
                }

                if (random.NextDouble() < mrate)
                {
                    // mutate the child

                    Swap(N, ref child);
                    mutate = true;
                }

                if (!cross && !mutate)
                {
                    continue;
                }

                // replace maximum fitness with child

                double childFitness = TourLength(N, child);
                double maxFitness   = int.MinValue;

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] > maxFitness)
                    {
                        maxFitness = fitness[i];
                    }
                }

                List <int> indices = new List <int>();

                for (int i = 0; i < population; i++)
                {
                    if (fitness[i] == maxFitness)
                    {
                        indices.Add(i);
                    }
                }

                int index = indices[random.Next(indices.Count)];

                for (int i = 0; i < N; i++)
                {
                    chromosome[index, i] = child[i];
                }

                fitness[index] = childFitness;

                if (childFitness < minFitness)
                {
                    minFitness = childFitness;

                    for (int i = 0; i < N; i++)
                    {
                        best[i] = child[i];
                    }
                }
            }

            return(best);
        }
      private void Setup(List<City> cities, List<Link> links) {
	        
	        D = InitializeWeight(cities, links);
	        P = new City[cities.Count, cities.Count];

			for( int k=0; k<cities.Count; k++ )
			{
	            for( int i=0; i<cities.Count; i++ )  {
	                for( int j=0; j<cities.Count; j++ )  {
	                    if( D[i,k] != Double.MaxValue 
	                     && D[k,j] != Double.MaxValue 
	                     && D[i,k]+D[k,j] < D[i,j] )
	                    {
	                        D[i,j] = D[i,k]+D[k,j];
	                        P[i,j] = cities[k];
	                    }
	                }
	            }
			}

	    }
 public void simulate(ref City[,] grid)
 {
 }