Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * aqfuncs.c - Hilfsfunktionen fuer den AQ-Basis algorithmus
- */
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "aqfuncs.h"
- // TODO del
- // ====================================================
- /* print_sample: print a 'struct sample' to 'out' */
- void print_sample(struct sample *s) {
- int i;
- printf("{");
- for (i = 0; i < NUM_FIELDS; ++i)
- if (i < NUM_FIELDS - 1)
- printf("%s, ", s->values[i]);
- else
- printf("%s", s->values[i]);
- printf("}\n");
- }
- /* print_samples: print a list of samples to 'out' */
- void print_samples(struct list_head *head) {
- struct sample *pos;
- list_for_each_entry(pos, head, list)
- print_sample(pos);
- }
- /* free_sample: free a 'struct sample' object */
- void free_sample(struct sample *s) {
- int i;
- for (i = 0; i < NUM_FIELDS; ++i)
- if (s->values[i])
- free(s->values[i]);
- free(s);
- }
- /* delete_samples: free memory occupied by a list of samples */
- void delete_samples(struct list_head *head) {
- struct sample *pos, *q;
- list_for_each_entry_safe(pos, q, head, list) {
- list_del(&pos->list);
- free_sample(pos);
- }
- }
- // ====================================================
- /**
- * Specifies a concept of a positive sample regarding a negative sample. Returns a list of specified concepts
- *
- * gl List head to store specified concepts in
- * og Concept to specify
- * p Positiv sample
- * n Negative sample
- */
- void specify(struct list_head * gl, struct sample * og, struct sample * p,
- struct sample * n) {
- int i, j;
- struct sample *c;
- for (i = 0; i < NUM_FIELDS; i++) {
- c = calloc(1, sizeof(*c));
- for (j = 0; j < NUM_FIELDS; j++) {
- c->values[j] = malloc(50+ 1);
- strcpy(c->values[j], WILDCARD);
- }
- if (strcmp(og->values[i], WILDCARD) == 0) {
- strcpy(c->values[i], p->values[i]);
- // fill all other attributes with wildcard (*)
- for (j = 0; j < NUM_FIELDS; j++) {
- if (i != j && strcmp(og->values[j], WILDCARD) == 0) {
- strcpy(c->values[j], WILDCARD);
- } else if (i != j) {
- strcpy(c->values[j], og->values[j]);
- }
- }
- if (match(c, n) != NUM_FIELDS) {
- // add element to concepts list
- list_add_tail(&c->list, gl);
- } else {
- free_sample(c);
- }
- } else {
- free_sample(c);
- }
- }
- }
- /**
- * Creates a list of concepts for a positive sample
- *
- * gl List head to store all concepts in
- * p Positive sample
- * nl List of negativ samples
- */
- void star(struct list_head * gl, struct sample * p, struct list_head *nl) {
- struct sample *u, *g, *n, *q;
- int i;
- // init empty g
- if (list_empty(gl)) {
- u = calloc(1, sizeof(*u));
- for (i = 0; i < NUM_FIELDS; i++) {
- u->values[i] = malloc(10+ 1);
- strcpy(u->values[i], WILDCARD);
- }
- list_add(&u->list, gl);
- }
- list_for_each_entry_safe(g, q, gl, list) {
- printf("Test concept:");
- print_sample(g);
- list_for_each_entry(n, nl, list) {
- if (match(g, n) == NUM_FIELDS) {
- printf("Negative sample matched: ");
- print_sample(n);
- // specify g
- specify(gl, g, p, n);
- // rm old g
- list_del(&g->list);
- printf("Specified to: \n");
- print_samples(gl);
- break;
- }
- }
- }
- printf("star done\n");
- }
- /*
- * Returns the number of correct matches between concept $c and sample $s
- */
- int match(struct sample* c, struct sample* s) {
- int matches, i;
- matches = 0;
- for (i = 0; i < NUM_FIELDS; i++) {
- if (strcmp(c->values[i], WILDCARD) == 0 || strcmp(s->values[i],
- WILDCARD) == 0 || strcmp(s->values[i], c->values[i]) == 0) {
- matches++;
- }
- }
- return matches;
- }
- /*
- * Removes all positive examples from $pos which matches at least one concept in $g
- */
- void cleanPos(struct list_head *g, struct list_head *pos) {
- struct sample *ps, *c, *q;
- list_for_each_entry(c, g, list) {
- list_for_each_entry_safe(ps, q, pos, list) {
- if (match(c, ps) == NUM_FIELDS) {
- printf("Delete positive example:");
- print_sample(ps);
- list_del(&ps->list);
- free_sample(ps);
- }
- }
- }
- }
- // ====================================================
- /* main, a lot of example stuff: */
- int main(int argc, char *argv[]) {
- struct sample *pos, *ns;
- LIST_HEAD(c);
- LIST_HEAD(nsl);
- pos = calloc(1, sizeof(*pos));
- // pos
- pos->values[0] = malloc(10 + 1);
- pos->values[1] = malloc(10 + 1);
- pos->values[2] = malloc(10 + 1);
- strcpy(pos->values[0], "A");
- strcpy(pos->values[1], "B");
- strcpy(pos->values[2], "C");
- // neg
- ns = calloc(1, sizeof(*ns));
- ns->values[0] = malloc(10 + 1);
- ns->values[1] = malloc(10 + 1);
- ns->values[2] = malloc(10 + 1);
- strcpy(ns->values[0], "A");
- strcpy(ns->values[1], "A");
- strcpy(ns->values[2], "D");
- list_add(&ns->list, &nsl);
- // neg
- ns = calloc(1, sizeof(*ns));
- ns->values[0] = malloc(10 + 1);
- ns->values[1] = malloc(10 + 1);
- ns->values[2] = malloc(10 + 1);
- strcpy(ns->values[0], "R");
- strcpy(ns->values[1], "B");
- strcpy(ns->values[2], "D");
- list_add(&ns->list, &nsl);
- printf("star...\n");
- star(&c, pos, &nsl);
- printf("print...\n");
- //print_samples(&c);
- printf("\ndeleting:");
- delete_samples(&c);
- delete_samples(&nsl);
- free_sample(pos);
- free(ns);
- printf("done\n");
- return 1;
- }
Add Comment
Please, Sign In to add comment