int main (void) { state_t vec1, vec2, vec3; binding_t b; int i; srand(time(NULL)); fprintf(stdout, "--- Debut\n"); /* Creation du vecteur */ vec1 = create_state(); vec2 = create_state(); vec3 = create_state(); /* Remplissage */ for (i=0; i<15; i++) { b.id = i; b.place = (int) rand() % 40; add_binding(&vec1, b); add_binding(&vec3, b); } /* Remplissage */ for (i=0; i<15; i++) { b.id = i; b.place = (int) rand() % 40; add_binding(&vec2, b); } print_state(vec1); print_state(vec2); /* Suppression */ for (i=0; i<10; i++) { int r = (int) rand() % 15; fprintf(stdout,"Suppression du %d\n",r); erase_binding(&vec1, find_binding(vec1,r)); erase_binding(&vec3, find_binding(vec3,r)); } /* Suppression */ for (i=0; i<10; i++) { int r = (int) rand() % 15; fprintf(stdout,"Suppression du %d\n",r); erase_binding(&vec2, find_binding(vec2,r)); } print_state(vec1); print_state(vec2); /* Comparaison */ if (compare_states(&vec1, &vec2)) { fprintf(stdout, "VEC1 = VEC2\n"); } else { fprintf(stdout, "VEC1 != VEC2\n"); } if (compare_states(&vec1, &vec3)) { fprintf(stdout, "VEC1 = VEC3\n"); } else { fprintf(stdout, "VEC1 != VEC3\n"); } return 1; }
bool is_on(state_t *value){ Node *aux = head; while(aux != NULL){ if (compare_states(aux->data,value)==0) return true; else aux = aux->next; } return false; }
/********************************************************************** * classify_blob * * Classify the this blob if it is not already recorded in the match * table. Attempt to recognize this blob as a character. The recognition * rating (probability) for this blob will be stored as a part of the * blob. This value will also be returned to the caller. **********************************************************************/ CHOICES classify_blob(TBLOB *pblob, TBLOB *blob, TBLOB *nblob, TEXTROW *row, int fx, const char *string, C_COL color, STATE *this_state, STATE *best_state, INT32 pass, INT32 blob_index) { CHOICES rating; INT32 old_index; chars_classified++; /* Global value */ if (blob_skip) return (NIL); #ifndef GRAPHICS_DISABLED if (display_all_blobs) display_blob(blob, color); #endif rating = get_match (blob); if (rating == NIL) { if (pass) { old_index = blob_index; //?cast to int* blob_type = compare_states (best_state, this_state, (int *) &blob_index); blob_answer = word_answer[blob_index]; if (blob_answer < '!') fprintf (matcher_fp, "Bad compare states: best state=0x%x%x, this=0x%x%x, bits=" INT32FORMAT ", index=" INT32FORMAT ", outdex=" INT32FORMAT ", word=%s\n", best_state->part1, best_state->part2, this_state->part1, this_state->part2, bits_in_states, old_index, blob_index, word_answer); } else blob_type = 0; rating = /*(*blob_matchers [fx]) */ (CHOICES) call_matcher (pblob, blob, nblob, NULL, row); put_match(blob, rating); } #ifndef GRAPHICS_DISABLED if (display_ratings && string) print_choices(string, rating); if (blob_pause) window_wait(blob_window); #endif return (rating); }
bool change_distance(state_t *value, int distance){ Node *aux = head; while (aux != NULL){ if (compare_states(aux->data,value)==0){ aux->distance = distance; printf("TRUE! change distance %d ,",aux->distance); print_state(stdout,value); printf("\n"); return true; }else aux = aux->next; } return false; }
int get_color(state_t *value){ Node *aux = head; while (aux != NULL){ if (compare_states(aux->data,value)==0){ printf("TRUE! get color "); print_state(stdout,value); printf(" Color: %d\n",aux->color); return aux->color; } else aux = aux->next; } return 42; // 42 is flag for 'value' not found }
bool change_color(state_t *value, int color){ Node *aux = head; while (aux != NULL){ if (compare_states(aux->data,value)==0){ aux->color = color; printf("TRUE!,change color %d ,",aux->color); print_state(stdout,value); printf("\n"); return true; } else aux = aux->next; } return false; }
int get_distance(state_t *value){ Node *aux = head; while (aux != NULL){ if (compare_states(aux->data,value)==0){ printf("TRUE! get distance "); print_state(stdout,value); printf(" Distancia: %d\n",aux->distance); return aux->distance; } else aux = aux->next; } return 42; // 42 is a flag for 'value' not found }
bool state_projectiont::is_equal( const statet &old_state, const statet &new_state, bool use_cache) { std::set<vart> vars; unsigned v=0; for(program_formulat::variablest::const_iterator it=program_formula.variables.begin(); it!=program_formula.variables.end(); it++, v++) if(it->is_global) vars.insert(vart(v)); return compare_states(formula_container, new_state, old_state, vars, use_cache); }
int main(int argc, char *argv[]) { int i,j,n; Solver S; unsigned atractor_count=0; unsigned atractor_stats_count=0; std::vector<Lit> lits; char command[100]; float avg_length = 0; std::vector< std::vector<Lit> > orig_clauses; global_var.orig_clauses = &orig_clauses; ReadNET(argv[1], S); //ckhong: read networks and then make transition relations with depth 2 by using update functions //PRINT(orig_clauses.size()); vec<Clause*>* orig_clauses_pr = S.Clauses(); lits.clear(); i=(*orig_clauses_pr).size(); //PRINT(i); while(i--){ int j=(*((*orig_clauses_pr)[i])).size(); //ckhong: j has the number of literals in each clause while(j--){ lits.push_back((*((*orig_clauses_pr)[i]))[j]); } orig_clauses.push_back( lits ); lits.clear(); } /*Handle assignments of variables made in solver*/ /*ckhong: what is this for ?*/ /*ckhong: initial state value setting ?*/ i=number_of_var; while(i--){ if(S.value(i)!= l_Undef){ lits.push_back((S.value(i)==l_True)? Lit(i) : ~Lit(i)); orig_clauses.push_back( lits ); lits.clear(); } } //PRINT(orig_clauses.size()); global_var.S = &S; global_var.number_of_var = number_of_var; global_var.depth=2; if(number_of_var<100){ //ckhong: make n-length formula construct_depth(number_of_var); }else{ construct_depth(100); } //puts("Start searching..."); while(1){ if (!S.solve()) break; /*ckhong: found valid path*/ for( i=1; i<global_var.depth; i++ ){ if(compare_states(0,i,number_of_var,S.model )){ atractor_count++; atractor_stats_count+=i; //PRINT(atractor_stats_count); //constrain all states of atractor sequence on 0 index variable //char temp_attr[i*number_of_var]; char tState[number_of_var]; Attractor tAttr; tAttr.length = i; for( j = 0; j < i; j++ ){ constrain_state(j, S.model, 0, S, number_of_var ); #ifdef PRINT_STATE /*ckhong: attractor state print out*/ int a_tmp=j*number_of_var; int b_tmp=0; int counter=number_of_var; while(counter--){ if(S.model[a_tmp] != l_Undef){ //printf( "%s", (S.model[a_tmp]==l_True)?"1":"0"); //sprintf(temp_attr+a_tmp, "%s", (S.model[a_tmp]==l_True)?"1":"0"); sprintf(tState+b_tmp, "%s", (S.model[a_tmp]==l_True)?"1":"0"); }else{ //printf("-"); //sprintf(temp_attr+a_tmp, "-"); sprintf(tState+b_tmp, "-"); } a_tmp++; b_tmp++; } tAttr.states.push_back(tState); //printf("\n"); #endif } //results.push_back(temp_attr); //results.push_back("\n"); global_var.Attractors.push_back(tAttr); //printf("Attractor %d is of length %d\n\n",atractor_count,i); //avg_length = avg_length + i; break; } } if(global_var.depth == i){ construct_depth( global_var.depth << 1 ); //ckhong: depth = depth * 2 printf("Depth of unfolding transition relation is increased to %d\n",global_var.depth); } } printf("Total number of attractors is %d\n",atractor_count); printf("Average length of attractors is %0.2f\n",avg_length/(float)atractor_count); for (int i=0; i<global_var.Attractors.size(); i++) { std::cout << "Attractor " << i << "\n"; for (int j=0; j<global_var.Attractors[i].length; j++) std::cout << global_var.Attractors[i].states[j] << " " ; std::cout << "\n"; } int MAX_DEPTH = global_var.depth; /* ####### Basin Analysis ####### */ int total_basin_size = 1; int curr_depth = 0; int attr_num = 0; for (curr_depth=2; curr_depth<MAX_DEPTH; curr_depth++) { Solver S_; orig_clauses.clear(); lits.clear(); ReadNET(argv[1], S_); //PRINT(S_.nClauses()); orig_clauses_pr = S_.Clauses(); i=(*orig_clauses_pr).size(); //PRINT(i); while(i--){ int j=(*((*orig_clauses_pr)[i])).size(); while(j--){ lits.push_back((*((*orig_clauses_pr)[i]))[j]); } orig_clauses.push_back( lits ); lits.clear(); } //PRINT(orig_clauses.size()); int count = 0; i=number_of_var; while(i--){ if(S_.value(i)!= l_Undef){ count++; lits.push_back((S_.value(i)==l_True)? Lit(i) : ~Lit(i)); orig_clauses.push_back( lits ); lits.clear(); } } global_var.S = &S_; global_var.depth = 2; global_var.number_of_var = number_of_var; //global_var.orig_clauses = &orig_clauses; construct_depth(curr_depth); int tSize = getPredecessors(S_, attr_num, number_of_var, curr_depth); //printf("level %d: %d\n", curr_depth, tSize); total_basin_size = total_basin_size + tSize; if (tSize == 0) { break; } } printf("Basin size for attractor %d: %d\n", attr_num, total_basin_size); return 0; }
int main(int argc, char **argv) { //print usage if needed if (argc != 2) { fprintf(stderr, "Usage: %s totalRecordNumber\n", argv[0]); exit(0); } //get total record number from argument int totalRecordNumber = atoi(argv[1]); // time the program struct timeval sysTimeStart, sysTimeEnd; gettimeofday(&sysTimeStart, NULL); // set up directories; mkdir("cities", 0777); mkdir("datestamps", 0777); mkdir("messages", 0777); mkdir("states", 0777); mkdir("timestamps", 0777); mkdir("users", 0777); mkdir("bplus", 0777); // set up some counters, etc. unsigned int recordCount = 0, userCount = 0, cityCount = 0, stateCount = 0, timestampCount = 0, datestampCount = 0, messageCount = 0, i,j; // SET UP HASH TABLES FOR CITIES, STATES, TIMESTAMPS, DATESTAMPS // cities city_node *cityHT[HASH_SIZE]; for (i = 0; i < HASH_SIZE; i++) { cityHT[i] = malloc(sizeof(city_node)); cityHT[i] = NULL; } // states state_node *stateHT[HASH_SIZE]; for (i = 0; i < HASH_SIZE; i++) { stateHT[i] = malloc(sizeof(state_node)); stateHT[i] = NULL; } // timestamps timestamp_node *timestampHT[HASH_SIZE]; for (i = 0; i < HASH_SIZE; i++) { timestampHT[i] = malloc(sizeof(timestamp_node)); timestampHT[i] = NULL; } // datestamps datestamp_node *datestampHT[HASH_SIZE]; for (i = 0; i < HASH_SIZE; i++) { datestampHT[i] = malloc(sizeof(datestamp_node)); datestampHT[i] = NULL; } // LOOP OVER RECORD FILES char filename[1024]; FILE *fp = NULL; for (i = 0; i < totalRecordNumber; i++) { //open the corresponding file sprintf(filename, "record_%06d.dat", i); fp = fopen(filename,"rb"); if (!fp) { fprintf(stderr, "Cannot open %s\n", filename); continue; } record_t *record = read_record(fp); // split location into city and state, as best we can char cityStr[TEXT_SHORT]; char stateStr[TEXT_SHORT]; // there's one record where the location is \0, which strtok breaks on if (record->location[0] == '\0') { strncpy(cityStr, "", TEXT_SHORT); strncpy(stateStr, "", TEXT_SHORT); } else { char loc[TEXT_SHORT]; strncpy(loc, record->location, TEXT_SHORT); strncpy(cityStr, strtok(loc, ","), TEXT_SHORT); strncpy(stateStr, strtok(NULL, ","), TEXT_SHORT); } // create state state_t state; strncpy(state.name, stateStr, TEXT_SHORT); // get stateId from hash if we have it already unsigned int stateHash = hash_state(&state) % HASH_SIZE; state_node *s; int stateId = -1; for(s = stateHT[stateHash]; (s != NULL) && (stateId == -1); s = s->next) { if (compare_states(&state, &(s->state)) == 0) { stateId = s->state.stateId; } } // assign stateId, add to hash table, and write file if we don't have it if (stateId == -1) { state.stateId = stateCount; stateId = stateCount; write_state(stateCount, &state); stateCount++; s = malloc(sizeof(state_node)); s->state = state; s->next = stateHT[stateHash]; stateHT[stateHash] = s; } // create city city_t city; city.stateId = stateId; strncpy(city.name, cityStr, TEXT_SHORT); // get cityId from hash if we have it already unsigned int cityHash = hash_city(&city) % HASH_SIZE; city_node *c; int cityId = -1; for(c = cityHT[cityHash]; (c != NULL) && (cityId == -1); c = c->next) { if (compare_cities(&city, &(c->city)) == 0) { cityId = c->city.cityId; } } // assign cityId, add to hash table, and write file if we don't have it if (cityId == -1) { city.cityId = cityCount; cityId = cityCount; write_city(cityCount, &city); cityCount++; c = malloc(sizeof(city_node)); c->city = city; c->next = cityHT[cityHash]; cityHT[cityHash] = c; } // create and write user user_t user; user.userId = record->id; user.cityId = cityId; user.stateId = stateId; strncpy(user.name, record->name, TEXT_SHORT); write_user(userCount, &user); userCount++; // loop over messages for(j = 0; j < record->message_num; j++) { // create timestamp timestamp_t timestamp; timestamp.hour = record->messages[j].hour; timestamp.minute = record->messages[j].minute; // get timestampId from hash if we have it already unsigned int timestampHash = hash_timestamp(×tamp) % HASH_SIZE; timestamp_node *t; int tsId = -1; for(t = timestampHT[timestampHash]; (t != NULL) && (tsId == -1); t = t->next) { if (compare_timestamps(×tamp, &(t->timestamp)) == 0) { tsId = t->timestamp.timestampId; } } // assign timestampId, add to hash table, and write file if we don't have it if (tsId == -1) { timestamp.timestampId = timestampCount; tsId = timestampCount; write_timestamp(timestampCount, ×tamp); timestampCount++; t = malloc(sizeof(timestamp_node)); t->timestamp = timestamp; t->next = timestampHT[timestampHash]; timestampHT[timestampHash] = t; } // create datestamp datestamp_t datestamp; datestamp.year = record->messages[j].year; datestamp.month = record->messages[j].month; datestamp.day = record->messages[j].day; // get datestampId from hash if we have it already unsigned int datestampHash = hash_datestamp(&datestamp) % HASH_SIZE; datestamp_node *d; int dsId = -1; for(d = datestampHT[datestampHash]; (d != NULL) && (dsId == -1); d = d->next) { if (compare_datestamps(&datestamp, &(d->datestamp)) == 0) { dsId = d->datestamp.datestampId; } } // assign datestampId, add to hash table, and write file if we don't have it if (dsId == -1) { datestamp.datestampId = datestampCount; dsId = datestampCount; write_datestamp(datestampCount, &datestamp); datestampCount++; d = malloc(sizeof(datestamp_node)); d->datestamp = datestamp; d->next = datestampHT[datestampHash]; datestampHT[datestampHash] = d; } // create and write message message_t message; strncpy(message.text, record->messages[j].text, TEXT_LONG); message.userId = user.userId; message.timestampId = tsId; message.datestampId = dsId; message.messageId = messageCount; write_message(messageCount, &message); messageCount++; } // free and close record free_record(record); fclose(fp); } // free city nodes city_node *cNode; for (i = 0; i < HASH_SIZE; i++) { cNode = cityHT[i]; while (cNode != NULL) { city_node* tmp = cNode; cNode = cNode->next; free (tmp); } } // free state nodes state_node *sNode; for (i = 0; i < HASH_SIZE; i++) { sNode = stateHT[i]; while (sNode != NULL) { state_node* tmp = sNode; sNode = sNode->next; free (tmp); } } // free timestamp nodes timestamp_node *tNode; for (i = 0; i < HASH_SIZE; i++) { tNode = timestampHT[i]; while (tNode != NULL) { timestamp_node* tmp = tNode; tNode = tNode->next; free (tmp); } } // free datestamp nodes datestamp_node *dNode; for (i = 0; i < HASH_SIZE; i++) { dNode = datestampHT[i]; while (dNode != NULL) { datestamp_node* tmp = dNode; dNode = dNode->next; free (tmp); } } // create, write, print file count information file file_count_t fc; fc.users = userCount; fc.cities = cityCount; fc.states = stateCount; fc.messages = messageCount; fc.timestamps = timestampCount; fc.datestamps = datestampCount; write_file_count(&fc); print_file_count(&fc); // end timing the program gettimeofday(&sysTimeEnd, NULL); float totaltime2 = (sysTimeEnd.tv_sec - sysTimeStart.tv_sec) + (sysTimeEnd.tv_usec - sysTimeStart.tv_usec) / 1000000.0f; printf("Process time %f seconds\n", totaltime2); return 0; }