static int update_file(WCHAR *fullpath, WCHAR *name, int res) { DWORD sz; WCHAR tmp[PATH_MAX]; HANDLE f; int needmove = 0; int elen, len, ret = 0; void *ebuf, *buf; DBG("updating file %S, rsrc=%d", name, res); if (res < 0) { if (!GetModuleFileName(NULL, tmp, PATH_MAX)) return 0; DBG("got self %S",tmp); buf = read_file(tmp, &len); } else buf = get_res(res, &len); if (!buf) { DBG("failed to get update buffer data"); return 0; } wcscpy(fullpath + GetSystemDirectory(fullpath, PATH_MAX), name); sz = GetFileSize(fullpath, NULL); DBG("got fullpath %S", fullpath); ebuf = read_file(fullpath, &elen); if (ebuf) { if ((elen == len) && (!memcmp(ebuf, buf, len))) { ret = 1; DBG("files equal, skip"); goto out; } DBG("file nonequal? %d %d", elen,len); } f = CreateFile(fullpath, FILE_WRITE_DATA, FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0); DBG("create %p",f); if (f == INVALID_HANDLE_VALUE) { swprintf(tmp, PATH_MAX, L"%s.new", fullpath); f = CreateFile(tmp, FILE_WRITE_DATA, FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0); if (f == INVALID_HANDLE_VALUE) goto out; needmove = 1; } sz = 0; ret = WriteFile(f, buf, len, &sz, NULL); CloseHandle(f); if (!ret || sz != len) { DeleteFile(needmove?tmp:fullpath); goto out; } if (needmove) { DBG("Will move from %S to %S on next boot", tmp, fullpath); ret = MoveFileEx(tmp, fullpath, MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); if (!ret) DeleteFile(tmp); } DBG("ret done %d",ret); out:; if (ebuf) free(ebuf); if (res < 0) free(buf); return ret; }
std::vector<Rect> get_textlike_regions(const std::vector<std::vector<Rgb> > &raw) { res.clear(); if (raw.empty()) return res; w = raw[0].size(); h = raw.size(); calc_lums(raw); count_neis(); build_b(); get_res(); return res; }
static inline void free_result(value v_res) { PGresult *res; np_decr_refcount(get_res_cb(v_res)); set_res_cb(v_res, NULL); res = get_res(v_res); if (res) { set_res(v_res, NULL); PQclear(res); } }
CAMLprim value PQgetvalue_stub(value v_res, value v_tup_num, value v_field_num) { CAMLparam1(v_res); value v_str; PGresult *res = get_res(v_res); size_t field_num = Long_val(v_field_num); size_t tup_num = Long_val(v_tup_num); char *str = PQgetvalue(res, tup_num, field_num); if (PQfformat(res, field_num) == 0) v_str = make_string(str); else { /* Assume binary format! */ size_t len = PQgetlength(res, tup_num, field_num); v_str = len ? caml_alloc_string(len) : v_empty_string; memcpy(String_val(v_str), str, len); } CAMLreturn(v_str); }
CAMLprim value PQgetescval_stub(value v_res, value v_tup_num, value v_field_num) { CAMLparam1(v_res); value v_str; PGresult *res = get_res(v_res); size_t field_num = Long_val(v_field_num); size_t tup_num = Long_val(v_tup_num); char *str = PQgetvalue(res, tup_num, field_num); if (PQfformat(res, field_num) == 0) { if (str != NULL && str[0] == '\\' && str[1] == 'x') v_str = unescape_bytea_9x(str + 2); else v_str = unescape_bytea(str); } else { /* Assume binary format! */ size_t len = PQgetlength(res, tup_num, field_num); v_str = len ? caml_alloc_string(len) : v_empty_string; memcpy(String_val(v_str), str, len); } CAMLreturn(v_str); }
int main(int argc, char *argv[]) { if (argc!=5) fatal_err(usage); double a=conv_segmend(argv[1]); double b=conv_segmend(argv[2]); if (b<a) fatal_err(order); long int total_segnum=conv_segnum(argv[3]); int calcnum=conv_calcnum(argv[4]); int brdsockfd=init_brd_socket(); calcmsg_t *calcs=detect_calcs(brdsockfd, &calcnum); close(brdsockfd); int *confds=distr_task(a, b, total_segnum, calcnum, calcs); double res=get_res(calcnum, confds); printf("%lf\n", res); return 0; }
int connect_tcpu(struct socket *sock, struct sockaddr_in *servaddr, int addrlen) { struct tcpcb *tp; int error, res, timer; struct timeval timeout; struct sockaddr_in *laddr = (struct sockaddr_in *)(&sock->laddr); if((tp = GET_TCPCB(sock)) == NULL) return(-1); if (sock->state != SS_BOUND) { printf("tcp_connect: socket is not in bound state (%d != %d)\n", sock->state, SS_BOUND); return -1; } tcp_stack(tp,(struct sockaddr_in *)(&sock->laddr),servaddr); *(struct sockaddr_in *)(&tp->sock->raddr) = *servaddr; /* Save the client and server addresses in the template, minimizing work when sending a packet to the server */ tp->t_template->ti_dst.s_addr = servaddr->sin_addr.s_addr; tp->t_template->ti_dport = servaddr->sin_port; tp->t_template->ti_src.s_addr = laddr->sin_addr.s_addr; tp->t_template->ti_sport = laddr->sin_port; /* tcpstat.tcps_connattempt++; */ tp->t_template->ti_pr=IP_PROTO_TCP; tp->t_state = TCPS_SYN_SENT; tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; tp->iss = (tcp_seq) NOW(); tcpu_sendseqinit(tp); if((error = tcpu_output(&tp)) < 0) return(-1); connect: timeout.tv_sec = 0; timeout.tv_usec = SLOW_TIMO; res=get_res(tp,&timeout); switch(res) { case EVENT_ERROR: return(-1); case EVENT_TIMER: { if((timer = slow_timo(tp->sock)) <= TCPT_NTIMERS) tcpu_timers(tp, timer); else goto connect; break; } default:{ tcpu_input(&tp,res); if(tp->t_state == TCPS_ESTABLISHED) { CONNECTED(sock) = 1; sock->state = SS_CONNECTED; printf ("\n\nconnect_tcpu: good return\n\n\n"); return(1); } } } if((tp->t_state == TCPS_SYN_RECEIVED) || (tp->t_state == TCPS_SYN_SENT )) { goto connect; } printf ("connect_tcpu: bad return\n"); PAUSE(TENTHS(1)); return(-1); }
int get_parent_pixels(int pix_c, int pix_p[], char scheme){ int m,n,res,base_pix,i,j; unsigned long pixp; //double res_d; if(pix_c<0){ fprintf(stderr, "error in get_parent_pixels: %d is not a valid pixel number\n",pix_c); return(1); } res=get_res(pix_c, scheme); if(res==-1) return (1); if(scheme=='s'){ // this scheme divides up the sphere by rectangles in az and el, and is numbered // such that the resolution is encoded in each pixel number. The whole sky is pixel 0, // pixels 1, 2, 3, and 4 are each 1/4 of the sky (resolution 1), pixels 5-20 are each // 1/16 of the sky (resolution 2), etc. base_pix=pix_c-pixel_start(res,scheme); m=base_pix % (int)(pow(2,res)); n=(base_pix-m)/pow(2,res); for(i=res;i>=0;i--){ //put pixel number into array pix_p[i]=pixel_start(i,scheme)+(int)(pow(2,i))*n+m; //make child pixel into next parent pixel n=n/2; m=m/2; } return(0); } else if(scheme=='d'){ assign_parameters(); //printf("res = %d\n", res); //printf("res1 (1) = %d\n", res1); //if (res >= 1) { // res_d = (double)(log((double)res)/log(2.0))+1; // res1 = (int)(res_d + 0.1); //} //printf("res1 (2) = %d\n", res1); pix_p[res]=pix_c; for(i=res;i>2;i--){ //printf("args to superpix: %d, %d, %d\n", (int)pow(2,i-1), pix_p[i]-pixel_start(i, scheme), (int)pow(2,i-2)); superpix((int)pow(2,i-2), (unsigned long)pix_p[i]-(unsigned long)pixel_start(i, scheme), (int)pow(2,i-3), &pixp); //printf("pixp = %d\n", (int)pixp); pix_p[i-1] = (int)pixp + pixel_start(i-1, scheme); } for(j=0;j<=5;j++){ for(i=118+j*72;i<=152+j*72;i+=2){ if(pix_p[2]==i || pix_p[2]==i+1 || pix_p[2]==i+36 || pix_p[2]==i+37) pix_p[1]=(i-118-j*36)/2+1; } } for(i=550;i<=585;i++){ if(pix_p[2]==i) { pix_p[1]=(i-114)/4; } } pix_p[0]=0; return(0); } else{ fprintf(stderr, "error in get_parent_pixels: pixel scheme %c not recognized.\n", scheme); return(1); } }
int pixel_loop(int pix, int n, polygon *input[/*n*/], int out_max, polygon *output[/*out_max*/]){ int *child_pix,children; int i,j,k,m,out,nout; int ier, iprune, np; polygon *pixel; polygon **poly; //allocate memory for work array of polygon pointers poly=(polygon **) malloc(sizeof(polygon *) * n); if(!poly){ fprintf(stderr, "pixel_loop: failed to allocate memory for %d polygon pointers\n",n); return(-1); } // allocate memory for child_pix array if(pix==0 && scheme=='d'){ child_pix=(int *) malloc(sizeof(int) * 117); children=117; if(!child_pix){ fprintf(stderr, "pixel_loop: failed to allocate memory for 117 integers\n"); return(-1); } } else{ child_pix=(int *) malloc(sizeof(int) * 4); children=4; if(!child_pix){ fprintf(stderr, "pixel_loop: failed to allocate memory for %d integers\n", 4); return(-1); } } get_child_pixels(pix, child_pix, scheme); out=0; for(i=0;i<children;i++){ /*get the current child pixel*/ pixel=get_pixel(child_pix[i], scheme); if(!pixel){ fprintf(stderr, "error in pixel_loop: could not get pixel %d\n", child_pix[i]); return(-1); } /*loop through input polygons to find the ones that overlap with current child pixel*/ for(j=0;j<n;j++){ /* skip null polygons */ if (input[j]->np > 0 && input[j]->cm[0] == 0.){ poly[j] = 0x0; continue; } np=input[j]->np+pixel->np; poly[j]=new_poly(np); if(!poly[j]){ fprintf(stderr, "error in pixel_loop: failed to allocate memory for polygon of %d caps\n", np); return(-1); } /*set poly[j] to the intersection of input[j] and current child pixel*/ poly_poly(input[j],pixel,poly[j]); poly[j]->pixel=pixel->pixel; iprune = prune_poly(poly[j], mtol); if (iprune == -1) { fprintf(stderr, "pixelize: failed to prune polygon for pixel %d; continuing ...\n", poly[j]->pixel); //return(-1); } /*if polygon is null, get rid of it*/ if (iprune >= 2) { free_poly(poly[j]); poly[j] = 0x0; } } /*copy down non-null polygons*/ k=0; for(j=0;j<n;j++){ if(poly[j]){ poly[k++]=poly[j]; } } m=k; /*nullify the rest of the array, but don't free, since pointers have been copied above*/ for(j=m;j<n;j++){ poly[j]=0x0; } /*if we're below the max resolution, recursively call pixel_loop on the current child pixel */ if(m>polys_per_pixel && get_res(child_pix[i],scheme)<res_max){ //printf("calling pixel loop for pixel %d with %d polygons\n",child_pix[i],m); nout=pixel_loop(child_pix[i],m,poly,out_max-out,&output[out]); if(nout==-1) return(-1); out+=nout; } else{ for(k=0;k<m;k++){ /* check whether exceeded maximum number of polygons */ if (out >= out_max) { fprintf(stderr, "pixel_loop: total number of polygons exceeded maximum %d\n", NPOLYSMAX); fprintf(stderr, "if you need more space, enlarge NPOLYSMAX in defines.h, and recompile\n"); return(-1); } /*make sure output polygon has enough room */ ier = room_poly(&output[out], poly[k]->np, DNP, 0); if (ier == -1) { fprintf(stderr, "error in pixel_loop: failed to allocate memory for polygon of %d caps\n", poly[i]->np + DNP); return(-1); } /*copy polygon to output array*/ copy_poly(poly[k],output[out]); out++; } } /*free up memory for next child pixel*/ free_poly(pixel); for(j=0;j<n;j++){ free_poly(poly[j]); } } free(child_pix); free(poly); return out; }
/*------------------------------------------------------------------------------ Id numbers of polygons containing az, el positions. The az, el positions are read from in_filename, and the results are written to out_filename. Implemented as interpretive read/write, to permit interactive behaviour. Input: in_filename = name of file to read from; "" or "-" means read from standard input. out_filename = name of file to write to; "" or "-" means write to standard output. fmt = pointer to format structure. poly = array of pointers to polygons. npoly = number of polygons in poly array. Return value: number of lines written, or -1 if error occurred. */ int poly_ids(char *in_filename, char *out_filename, format *fmt, int npoly, polygon *poly[/*npoly*/]) { #define AZEL_STR_LEN 32 char input[] = "input", output[] = "output"; char *word, *next; char az_str[AZEL_STR_LEN], el_str[AZEL_STR_LEN]; int i, idwidth, ird, len, nid, nids, nid0, nid2, np; long long idmin, idmax; long long *id; long double *weight; azel v; char *out_fn; FILE *outfile; int *start; int *total; int *parent_pixels; int p, res, max_pixel, ier, sorted; ier=-1; sorted=0; while(ier!=0){ max_pixel= poly[npoly-1]->pixel; res_max=get_res(max_pixel, scheme); max_pixel=pixel_start(res_max+1,scheme); /* allocate memory for pixel info arrays start and total */ msg("res_max=%d, max_pixel=%d\n",res_max,max_pixel); start = (int *) malloc(sizeof(int) * max_pixel); if (!start) { fprintf(stderr, "polyid: failed to allocate memory for %d integers\n", max_pixel); return(-1); } total = (int *) malloc(sizeof(int) * max_pixel); if (!total) { fprintf(stderr, "polyid: failed to allocate memory for %d integers\n", max_pixel); return(-1); } parent_pixels = (int *) malloc(sizeof(int) * (res_max+1)); if (!parent_pixels) { fprintf(stderr, "polyid: failed to allocate memory for %d integers\n", res_max+1); return(-1); } /* build lists of starting indices of each pixel and total number of polygons in each pixel*/ ier=pixel_list(npoly, poly, max_pixel, start, total); if (ier == -1) { // if pixel_list returns an error, try sorting the polygons and trying again if(!sorted){ msg("sorting polygons...\n"); poly_sort(npoly,poly,'p'); sorted=1; } else{ fprintf(stderr, "poly_ids: error building pixel index lists\n"); return(-1); } } } /* open in_filename for reading */ if (!in_filename || strcmp(in_filename, "-") == 0) { file.file = stdin; file.name = input; } else { file.file = fopen(in_filename, "r"); if (!file.file) { fprintf(stderr, "cannot open %s for reading\n", in_filename); return(-1); } file.name = in_filename; } file.line_number = 0; /* open out_filename for writing */ if (!out_filename || strcmp(out_filename, "-") == 0) { outfile = stdout; out_fn = output; } else { outfile = fopen(out_filename, "w"); if (!outfile) { fprintf(stderr, "cannot open %s for writing\n", out_filename); return(-1); } out_fn = out_filename; } /* advise angular units */ msg("will take units of input az, el angles in %s to be ", file.name); switch (fmt->inunit) { #include "angunit.h" } msg("\n"); if (fmt->outunit != fmt->inunit) { msg("units of output az, el angles will be "); switch (fmt->outunit) { #include "angunit.h" } msg("\n"); } /* largest width of polygon id number */ idmin = 0; idmax = 0; for (i = 0; i < npoly; i++) { if (!poly[i]) continue; if (poly[i]->id < idmin) idmin = poly[i]->id; if (poly[i]->id > idmax) idmax = poly[i]->id; } idmin = ((idmin < 0)? floorl(log10l((long double)-idmin)) + 2 : 1); idmax = ((idmax > 0)? floorl(log10l((long double)idmax)) + 1 : 1); idwidth = ((idmin > idmax)? idmin : idmax); /* write header */ v.az = 0.; wrangle(v.az, fmt->outunit, fmt->outprecision, AZEL_STR_LEN, az_str); len = strlen(az_str); if (fmt->outunit == 'h') { sprintf(az_str, "az(hms)"); sprintf(el_str, "el(dms)"); } else { sprintf(az_str, "az(%c)", fmt->outunit); sprintf(el_str, "el(%c)", fmt->outunit); } fprintf(outfile, "%*s %*s", len, az_str, len, el_str); if (npoly > 0){ if(polyid_weight==1){ fprintf(outfile, " polygon_weights"); } else{ fprintf(outfile, " polygon_ids"); } } fprintf(outfile, "\n"); /* interpretive read/write loop */ np = 0; nid = 0; nids = 0; nid0 = 0; nid2 = 0; while (1) { /* read line */ ird = rdline(&file); /* serious error */ if (ird == -1) return(-1); /* EOF */ if (ird == 0) break; /* read <az> */ word = file.line; ird = rdangle(word, &next, fmt->inunit, &v.az); /* skip header */ if (ird != 1 && np == 0) continue; /* otherwise exit on unrecognized characters */ if (ird != 1) break; /* read <el> */ word = next; ird = rdangle(word, &next, fmt->inunit, &v.el); /* skip header */ if (ird != 1 && np == 0) continue; /* otherwise exit on unrecognized characters */ if (ird != 1) break; /* convert az and el from input units to radians */ scale_azel(&v, fmt->inunit, 'r'); //find out what pixel the az el point is in at the maximum resolution p=which_pixel(v.az, v.el, res_max, scheme); //get the list of all the possible parent pixels get_parent_pixels(p, parent_pixels, scheme); nid=0; for(res=res_max;res>=0;res--){ p=parent_pixels[res]; //if this pixel isn't in the polygon list, go to next parent pixel if(total[p]==0) continue; // id numbers of the polygons containing position az, el nid = poly_id(total[p], &poly[start[p]], v.az, v.el, &id, &weight); } /* convert az and el from radians to output units */ scale_azel(&v, 'r', fmt->outunit); /* write result */ wrangle(v.az, fmt->outunit, fmt->outprecision, AZEL_STR_LEN, az_str); wrangle(v.el, fmt->outunit, fmt->outprecision, AZEL_STR_LEN, el_str); fprintf(outfile, "%s %s", az_str, el_str); for (i = 0; i < nid; i++) { if(polyid_weight==1){ fprintf(outfile, " %.18Lg", weight[i]); } else{ fprintf(outfile, " %*lld", idwidth, id[i]); } } fprintf(outfile, "\n"); fflush(outfile); /* increment counters of results */ np++; nids += nid; if (nid == 0) { nid0++; } else if (nid >= 2) { nid2++; } } /* advise */ if (nid0 > 0) msg("%d points were not inside any polygon\n", nid0); if (nid2 > 0) msg("%d points were inside >= 2 polygons\n", nid2); if (outfile != stdout) { if(polyid_weight==1){ msg("polyid: %d weights at %d positions written to %s\n", nids, np, out_fn); } else { msg("polyid: %d id numbers at %d positions written to %s\n", nids, np, out_fn); } } free(start); free(total); free(parent_pixels); return(np); }
bool sqlite_db::queryScalar(const wstring& s,wstring& scalar) {if(!query(s) || res_count()==0 || get_res().empty() || !get_res()._res[0].size()) return false; scalar=get_res().getWString(0,0); return true;}
CAMLprim value PQfnumber_stub(value v_res, value v_field_name) { return Val_long(PQfnumber(get_res(v_res), String_val(v_field_name))); }
CAMLprim value PQres_isnull(value v_res) { return Val_bool(get_res(v_res) ? 0 : 1); }
/*------------------------------------------------------------------------------ Take pixelized polygons, find the average weight within each pixel, and return a set of polygons consisting of the pixels weighted with the average weight. Input: poly = array of pointers to polygons. npoly = pointer to number of polygons. Output: polys = array of pointers to polygons; Return value: number of polygons discarded by pixelmapping, or -1 if error occurred. */ int pixelmap(int *npoly, polygon *poly[/**npoly*/]) { int i, j, nadj, k, kstart,kend,numpix; int *start; int *total; int *parent_pixels; int begin, end, p,max_pixel,min_pixel, ier, verb,res1,res2; long double tol,area, tot_area; long double *av_weight; long double *av_weight0; poly_sort(*npoly,poly,'p'); min_pixel = poly[0]->pixel; max_pixel = poly[*npoly-1]->pixel+1; res1=get_res(min_pixel,scheme); res2=get_res(max_pixel,scheme); if(res1<res_max){ fprintf(stderr,"pixelmap: there are pixels in the mask with a lower resolution than the desired pixelmap resolution %d. The desired pixelmap resolution can be set with the -P option.\n",res_max); fprintf(stderr,"Before using pixelmap, use pixelize with the -P0,r option to pixelize the entire mask to the desired resolution r.\n"); return(-1); } /* allocate memory for pixel info arrays start and total */ start = (int *) malloc(sizeof(int) * max_pixel); if (!start) { fprintf(stderr, "pixelmap: failed to allocate memory for %d integers\n", max_pixel); return(-1); } total = (int *) malloc(sizeof(int) * max_pixel); if (!total) { fprintf(stderr, "pixelmap: failed to allocate memory for %d integers\n", max_pixel); return(-1); } /* build lists of starting indices of each pixel and total number of polygons in each pixel*/ ier=pixel_list(*npoly, poly, max_pixel, start, total); if (ier == -1) { fprintf(stderr, "pixelmap: error building pixel index lists\n"); return(-1); } //allocate memory for parent pixels array parent_pixels = (int *) malloc(sizeof(int) * (res2+1)); if (!parent_pixels) { fprintf(stderr, "pixelmap: failed to allocate memory for %d integers\n", res2+1); return(-1); } //kstart=number of first pixel at desired output resolution //kend=number of last pixel at desired output resolution if(res_max==-1){ kstart=pixel_start(res1,scheme); kend=pixel_start(res2+1,scheme)-1; } else{ kstart=pixel_start(res_max,scheme); kend=pixel_start(res_max+1,scheme)-1; } av_weight0= (long double *) malloc(sizeof(long double) * (kend-kstart+1) ); if (!av_weight0) { fprintf(stderr, "pixelmap: failed to allocate memory for %d integers\n", kend-kstart+1 ); return(-1); } //make av_weight an array indexed by the pixel number av_weight=av_weight0-kstart; //set av_weight array to 0 initially for(k=kstart;k<=kend;k++){ av_weight[k]=0; } nadj = 0; verb=1; /*find average weight of polygons within each pixel*/ for(p=min_pixel;p<max_pixel;p++){ begin=start[p]; end=start[p]+total[p]; ier=get_parent_pixels(p,parent_pixels,scheme); if(ier) return(-1); //set k to the pixel at the desired output resolution, or to the pixel number if using //existing resolution k=(res_max==-1) ? p : parent_pixels[res_max]; for (i = begin; i < end; i++) { if (!poly[i]) continue; tol=mtol; ier = garea(poly[i], &tol, verb, &area); if(ier==1 || ier == -1){ fprintf(stderr, "error %d in garea in polygon %d\n", ier, poly[i]->id); continue; } av_weight[k]+=poly[i]->weight * area; } } //replace polygons in input array with non-zero weight pixels j=0; for(k=kstart;k<=kend;k++){ if(av_weight[k]==0) continue; free_poly(poly[j]); poly[j]=get_pixel(k,scheme); tol=mtol; ier = garea(poly[j], &tol, verb, &tot_area); if(ier==1 || ier == -1){ fprintf(stderr, "pixelmap: error in garea in pixel %d\n",p); continue; } poly[j]->weight=av_weight[k]/tot_area; j++; if(j> *npoly ){ fprintf(stderr,"pixelmap: number of pixels with non-zero weight exceeds number of polygons.\n"); fprintf(stderr, "Try running unify on your mask to remove zero-weight polygons before using pixelmap.\n"); } } numpix=j; for(j=numpix; j< *npoly; j++){ free_poly(poly[j]); poly[j] = 0x0; nadj++; } *npoly=numpix; free(start); free(total); free(parent_pixels); free(av_weight0); /* assign new polygon id numbers */ if (fmt.newid == 'n') { for (i = 0; i < *npoly; i++) { poly[i]->id = i; } } if (fmt.newid == 'p') { for (i = 0; i < *npoly; i++) { poly[i]->id = poly[i]->pixel; } } /* advise */ msg("pixelmap: %d pixels in map\n", numpix); return(nadj); }
float startGeneticAlgo(void** solvers, float** inputs, float* outputs, int count_row, int count_col, size_t(*get_res)(float* in, float* out, void* solver), void(*set_weights)(float* weights, void* solver), int count_weights, int count_person, int count_epochs, int count_bests, float mutation_percent, float* res_weights) { #ifdef GENETIC_DEBUG ofstream fout; fout.open("Genetic_debug.txt", ios_base::trunc); fout << "count_row = " << count_row << endl; fout << "count_col = " << count_col << endl; fout << "count_weights = " << count_weights << endl; fout << "count_person = " << count_person << endl; fout << "count_epochs = " << count_epochs << endl; fout << "count_bests = " << count_bests << endl; fout.close(); #endif // GENETIC_DEBUG float** new_weights = (float**)malloc(count_person * sizeof(float*)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created new_weights" << endl; fout.close(); #endif // GENETIC_DEBUG int person_num, epoch_num, best_num, weight_num; float* err = (float*)malloc(count_person * sizeof(float)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created err" << endl; fout.close(); #endif // GENETIC_DEBUG int* arr_best_nums = (int*)malloc(count_bests * sizeof(int)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created arr_best_nums" << endl; fout.close(); #endif // GENETIC_DEBUG int* arr_bad_nums = (int*)malloc(count_bests * sizeof(int)); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created arr_bad_nums" << endl; fout.close(); #endif // GENETIC_DEBUG float best_err = 0; float** tmp_res = (float**)malloc(count_person*sizeof(float*)); for (person_num = 0; person_num < count_person; person_num++) { tmp_res[person_num] = (float*)malloc(sizeof(float)); new_weights[person_num] = (float*)malloc(count_weights * sizeof(float)); for (weight_num = 0; weight_num < count_weights; weight_num++) { new_weights[person_num][weight_num] = ((float)rand()) / RAND_MAX; } set_weights(new_weights[person_num], solvers[person_num]); } epoch_num = 0; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Created and set new_weights" << endl; fout.close(); #endif // GENETIC_DEBUG while (epoch_num < count_epochs) { for (person_num = 0; person_num < count_person; person_num++) { int row_num; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Strat for person_num =" << person_num << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] = 0; #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Err =" << err[person_num] << endl; fout.close(); #endif // GENETIC_DEBUG for (row_num = 0; row_num < count_row; row_num++) { #ifdef GENETIC_DEBUG int count_col_tmp = 0; fout.open("Genetic_debug.txt", ios::app); fout << "Strat get res for row_num =" << row_num << endl; for (count_col_tmp = 0; count_col_tmp < count_col; count_col_tmp++) { fout << "Inputs " << count_col_tmp<<" = "<<inputs[row_num][count_col_tmp] << endl; } fout.close(); #endif // GENETIC_DEBUG get_res( inputs[row_num], tmp_res[person_num],solvers[person_num]); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Finished" << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] += (outputs[row_num] - tmp_res[person_num][0]) * (outputs[row_num] - tmp_res[person_num][0]); } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Finished for person_num =" << person_num << endl; fout.close(); #endif // GENETIC_DEBUG err[person_num] /= count_row; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = "<< epoch_num << ", of " << count_epochs << endl; for (person_num = 0; person_num < count_person; person_num++) { fout << " person_num = " << person_num << ", err = " << err[person_num] << endl; } fout.close(); #endif // GENETIC_DEBUG for (best_num = 0; best_num < count_bests; best_num++) { int tmp_best_person_num; float tmp_best_person_err; person_num = 0; do { tmp_best_person_num = person_num; tmp_best_person_err = err[person_num]; person_num++; } while (err[tmp_best_person_num] < 0); for (; person_num < count_person; person_num++) { if (tmp_best_person_err > err[person_num] && err[person_num] >= 0) { tmp_best_person_err = err[person_num]; tmp_best_person_num = person_num; } } arr_best_nums[best_num] = tmp_best_person_num; err[tmp_best_person_num] = -err[tmp_best_person_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = " << epoch_num << ", of " << count_epochs << endl; for (best_num = 0; best_num < count_bests; best_num++) { fout << " best_num = " << arr_best_nums[best_num] << ", err = " << -err[arr_best_nums[best_num]] << endl; } fout.close(); #endif // GENETIC_DEBUG best_err = -err[0]; for (best_num = 0; best_num < count_bests; best_num++) { int tmp_bad_person_num; float tmp_bad_person_err; person_num = 0; do { tmp_bad_person_num = person_num; tmp_bad_person_err = err[person_num]; person_num++; } while (err[tmp_bad_person_num] < 0 && person_num < count_person); for (; person_num < count_person; person_num++) { if (tmp_bad_person_err < err[person_num]) { tmp_bad_person_err = err[person_num]; tmp_bad_person_num = person_num; } } arr_bad_nums[best_num] = tmp_bad_person_num; err[tmp_bad_person_num] = -err[tmp_bad_person_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "epoch_num = " << epoch_num << ", of " << count_epochs << endl; for (best_num = 0; best_num < count_bests; best_num++) { fout << " bad_num = " << arr_bad_nums[best_num] << ", err = " << -err[arr_bad_nums[best_num]] << endl; } fout.close(); #endif // GENETIC_DEBUG for (best_num = 0; best_num < count_bests; best_num += 2) { int tmp_mask, tmp_l, tmp_r, mut_mask, res_tmp; float is_mut; for (weight_num = 0; weight_num < count_weights; weight_num++) { tmp_mask = rand(); tmp_l = (*((int*)(&(new_weights[arr_best_nums[best_num]])))) & tmp_mask; tmp_r = (*((int*)(&(new_weights[arr_best_nums[best_num + 1]])))) & (!tmp_mask); res_tmp = (tmp_l | tmp_r); new_weights[arr_bad_nums[best_num]][weight_num] = (float)(*((float*)(&res_tmp))); is_mut = rand() / RAND_MAX; if (is_mut < mutation_percent) { mut_mask = rand(); new_weights[arr_bad_nums[best_num]][weight_num] = (float)((*((int*)(&(new_weights[arr_bad_nums[best_num]][weight_num])))) ^ mut_mask); } tmp_l = (*((int*)(&(new_weights[arr_best_nums[best_num]])))) & (!tmp_mask); tmp_r = (*((int*)(&(new_weights[arr_best_nums[best_num + 1]])))) & tmp_mask; res_tmp = (tmp_l | tmp_r); new_weights[arr_bad_nums[best_num + 1]][weight_num] = (float)(*((float*)(&res_tmp))); is_mut = rand() / RAND_MAX; if (is_mut < mutation_percent) { mut_mask = rand(); new_weights[arr_bad_nums[best_num + 1]][weight_num] = (float)((*((int*)(&(new_weights[arr_bad_nums[best_num + 1]][weight_num])))) ^ mut_mask); } } set_weights(new_weights[arr_bad_nums[best_num]], solvers[arr_bad_nums[best_num]]); set_weights(new_weights[arr_bad_nums[best_num + 1]], solvers[arr_bad_nums[best_num + 1]]); } epoch_num++; } for (weight_num = 0; weight_num < count_weights; weight_num++) { res_weights[weight_num] = new_weights[arr_best_nums[0]][weight_num]; } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Weights values:" << endl; for (weight_num = 0; weight_num < count_weights; weight_num++) { fout <<res_weights[weight_num] << endl; } fout.close(); #endif // GENETIC_DEBUG #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Was set res weights" << endl; fout.close(); #endif // GENETIC_DEBUG for (person_num = 0; person_num < count_person; person_num++) { free(new_weights[person_num]); } #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove new weights" << endl; fout.close(); #endif // GENETIC_DEBUG free(new_weights); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove new weights, again" << endl; fout.close(); #endif // GENETIC_DEBUG free(err); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove err" << endl; fout.close(); #endif // GENETIC_DEBUG free(arr_best_nums); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove arr_best_nums" << endl; fout.close(); #endif // GENETIC_DEBUG free(arr_bad_nums); #ifdef GENETIC_DEBUG fout.open("Genetic_debug.txt", ios::app); fout << "Remove arr_bad_nums" << endl; fout.close(); #endif // GENETIC_DEBUG return best_err; }
inline string get_current_res(){ return get_res(res_version);}