void KalmanFilterBase::reset() { ZeroDelayObserver::reset(); clearA(); clearC(); clearQ(); clearR(); clearStateCovariance(); }
void KalmanFilterBase::setStateSize(unsigned n) { if (n!=n_) { ZeroDelayObserver::setStateSize(n); oc_.stateIdentity = Matrix::Identity(n,n); clearA(); clearC(); clearQ(); clearStateCovariance(); } }
void KalmanFilterBase::setStateSize(unsigned n, unsigned nt) { if ((n!=n_) || (nt_ !=nt)) { ZeroDelayObserver::setStateSize(n); nt_=nt; clearA(); clearC(); clearQ(); clearStateCovariance(); } }
int main(int argc, const char * argv[]) { int x,choice; struct CircularQueue cq; cq.front = CRICULARQUEUESIZE-1; cq.rear = CRICULARQUEUESIZE-1; do{ menu(); scanf("%d", &choice); switch (choice) { case 1: printf("Enter elements to enqueue\n"); scanf("%d", &x); enqueue(&cq, x); break; case 2: x = dequeue(&cq); printf("\n%d is dequeue\n",x); break; case 3: display(&cq); break; case 4: clearQ(&cq); break; case 5: printf("Enter element you need to search for \n"); scanf("%d",&x); int pos = QueueSearch(&cq, x); printf("The key element %d found at index %d\n", x, pos); break; default: printf("\nWrong entry, Try again !\n"); break; } }while (choice != 6); return 0; }
/* search flip new edge first if stuck flip all */ void tabu_search(){ int *g; int *new_g; int gsize; int count; int i; int j; int best_count; int best_i; int best_j; /* some vars for storing result of flip_2_edge */ int best_count_2; int node[4]; /* start with a graph of size 8 */ if( !ReadGraph("204.ce", &g, &gsize) ){ fprintf(stderr, "cannot read\n" ); fflush(stderr); exit(1); } bool flip_new_edge_only = true; int tabu_size; int stuck_num; int stuck_cnt; int stuck_threshold = 10; /* tabu list */ std::set<int> ban_s; std::queue<int> ban_q; /* best_count's collector */ std::vector<int> best_K; int best_start = 0; /* search */ int ra1; int ra2; while(gsize < MAXSIZE){ count = CliqueCount( g, gsize, flip_new_edge_only ); best_K.clear(); best_start = 0; if( count == 0 ){ printf("...Euraka! Counter example FOUND!\n"); PrintGraph(g, gsize); new_g = (int *)malloc((gsize+1)*(gsize+1)*sizeof(int)); if(new_g == NULL) exit(1); CopyGraph( g, gsize, new_g, gsize + 1 ); for(i=0; i<gsize+1; i++){ ra1 = rand() % 2; if(ra1 == 0){ new_g[i*(gsize+1) + gsize] = 0; new_g[gsize*(gsize+1) + i] = 0; } else{ new_g[i*(gsize+1) + gsize] = 1; new_g[gsize*(gsize+1) + i] = 1; } } free(g); g = new_g; gsize++; ban_s.clear(); clearQ(ban_q); flip_new_edge_only = true; stuck_num = 0; stuck_cnt = 0; continue; } best_count = BIGCOUNT; int key; size_t sz; best_count_2 = BIGCOUNT; if( flip_new_edge_only ){ j = gsize - 1; for(i=0; i<gsize-1; i++){ flip_1_edge(g, gsize, i, j, ban_s, best_K, best_start, best_count, true); } } else{ /* flip 1 edge */ for(i=0; i<gsize; i++){ for(j=i+1; j<gsize; j++){ ra1 = rand() % 30; if(ra1 == 0){ flip_1_edge(g, gsize, i, j, ban_s, best_K, best_start, best_count, false); } } } } if(best_count == BIGCOUNT){ printf("no best found, terminating..\n"); exit(1); } sz = best_K.size(); if(best_start < sz - 1){ try_flip_2_edge(g, gsize, best_count_2, best_K, best_start, node, flip_new_edge_only); } tabu_size = (flip_new_edge_only)? gsize/4 : gsize + gsize; if(best_count <= best_count_2){ /* flip 1 edge */ ra1 = (best_start == sz - 1)? best_start : best_start + rand() % (sz - best_start); key = best_K[ra1]; best_i = getI(key); best_j = getJ(key); g[ best_i*gsize + best_j ] = 1 - g[ best_i*gsize + best_j ]; put_to_tabu_list(ban_q, ban_s, tabu_size, key); /* stuck? */ if( flip_new_edge_only ){ i = stuck_num - best_count; if( i < 0 ) i = -i; if( i < 3 ){ stuck_cnt++; if(stuck_cnt == stuck_threshold){ printf("stucked..\n.\n"); flip_new_edge_only = false; stuck_cnt = 0; stuck_num = 0; } } else{ stuck_num = best_count; stuck_cnt = 0; } } printf("ce size: %d, best_count: %d, best edge: (%d, %d), new color: %d\n", gsize, best_count, best_i, best_j, g[best_i*gsize + best_j]); } else{ /* flip 2 edge */ g[ node[0]*gsize + node[1] ] = 1 - g[ node[0]*gsize + node[1] ]; g[ node[2]*gsize + node[3] ] = 1 - g[ node[2]*gsize + node[3] ]; put_to_tabu_list(ban_q, ban_s, tabu_size, getKey(node[0], node[1])); put_to_tabu_list(ban_q, ban_s, tabu_size, getKey(node[0], node[1])); printf("ce size: %d, best_count: %d, best edge: (%d, %d), (%d, %d)\n", gsize, best_count_2, node[0], node[1], node[2], node[3]); } /* rinse and repeat */ } }
/* partial version: assuming the counter example is embeded in the graph one size bigger */ void tabu_search_part(){ int *g; int *new_g; int gsize; int count; int i; int j; int best_count; int best_i; int best_j; /* init tabu list which is made up by 1 set and 1 queue */ std::set<int> ban_s; std::queue<int> ban_q; /* best_counts collector */ std::vector<int> best_k; int best_start = 0; /* start with graph of size 8 */ gsize = 8; g = (int *)malloc(gsize*gsize*sizeof(int)); if(g == NULL) exit(1); /* start out with a counter example */ memset(g, 0, gsize*gsize*sizeof(int)); g[0*gsize + 2] = 1; g[1*gsize + 4] = 1; /* search */ int ra1; while(gsize < MAXSIZE){ /* find how we are doing */ count = CliqueCountPart(g, gsize); /* reset collecor */ best_k.clear(); best_start = 0; /* if we get a counter example */ if(count == 0){ printf("22222....Euraka! Counter found\n"); PrintGraph(g, gsize); /* make a new graph one size bigger */ new_g = (int *)malloc((gsize+1)*(gsize+1)*sizeof(int)); if(new_g == NULL) exit(1); /* copy the old graph into the new graph leaving the last row and last column alone */ CopyGraph(g, gsize, new_g, gsize+1); /* zero out the last column and last row */ for(i=0; i<gsize+1; i++){ ra1 = rand() % 2; if(ra1 == 0){ new_g[i*(gsize+1) + gsize] = 0; new_g[gsize*(gsize+1) + i] = 0; } else{ new_g[i*(gsize+1) + gsize] = 1; new_g[gsize*(gsize+1) + i] = 1; } } /* throw away the old graph and make new one */ free(g); g = new_g; gsize++; /* reset the taboo list for the new graph */ ban_s.clear(); clearQ(ban_q); /* keep going */ continue; } /* otherwise, random flip */ best_count = BIGCOUNT; int key; size_t sz; /* unlike tabu_search_full, here we only flip the new edge, which is the last row amd last column */ j = gsize - 1; for(i=0; i<gsize-1; i++){ ra1 = rand() % 2; if(ra1 == 0){ /* flip it */ g[i*gsize + j] = 1 - g[i*gsize + j]; count = CliqueCountPart(g, gsize); /* is it better and the i,j,count not tabu? */ key = getKey(i, j); if(count <= best_count && ban_s.count(key) == 0){ if(count == best_count){ best_k.push_back(key); } else{ sz = best_k.size(); if(sz == 0){ best_k.push_back(key); } else{ best_k[sz-1] = key; best_start = sz - 1; } } best_count = count; } /* flip it back */ g[i*gsize + j] = 1 - g[i*gsize + j]; } } if(best_count == BIGCOUNT){ printf("no best edge found, terminating\n"); exit(1); } /* keep the best flip we saw */ sz = best_k.size(); ra1 = rand() % (sz - best_start); ra1 += best_start; key = best_k[ra1]; best_i = getI(key); best_j = getJ(key); g[best_i*gsize + best_j] = 1 - g[best_i*gsize + best_j]; /* tabu this graph configuration so that we do not visit it again */ if(ban_q.size() == TABOOSIZE_PART){ ban_s.erase(ban_q.front()); ban_q.pop(); } ban_q.push(key); ban_s.insert(key); printf("ce size: %d, best_count: %d, best edge: (%d, %d), new color: %d\n", gsize, best_count, best_i, best_j, g[best_i*gsize + best_j]); /* rinse and repeat */ } }