void *staff_runner(void *arg){ Runner_Arg *rarg = (Runner_Arg *) arg; int read; int fd_is_close; while(1){ // lock ; read file; int locked = pthread_mutex_lock(rarg->read_fd_lock); if (locked!=0) return error_lock("Could not lock on file : ", rarg->read_fd_lock); char *line = read_line_from_input(rarg->fd, &fd_is_close); if (!line){ if (fd_is_close){ pthread_mutex_unlock(rarg->read_fd_lock); //TODO - next step. return NULL; } return error_lock("Strange... could not allocate memory? : " , rarg->read_fd_lock); } if (pthread_mutex_unlock(rarg->read_fd_lock) != 0){ perror("Unlock :"); } char **arguments = split(line, &read); // Check arguments is clean: if (arguments == NULL ) return error_("Could not allocate in split of input line: "); if (read != 6 ){ fprintf(stderr, "Ignoring input line: %s | %d.", line, read); freeArray(arguments); continue; } //Create new Staff entity. if (strcasecmp(arguments[2], "teacher") == 0){ if (read_teacher(arg, arguments, read) != 0){ return NULL; } } else{ fprintf(stderr, "EntityType %s is not valid\n", arguments[2]); } freeArray(arguments); } }
int main(int argc, char *argv[]) { if (argc != 2) { std::cerr << "usage: " << argv[0] << " inputfile" << std::endl; exit(1); } double teacher[MAX_TEACHERS][IN_SIZE]; if (!read_teacher(argv[1], teacher)) { std::cerr << "read_teacher failed" << std::endl; exit(1); } double in[IN_SIZE], out[OUT_SIZE]; double weights[IN_SIZE][OUT_SIZE]; // initialize weights for (int i = 0; i < IN_SIZE; i++) { for (int j = 0; j < OUT_SIZE; j++) { weights[i][j] = 0; // works only for perceptron ? (should be random numbers) } } // learning bool is_learning = true; int num_loops = 0; while (is_learning) { for (int j = 0; j < MAX_TEACHERS; j++) { for (int k = 0; k < IN_SIZE - 1; k++) { in[k] = teacher[j][k]; } in[IN_SIZE-1] = 1; for (int k = 0; k < OUT_SIZE; k++) { // calculating the current output double o = 0; for (int l = 0; l < IN_SIZE; l++) { o += weights[l][k] * in[l]; } // check if it fires or not out[k] = step_func(o); } if (is_learned(teacher, in, out, weights)) { is_learning = false; break; } // update weights for (int k = 0; k < OUT_SIZE; k++) { for (int l = 0; l < IN_SIZE; l++) { weights[l][k] += ETA * (teacher[j][IN_SIZE-1] - out[k]) * in[l]; } } } if (++num_loops == MAX_LOOPS) { std::cout << "not solved" << std::endl; break; } } std::cout << "num_loops: " << num_loops << std::endl; for (int k = 0; k < OUT_SIZE; k++) { for (int l = 0; l < IN_SIZE; l++) { std::cout << weights[l][k] << " "; } std::cout << std::endl; } // check if learning works for (int j = 0; j < MAX_TEACHERS; j++) { for (int k = 0; k < IN_SIZE - 1; k++) { in[k] = teacher[j][k]; } for (int k = 0; k < OUT_SIZE; k++) { // calculating the current output double o = 0; for (int l = 0; l < IN_SIZE; l++) { o += weights[l][k] * in[l]; } std::cout << "in: " << in[0] << ", " << in[1] << " - o: " << o << std::endl; } } return 0; }