void p_show (void) { int result, index; if (stream_->type == T_NUM) { index = atoi(stream_->lexeme); GTNEXT(); if (index <= 0 || index > POOLSIZE) printf ("Value %d out of range. Range is 1 to %d.\n", index, POOLSIZE); else printsolution(--index, NULL); } else if (!strcmp (stream_->lexeme, "best")) { GTNEXT(); result = p_feasible(); if (stream_->type != T_EOF) { printf ("Expected nothing, but got '%s'.\n", stream_->lexeme); return; } if (!result) printsolution (POOLSIZE-1, NULL); else if (result == 1) { for (index = POOLSIZE-1; index >= 0 && pool_->rbuf[index].ptr != pool_->bestfeasible; index--); printsolution (index, NULL); } } else printf ("Expected number or 'best', but got '%s'.\n", stream_->lexeme); }
bool solutions(int** a, int n, int col, vector<vector<string> > & result) { if (col >= n) { vector<string> res = printsolution(a, n); result.push_back(res); return true; } for (int i = 0; i < n; i++) { if (isSafe(a, n, i, col)) { a[i][col] = 1; solutions(a, n, col+1, result); a[i][col] = 0; } } return false; }
/* Parser for commands entered at runtime for simulated annealing and foolish hill climbing. The parser adheres to the following grammar: <cparse>=> status | exit | quit | q | Q | set <saparam> num | get <saparam> | show <show> <saparam>=> temp | alpha | iter | beta | perturb <show>=> best <feasible> | epsilon <feasible>=> feasible | epsilon */ void csaparse (void) { int result; double val; if ( !strcmp (stream_->lexeme, "exit") || !strcmp (stream_->lexeme, "quit") || !strcmp (stream_->lexeme, "Quit") || !strcmp (stream_->lexeme, "q") || !strcmp (stream_->lexeme, "Q") ) { if (GTNEXT()->type != T_EOF) { printf ("Expected nothing, but got '%s'.\n", stream_->lexeme); return; } printf("Final:\n"); printsastatus (); kill(getppid(), SIGQUIT); exit(EXIT_SUCCESS); } else if (!strcmp(stream_->lexeme, "status")) { GTNEXT(); printsastatus(); } else if (!strcmp(stream_->lexeme, "set")) { result = saparam(); GTNEXT(); if (stream_->type == T_NUM && stream_->next->type) val = atof (stream_->lexeme); else { printf ("Expected number, but got '%s'.\n", stream_->lexeme); return; } if (GTNEXT()->type != T_EOF) { printf ("Expected nothing, but got '%s'.\n", stream_->lexeme); return; } switch (result) { case COM_T: pool_->T = val; printf("Temperature now set to: %f\n", val); break; case COM_ITER: pool_->iterations = val; printf("Number of iterationts now set to: %f\n", val); break; case COM_ALPHA: pool_->alpha = val; printf("Alpha now set to: %f\n", val); break; case COM_BETA: pool_->beta = val; printf("Beta now set to: %f\n", val); break; case COM_PERTURB: if (val <=1) { pool_->perturb = mutate1; printf("Set Perturbation Function to mutate function 1.\n"); } else if (val == 2) { pool_->perturb = mutate2; printf("Set Perturbation Function to mutate function 2.\n"); } else { pool_->perturb = pairwise_ex; printf("Set Perturbation Function to mutate function 3 (pairwise exchange).\n"); } break; default: break; } } else if (!strcmp(stream_->lexeme, "get")) { result = saparam(); if (GTNEXT()->type != T_EOF) { printf ("Expected nothing, but got '%s'.\n", stream_->lexeme); return; } switch (result) { case COM_T: printf ("Temperature currently set to: %f\n", pool_->T); break; case COM_ITER: printf ("Number of iterations currently set to: %f\n", pool_->iterations); break; case COM_ALPHA: printf ("Alpha currently set to: %f\n", pool_->alpha); break; case COM_BETA: printf ("Beta currently set to: %f\n", pool_->beta); break; case COM_PERTURB: if (pool_->perturb == mutate1) printf("Current Perturbation Function is: mutate function 1.\n"); else if (pool_->perturb == mutate1) printf("Current Perturbation Function is: mutate function 2.\n"); else printf("Current Perturbation Function is: mutate function 3 (pairwise exchange).\n"); break; default: break; } } else if (!strcmp (stream_->lexeme, "show")) { GTNEXT(); result = sashow(); if (result == COM_ERR) return; if (stream_->type != T_EOF) { printf ("Expected nothing, but got '%s'.\n", stream_->lexeme); return; } switch (result) { case 0: printsolution (SIMA_curr, NULL); break; case 1: printsolution (SIMA_curr, NULL); break; case 2: printsolution (-1, pool_->bestfeasible); default: break; } } else printf ("Command Line Error: Unrecognized: '%s'\n", stream_->lexeme); }
int main() { int m,n,p,i,j,comb,max; printf("Enter number of equations and variables\n"); scanf("%d %d",&m,&n); p=n+1; float *optim=(float *)malloc(n*sizeof(float)); float *blank=(float *)malloc(n*sizeof(float)); float **mat = allocate(m,p); printf("Enter the coefficients of the equations\n"); scanf("%*c"); for(i=0;i<m;i++) { printf("Enter equation %d \n",i+1); for(j=0;j<p;j++) { scanf("%f",(j+*(mat+i))); } } printf("The coefficient matrix is \n"); printmatrix(mat,m,p); printf("\n"); printf("Enter the function to be optimized \n"); for(j=0;j<n;j++) { scanf("%f",optim+j); } printf("Enter 1 to maximize, 0 to minimize \n"); scanf("%d",&max); comb =ncr(n,n-m); float **sols = allocate(comb,n); float **store = allocate(comb,n); float **temp = allocate(m,m+1); int *type=(int *)malloc(comb*sizeof(int)); fill(blank,0,m,n,store); for( i=0;i<comb;i++) { int k=0,l; for(j=0;j<n;j++) { if(fabs(store[i][j] - 1) < 0.001) { for(l=0;l<m;l++) { temp[l][k]=mat[l][j]; } k++; } } for(l=0;l<m;l++) { temp[l][k]=mat[l][n]; } //printmatrix(temp,m,m+1); // printf("\n"); float * fs = (float *)malloc(m*sizeof(float)); type[i]= gauss(temp,m,fs); k =0; for(j=0;j<n;j++) { if(store[i][j] < 0.001) { sols[i][j]=0; } else { sols[i][j]=fs[k]; k++; } } } // printmatrix(sols,comb,n); printsolution(sols,m,n,type,optim,max); return 0; }