Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-threaded Simulated Annealing
    primarykey
    data
    text
    <p>I wrote a multithreaded simulated annealing program but its not running. I am not sure if the code is correct or not. The code is able to compile but when i run the code it crashes. Its just a run time error.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;time.h&gt; #include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;math.h&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;fstream&gt; #include &lt;ctime&gt; #include &lt;windows.h&gt; #include &lt;process.h&gt; using namespace std; typedef vector&lt;double&gt; Layer; //defines a vector type typedef struct { Layer Solution1; double temp1; double coolingrate1; int MCL1; int prob1; }t; //void SA(Layer Solution, double temp, double coolingrate, int MCL, int prob){ double Rand_NormalDistri(double mean, double stddev) { //Random Number from Normal Distribution static double n2 = 0.0; static int n2_cached = 0; if (!n2_cached) { // choose a point x,y in the unit circle uniformly at random double x, y, r; do { // scale two random integers to doubles between -1 and 1 x = 2.0*rand()/RAND_MAX - 1; y = 2.0*rand()/RAND_MAX - 1; r = x*x + y*y; } while (r == 0.0 || r &gt; 1.0); { // Apply Box-Muller transform on x, y double d = sqrt(-2.0*log(r)/r); double n1 = x*d; n2 = y*d; // scale and translate to get desired mean and standard deviation double result = n1*stddev + mean; n2_cached = 1; return result; } } else { n2_cached = 0; return n2*stddev + mean; } } double FitnessFunc(Layer x, int ProbNum) { int i,j,k; double z; double fit = 0; double sumSCH; if(ProbNum==1){ // Ellipsoidal function for(j=0;j&lt; x.size();j++) fit+=((j+1)*(x[j]*x[j])); } else if(ProbNum==2){ // Schwefel's function for(j=0; j&lt; x.size(); j++) { sumSCH=0; for(i=0; i&lt;j; i++) sumSCH += x[i]; fit += sumSCH * sumSCH; } } else if(ProbNum==3){ // Rosenbrock's function for(j=0; j&lt; x.size()-1; j++) fit += 100.0*(x[j]*x[j] - x[j+1])*(x[j]*x[j] - x[j+1]) + (x[j]-1.0)*(x[j]-1.0); } return fit; } double probl(double energychange, double temp){ double a; a= (-energychange)/temp; return double(min(1.0,exp(a))); } int random (int min, int max){ int n = max - min + 1; int remainder = RAND_MAX % n; int x; do{ x = rand(); }while (x &gt;= RAND_MAX - remainder); return min + x % n; } //void SA(Layer Solution, double temp, double coolingrate, int MCL, int prob){ void SA(void *param){ t *args = (t*) param; Layer Solution = args-&gt;Solution1; double temp = args-&gt;temp1; double coolingrate = args-&gt;coolingrate1; int MCL = args-&gt;MCL1; int prob = args-&gt;prob1; double Energy; double EnergyNew; double EnergyChange; Layer SolutionNew(50); Energy = FitnessFunc(Solution, prob); while (temp &gt; 0.01){ for ( int i = 0; i &lt; MCL; i++){ for (int j = 0 ; j &lt; SolutionNew.size(); j++){ SolutionNew[j] = Rand_NormalDistri(5, 1); } EnergyNew = FitnessFunc(SolutionNew, prob); EnergyChange = EnergyNew - Energy; if(EnergyChange &lt;= 0){ Solution = SolutionNew; Energy = EnergyNew; } if(probl(EnergyChange ,temp ) &gt; random(0,1)){ //cout&lt;&lt;SolutionNew[i]&lt;&lt;endl; Solution = SolutionNew; Energy = EnergyNew; cout &lt;&lt; temp &lt;&lt; "=" &lt;&lt; Energy &lt;&lt; endl; } } temp = temp * coolingrate; } } int main () { srand ( time(NULL) ); //seed for getting different numbers each time the prog is run Layer SearchSpace(50); //declare a vector of 20 dimensions //for(int a = 0;a &lt; 10; a++){ for (int i = 0 ; i &lt; SearchSpace.size(); i++){ SearchSpace[i] = Rand_NormalDistri(5, 1); } t *arg1; arg1 = (t *)malloc(sizeof(t)); arg1-&gt;Solution1 = SearchSpace; arg1-&gt;temp1 = 1000; arg1-&gt;coolingrate1 = 0.01; arg1-&gt;MCL1 = 100; arg1-&gt;prob1 = 3; //cout &lt;&lt; "Test " &lt;&lt; ""&lt;&lt;endl; _beginthread( SA, 0, (void*) arg1); Sleep( 100 ); //SA(SearchSpace, 1000, 0.01, 100, 3); //} return 0; } </code></pre> <p>Please help.</p> <p>Thanks Avinesh</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload