void RainingProcess(SearchSpace *s, int *flow) { int i, j; double tmp, rand, dist; for (i = 1; i < s->nsr + 1; i++) { rand = GenerateUniformRandomNumber(0, 1); dist = EuclideanDistance(s->a[0]->x, s->a[i]->x, s->n); /* It obtains the euclidean distance for further use */ if ((dist < s->dmax) || (rand < 0.1)) { DestroyAgent(&(s->a[i]), _WCA_); s->a[i] = GenerateNewAgent(s, _WCA_); } } for (i = s->nsr + 1; i < flow[0]; i++) { rand = GenerateUniformRandomNumber(0, 1); dist = EuclideanDistance(s->a[0]->x, s->a[i]->x, s->n); /* It obtains the euclidean distance for further use */ if ((dist < s->dmax) || (rand < 0.1)) { DestroyAgent(&(s->a[i]), _WCA_); s->a[i] = GenerateNewAgent(s, _WCA_); } } }
void MutationAndRecombination(SearchSpace *s, int target, prtFun Evaluate, va_list arg) { Agent *mutant; int a, b, c, k; mutant = GenerateNewAgent(s, _DE_); a = b = c = target; while (a == target) a = GenerateUniformRandomNumber(0, 1) * s->m; while (b == target || b == a) b = GenerateUniformRandomNumber(0, 1) * s->m; while (c == target || c == b || c == a) c = GenerateUniformRandomNumber(0, 1) * s->m; for (k = 0; k < s->n; k++) { mutant->x[k] = GenerateUniformRandomNumber(0, 1) < s->cross_probability ? s->a[target]->x[k] : s->a[a]->x[k] + s->mutation_factor * (s->a[b]->x[k] - s->a[c]->x[k]); } CheckAgentLimits(s, mutant); mutant->fit = Evaluate(mutant, arg); if (mutant->fit < s->a[target]->fit) { DestroyAgent(&s->a[target], _DE_); s->a[target] = CopyAgent(mutant, _DE_, _NOTENSOR_); } DestroyAgent(&mutant, _DE_); }
/* It executes the Brain Storm Optimization for function minimization according to Algorithm 1 (El-Abd, 2017) Parameters: s: search space Evaluate: pointer to the function used to evaluate particles arg: list of additional arguments */ void runBSO(SearchSpace *s, prtFun Evaluate, ...) { va_list arg, argtmp; int i, j, z, k, t, *best = NULL, c1, c2, **ideas_per_cluster = NULL; double p, r; Agent *nidea = NULL; va_start(arg, Evaluate); va_copy(argtmp, arg); if (!s) { fprintf(stderr, "\nSearch space not allocated @runBSO.\n"); exit(-1); } ideas_per_cluster = (int **)malloc(s->k * sizeof(int *)); best = (int *)malloc(s->k * sizeof(int)); nidea = CreateAgent(s->n, _BSO_, _NOTENSOR_); EvaluateSearchSpace(s, _BSO_, Evaluate, arg); /* Initial evaluation */ for (t = 1; t <= s->iterations; t++) { fprintf(stderr, "\nRunning iteration %d/%d ... ", t, s->iterations); /* clustering ideas */ k_means(s, best, &ideas_per_cluster); /* for each idea */ for (i = 0; i < s->m; i++) { va_copy(arg, argtmp); p = GenerateUniformRandomNumber(0, 1); if (s->p_one_cluster > p) { c1 = (int)GenerateUniformRandomNumber(0, s->k); /* selecting a cluster probabilistically */ p = GenerateUniformRandomNumber(0, 1); /* creating a new idea based on the cluster selected previously. We also consider if cluster c1 has a single idea, i.e., ideas_per_cluster[c1][0] == 0. Notice we do not consider the cluster's center into that computation @kmeans function, which means a unitary cluster has ideas_per_cluster[c1][0] == 0. */ if ((s->p_one_center > p) || (ideas_per_cluster[c1][0] == 0)) { for (k = 0; k < s->n; k++) nidea->x[k] = s->a[best[c1]]->x[k]; } else { /* creating a new idea based on another idea j selected randomly from cluster c1 */ j = (int)GenerateUniformRandomNumber(1, ideas_per_cluster[c1][0]); j = ideas_per_cluster[c1][j]; for (k = 0; k < s->n; k++) nidea->x[k] = s->a[j]->x[k]; } } else { /* selecting two clusters' centers probabilistically */ c1 = (int)GenerateUniformRandomNumber(0, s->k); c2 = (int)GenerateUniformRandomNumber(0, s->k); /* selecting two ideas randomly */ if (ideas_per_cluster[c1][0] == 0) j = best[c1]; else { j = (int)GenerateUniformRandomNumber(1, ideas_per_cluster[c1][0]); j = ideas_per_cluster[c1][j]; } if (ideas_per_cluster[c2][0] == 0) z = best[c2]; else { z = (int)GenerateUniformRandomNumber(1, ideas_per_cluster[c2][0]); z = ideas_per_cluster[c2][z]; } p = GenerateUniformRandomNumber(0, 1); r = GenerateUniformRandomNumber(0, 1); /* it creates a new idea based on a random combination of two selected clusters' centers */ if (s->p_two_centers > p) { for (k = 0; k < s->n; k++) nidea->x[k] = r * s->a[best[c1]]->x[k] + (1 - r) * s->a[best[c2]]->x[k]; } else { /* it creates a new idea based on the ideas selected at random from the clusters previously chosen */ for (k = 0; k < s->n; k++) nidea->x[k] = r * s->a[j]->x[k] + (1 - r) * s->a[z]->x[k]; } } /* adding local noise to the new created idea */ p = (0.5 * s->iterations - t) / k; r = GenerateUniformRandomNumber(0, 1) * Logistic_Sigmoid(p); for (k = 0; k < s->n; k++) nidea->x[k] += r * randGaussian(0, 1); /* It evaluates the new created idea */ CheckAgentLimits(s, nidea); p = Evaluate(nidea, arg); if (p < s->a[i]->fit) { /* if the new idea is better than the current one */ for (k = 0; k < s->n; k++) s->a[i]->x[k] = nidea->x[k]; s->a[i]->fit = p; } if (s->a[i]->fit < s->gfit) s->gfit = s->a[i]->fit; } fprintf(stderr, "OK (minimum fitness value %lf)", s->gfit); for (i = 0; i < s->k; i++) free(ideas_per_cluster[i]); } free(ideas_per_cluster); free(best); DestroyAgent(&nidea, _BSO_); va_end(arg); }
/* It executes the Black Hole Algorithm for function minimization Parameters: s: search space Evaluate: pointer to the function used to evaluate agents arg: list of additional arguments */ void runBHA(SearchSpace *s, prtFun Evaluate, ...) { va_list arg, argtmp; int t, i, j; double fitValue, sum, rand, dist, radius; Agent *tmp = NULL; va_start(arg, Evaluate); va_copy(argtmp, arg); if (!s) { fprintf(stderr, "\nSearch space not allocated @runBHA.\n"); exit(-1); } EvaluateSearchSpace(s, _BHA_, Evaluate, arg); /* Initial evaluation of the search space */ for (t = 1; t <= s->iterations; t++) { fprintf(stderr, "\nRunning iteration %d/%d ... ", t, s->iterations); sum = 0; /* Changing the position of each star according to Equation 3 */ for (i = 0; i < s->m; i++) { va_copy(arg, argtmp); rand = GenerateUniformRandomNumber(0, 1); for (j = 0; j < s->n; j++) s->a[i]->x[j] += rand * (s->g[j] - s->a[i]->x[j]); CheckAgentLimits(s, s->a[i]); s->a[i]->fit = Evaluate(s->a[i], arg); /* It executes the fitness function for agent i */ tmp = CopyAgent(s->a[i], _BHA_, _NOTENSOR_); if (s->a[i]->fit < s->gfit) { fitValue = s->gfit; s->gfit = s->a[i]->fit; s->a[i]->fit = fitValue; for (j = 0; j < s->n; j++) { tmp->x[j] = s->g[j]; s->g[j] = s->a[i]->x[j]; s->a[i]->x[j] = tmp->x[j]; } } DestroyAgent(&tmp, _BHA_); sum = sum + s->a[i]->fit; } va_copy(arg, argtmp); /* Event Horizon and evaluating the solutions */ radius = s->gfit / sum; for (i = 0; i < s->m; i++) { dist = EuclideanDistance(s->g, s->a[i]->x, s->n); /* It obtains the euclidean distance */ if (dist < radius) { DestroyAgent(&(s->a[i]), _BHA_); s->a[i] = GenerateNewAgent(s, _BHA_); } } EvaluateSearchSpace(s, _BHA_, Evaluate, arg); fprintf(stderr, "OK (minimum fitness value %lf)", s->gfit); } va_end(arg); }
int main() { INPUT_RECORD irBuffer; memset(&irBuffer,0,sizeof(INPUT_RECORD)); DWORD dwResult; AllocConsole(); hIn = GetStdHandle(STD_INPUT_HANDLE); g_hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetMonitorSize( SCREEN_TEXT_SIZE_X, SCREEN_TEXT_SIZE_Y ); InitMyLog(); char szNationName[100] = {0,} ;//021007 lsw if( GetPrivateProfileString( "NATION_SET", "NATION_NAME", "" , szNationName, 50,PROXY_SERVER_INI_ ) ) { bool bIsFree = (bool)GetPrivateProfileInt( "NATION_SET", "BETA_SERVER", 0, PROXY_SERVER_INI_ ); if(!LocalMgr.InitVersion(szNationName,bIsFree)){return false;} } else { return false; } if( !StartProxyServer() ) { goto FinishProxyServer; } g_pINet->ResumeTimer( 4 ); g_pINet->ResumeTimer( 5 ); g_pINet->ResumeTimer( 6 ); while ( g_pServerTable->IsServerRunning() ) { ReadConsoleInput(hIn,&irBuffer,1,&dwResult); if (irBuffer.EventType == KEY_EVENT) { if (irBuffer.Event.KeyEvent.bKeyDown) { switch ( irBuffer.Event.KeyEvent.wVirtualKeyCode ) { case VK_ESCAPE: { if( MessageBox( NULL, "Are you sure to DESTROY this Proxy server?", "IMPORTANT", MB_YESNO ) == IDYES ) g_pServerTable->DestroyServer( FINISH_TYPE_NORMAL ); }break; case VK_F1: { SetEvent(g_pProxy->hKeyEvent[0]); }break; case VK_F2: { SetEvent(g_pProxy->hKeyEvent[2]); }break; case VK_F3: { // Decrese Max User SetEvent(g_pProxy->hKeyEvent[7]); }break; case VK_F4: { // Increse Max User SetEvent(g_pProxy->hKeyEvent[6]); }break; case VK_F5: { if ( g_pServerTable->ToggleUserAcceptAllowed() == true ) { MyLog( LOG_NORMAL, "USER ACCEPT ALLOWED by Key Event" ); } else { MyLog( LOG_NORMAL, "USER ACCEPT STOPED by Key Event" ); } }break; case VK_F6: { // Increse Limit SetEvent(g_pProxy->hKeyEvent[4]); }break; case VK_F7: { // Decrese Limit SetEvent(g_pProxy->hKeyEvent[5]); }break; case VK_F8: { // Increse Limit //SetEvent(g_pProxy->hKeyEvent[4]); //if( MessageBox( NULL, "WARNING!!! Are you sure Close One AgentServer?", "IMPORTANT", MB_YESNO ) == IDYES ) if( MessageBox( NULL, "WARNING!!! Are you sure Close All AgentServer?", "IMPORTANT", MB_YESNO ) == IDYES )//020822 lsw DestroyAgent(); }break; case VK_F9: { if( MessageBox( NULL, "WARNING!!! Are you sure to DESTROY All DBDemon?", "IMPORTANT", MB_YESNO ) == IDYES ) DestroyAllDBDemon(); }break; case VK_F10: { if( MessageBox( NULL, "WARNING!!! Are you sure to DESTROY ALL Map SERVERS?", "IMPORTANT", MB_YESNO ) == IDYES ) DestroyAllMapserver(); //SetEvent(g_pProxy->hKeyEvent[1]); }break; case VK_F11: { MyLog( LOG_JUST_DISPLAY, " " ); MyLog( LOG_JUST_DISPLAY, "Attempt all disconnected Listener....." ); MyLog( LOG_JUST_DISPLAY, " " ); g_pRMTable->ConnectAllDisconnectedListener(); }break; case VK_F12: { g_pServerTable->ShowListenerStatus(); }break; default:break; } } } } FinishProxyServer: MyLog( LOG_NORMAL, "Deleting ProxyServer Resources..." ); EndProxyServer(); MyLog( LOG_NORMAL, "*** PROXY SERVER Finished Successfully." ); FreeMyLog(); FreeConsole(); return 0; }