Example #1
0
        private void Gs_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            AddPointsToChart(e.IterationCount, e.BestChromosome.Fitness, e.AverageFitness);
            Decode(e.BestChromosome);

            PopulationChart.Refresh();
            Application.DoEvents();
        }
Example #2
0
        private static void G_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            var chromosome = e.BestChromosome;
            var v1         = (int)chromosome.Data[0];
            var v2         = (int)chromosome.Data[1];
            var v3         = (int)chromosome.Data[2];
            var v4         = (int)chromosome.Data[3];

            Console.WriteLine($"{e.BestChromosome.Fitness} {e.AverageFitness} Iteration completed | {v1} {v2} {v3} {v4} | {v1 + v2 + v3 + v4}");
        }
Example #3
0
        private void Gs_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            BestSolution = e.BestChromosome;
            AddPointsToChart(e.IterationCount, BestSolution.Fitness, e.AverageFitness);

            label1.Text = $"Total Length : {(int)CalculateFitness_TSPProblem(BestSolution)}";

            map.Refresh();
            PopulationChart.Refresh();

            Application.DoEvents();
        }
Example #4
0
        private void reactionDiffusionControl_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            TimeSpan frameTime = DateTime.Now - this._startFrameTime;

            this.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                new DispatcherOperationCallback(
                    delegate(object param)
            {
                this.textBlock6.Text = param as string;

                return(null);
            }), frameTime.TotalMilliseconds.ToString());

            this._startFrameTime = DateTime.Now;
        }
Example #5
0
        private void Gs_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            var best_chromosome = e.BestChromosome;

            queen_positions = new List <Point>();

            foreach (var pt in best_chromosome.Data)
            {
                queen_positions.Add((Point)pt);
            }

            AddPointsToChart(e.IterationCount, best_chromosome.Fitness, e.AverageFitness);

            Application.DoEvents();
            Chessboard.Refresh();
        }
        private void reactionDiffusionControl_IterationCompleted( object sender, IterationCompletedEventArgs e )
        {
            TimeSpan frameTime = DateTime.Now - this._startFrameTime;

            this.Dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new DispatcherOperationCallback(
                        delegate( object param )
                        {
                            this.textBlock6.Text = param as string;

                            return null;

                        } ), frameTime.TotalMilliseconds.ToString() );

            this._startFrameTime = DateTime.Now;
        }
Example #7
0
        private void Gs_IterationCompleted(object sender, IterationCompletedEventArgs e)
        {
            resultlist.Items.Add("= Iteration Completed =========");

            int val1  = (int)e.BestChromosome.Data[0];
            int val2  = (int)e.BestChromosome.Data[1];
            int val3  = (int)e.BestChromosome.Data[2];
            int val4  = (int)e.BestChromosome.Data[3];
            int val5  = (int)e.BestChromosome.Data[4];
            int val6  = (int)e.BestChromosome.Data[5];
            int val7  = (int)e.BestChromosome.Data[6];
            int val8  = (int)e.BestChromosome.Data[7];
            int val9  = (int)e.BestChromosome.Data[8];
            int val10 = (int)e.BestChromosome.Data[9];

            resultlist.Items.Add($"{val1} + {val2} + {val3} + {val4} + {val5} = {val1 + val2 + val3 + val4 + val5}");
            resultlist.Items.Add($"{val6} * {val7} * {val8} * {val9} * {val10} = {val6 * val7 * val8 * val9 * val10}");

            AddPointsToChart(e.IterationCount, e.BestChromosome.Fitness, e.AverageFitness);

            PopulationChart.Refresh();
            Application.DoEvents();
        }
        /// <summary>
        /// Raises the InterationCompleted event.
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnInterationCompleted(IterationCompletedEventArgs e)
        {
            Trace.Assert(e != null);

            if (IterationCompleted != null)
            {
                IterationCompleted(this, e);
            }
        }
        /// <summary>
        /// Computes one frame of the reaction.
        /// </summary>
        /// <returns>An array containing the reaction products for the next frame. </returns>
        public unsafe double[, ,] ComputeReaction()
        {
            Debug.Assert(CheckInvariant(), "ComputeReaction: CheckInvariant failed");

            double Xa;
            double Xb;
            double Xc;
            double c;
            double d;
            double f;
            double g0;
            double feedbackFromC;
            double feedbackFromB;

            iteration++;

            // If boundary conditions have changed since the previous frame, update them.
            if (this._boundaryConditionsPrev != this._boundaryConditions)
            {
                this.SetBoundaryConditions(this._boundaryConditions);

                this._boundaryConditionsPrev = this._boundaryConditions;
            }

            // Copy boundary values to the out buffer.
            for (int i = 0; i < vesselWidth; i++)
            {
                reactionOut[indexA, i, 0] = reaction[indexA, i, 0];
                reactionOut[indexB, i, 0] = reaction[indexB, i, 0];
                reactionOut[indexC, i, 0] = reaction[indexC, i, 0];

                reactionOut[indexA, i, vesselHeight - 1] = reaction[indexA, i, vesselHeight - 1];
                reactionOut[indexB, i, vesselHeight - 1] = reaction[indexB, i, vesselHeight - 1];
                reactionOut[indexC, i, vesselHeight - 1] = reaction[indexC, i, vesselHeight - 1];
            }

            for (int j = 0; j < vesselHeight; j++)
            {
                reactionOut[indexA, 0, j] = reaction[indexA, 0, j];
                reactionOut[indexB, 0, j] = reaction[indexB, 0, j];
                reactionOut[indexC, 0, j] = reaction[indexC, 0, j];

                reactionOut[indexA, vesselWidth - 1, j] = reaction[indexA, vesselWidth - 1, j];
                reactionOut[indexB, vesselWidth - 1, j] = reaction[indexB, vesselWidth - 1, j];
                reactionOut[indexC, vesselWidth - 1, j] = reaction[indexC, vesselWidth - 1, j];
            }

            // Traverse the grid and compute concentrations for
            // all three reactants. This implements the Gontar model.
            for (int i = 1; i < vesselWidth - 1; i++)
            {
                for (int j = 1; j < vesselHeight - 1; j++)
                {
                    try
                    {
                        // Compute concentrations at cell (i,j).
                        feedbackFromC = Math.Exp( -W1 / weight( indexC, i, j ) );
                        feedbackFromB = Math.Exp( -W2 * weight( indexB, i, j ) );
                        g0 = ( K1 * K2 * feedbackFromC * feedbackFromB ) / ( 1.0 + K1 * feedbackFromC );

                        // Compute concentrations at cell (i,j).
                        //c = -W1 / weight(2, i, j);
                        //f = -W2 * weight(1, i, j);
                        //e_to_c = Math.Exp(c);
                        //e_to_f = Math.Exp(f);
                        //d = 1.0 + K1 * e_to_c;
                        //g0 = (K1 * K2 * e_to_c * e_to_f) / d;

                        Xc = b * (g0 / (g0 + 1));
                        if (Xc <= minConcentration) Xc = minConcentration;

                        Xb = (K1 * feedbackFromC / (1 + K1 * feedbackFromC)) * (b - Xc);
                        if (Xb <= minConcentration) Xb = minConcentration;

                        Xa = b - Xb - Xc;
                        if (Xa <= minConcentration) Xa = minConcentration;

                        if (Xa > b) Xa = b;

                        if (Xb > b) Xb = b;

                        if (Xc > b) Xc = b;

                        reactionOut[indexA, i, j] = Xa;
                        reactionOut[indexB, i, j] = Xb;
                        reactionOut[indexC, i, j] = Xc;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("caught exception: ", ex.ToString());
                    }

                }
            }

            int ubi = reaction.GetUpperBound(1) + 1;
            int ubj = reaction.GetUpperBound(2) + 1;

            // Copy the out buffer to the in buffer.
            fixed (double* pOutBuff = reactionOut, pInBuff = reaction)
            {
                uint buffSize = (uint)(3 * ubi * ubj * sizeof(double));

                uint count = buffSize;

                double* ps = pOutBuff;
                double* pd = pInBuff;

                lock (reactionOut.SyncRoot)
                {
                    try
                    {
                        CopyMemory((void*)pd, (void*)ps, count);
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine(ex.Message);
                    }
                }
            }

            // Raise the IterationCompleted event.
            IterationCompletedEventArgs e = new IterationCompletedEventArgs(this.iteration);
            OnInterationCompleted(e);

            return reactionOut;
        }
Example #10
0
 private static void G_IterationCompleted(object sender, IterationCompletedEventArgs e)
 {
     Decode(e.BestChromosome);
 }
Example #11
0
        /// <summary>
        ///     Run the k-means clustering algorithm for a specified amount of iterations.
        /// </summary>
        /// <param name="iterations">Iterations to run.</param>
        /// <param name="allowEmptyClusters">True to allow the optimization to leave clusters empty.</param>
        public void Run(int iterations, bool allowEmptyClusters)
        {
            var  rnd = new Random();
            bool hasChanged;
            var  iteration = 0;

            do
            {
                hasChanged = false;

                // Compute cluster averages
                var averages = new List <VectorNd>(this.Clusters.Count);
                this.Clusters.ForEach(cluster => averages.Add(cluster.Average()));

                // Create placeholder clusters for next iteration
                var newClusters = new List <KMeansCluster>(this.Clusters.Count);
                for (var i = 0; i < this.Clusters.Count; i++)
                {
                    newClusters.Add(new KMeansCluster());
                }

                // Find the closest average for each vector in each cluster
                this.Clusters.ForEach(
                    cluster =>
                {
                    var ind = this.Clusters.IndexOf(cluster);
                    for (var i = 0; i < cluster.Count; i++)
                    {
                        var vector   = cluster[i];
                        var simIndex = this.FindIndexOfSimilar(averages, vector);
                        newClusters[simIndex].Add(vector);
                        if (simIndex != ind)
                        {
                            hasChanged = true;
                        }
                    }
                });

                // Check for empty clusters
                if (!allowEmptyClusters)
                {
                    newClusters.ForEach(
                        cluster =>
                    {
                        if (cluster.Count == 0)
                        {
                            Console.WriteLine("Cluster has no mass");
                            var biggest = newClusters.OrderByDescending(x => x.Count)
                                          .First();

                            var randomVector = biggest[rnd.Next(biggest.Count)];

                            biggest.Remove(randomVector);
                            cluster.Add(randomVector);
                        }
                    });
                }

                // Update clusters and increase iteration
                this.Clusters = newClusters;
                var iterArgs = new IterationCompletedEventArgs
                {
                    Iteration = iteration, Clusters = newClusters
                };
                this.OnIterationCompleted(iterArgs);
                iteration++;
                this.currentIterations++;
            } while (hasChanged &&
                     iteration < iterations &&
                     this.currentIterations < this.maxIterations);
        }
Example #12
0
 /// <summary>
 ///     Method to call when an iteration is completed.
 /// </summary>
 /// <param name="iterArgs">Data for the current iteration.</param>
 protected virtual void OnIterationCompleted(IterationCompletedEventArgs iterArgs) =>
 this.IterationCompleted?.Invoke(this, iterArgs);