Exemplo n.º 1
0
void pins_model_init(model_t model)
{
    lts_type_t ltstype;
    matrix_t *dm_info = malloc(sizeof(matrix_t));
    matrix_t *dm_read_info = malloc(sizeof(matrix_t));
    matrix_t *dm_must_write_info = malloc(sizeof(matrix_t));

    // get ltstypes
    ltstype=lts_type_create();
    const int state_length = pnml_vars;

    // adding types
    int ntypes = 2;
    int int_type = lts_type_add_type(ltstype, "int", NULL);
    int act_type =lts_type_add_type(ltstype, "action", NULL);

    lts_type_set_format (ltstype, int_type, LTStypeDirect);
    lts_type_set_format (ltstype, act_type, LTStypeEnum);

    lts_type_set_state_length(ltstype, state_length);

    // set state name & type
    for (int i=0; i < state_length; ++i) {
        lts_type_set_state_name(ltstype,i,pnml_var_names[i]);
        lts_type_set_state_typeno(ltstype,i,int_type);
    }

    // edge label types
    lts_type_set_edge_label_count (ltstype, 1);
    lts_type_set_edge_label_name(ltstype, 0, "action");
    lts_type_set_edge_label_type(ltstype, 0, "action");
    lts_type_set_edge_label_typeno(ltstype, 0, act_type);

    int bool_is_new, bool_type = lts_type_add_type(ltstype, LTSMIN_TYPE_BOOL, NULL);

    GBsetLTStype(model, ltstype); // must set ltstype before setting initial state
                                  // creates tables for types!

    if (bool_is_new) {
        GBchunkPutAt(model, bool_type, chunk_str(LTSMIN_VALUE_BOOL_FALSE), 0);
        GBchunkPutAt(model, bool_type, chunk_str(LTSMIN_VALUE_BOOL_TRUE ), 1);
    }

    // setting values for types
    for (int i = 0; i < pnml_groups; i++) {
        GBchunkPutAt(model, act_type, chunk_str((char*) pnml_group_names[i]), i);
    }

    // get initial state
    GBsetInitialState(model,pnml_initial_state);

    for (int i = 0; i < pnml_vars; i++) {
        if (max_token_count < pnml_initial_state[i]) {
            max_token_count = pnml_initial_state[i];
        }
    }

    // get next state
    GBsetNextStateLong(model, (next_method_grey_t) pnml_get_successor);

    lts_type_validate(ltstype); // done with ltstype

    // initialize the state read/write dependency matrices
    dm_create(dm_read_info, pnml_groups, state_length);
    dm_create(dm_must_write_info, pnml_groups, state_length);

    for (int i = 0; i < pnml_groups; i++) {
        for (int j = 0; j < pnml_sources[i][0]; j++) {
            dm_set(dm_read_info, i, pnml_sources[i][j*2+1]);
            dm_set(dm_must_write_info, i, pnml_sources[i][j*2+1]);
        }
        for (int j = 0; j < pnml_targets[i][0]; j++) {
            if (!pnml_safe_places[pnml_targets[i][j*2+1]]) {
                /* If the place is safe we don't need to mark it read-dependent. */
                dm_set(dm_read_info, i, pnml_targets[i][j*2+1]);
            }
            dm_set(dm_must_write_info, i, pnml_targets[i][j*2+1]);
        }
    }
    dm_copy(dm_read_info, dm_info);
    dm_apply_or(dm_info, dm_must_write_info);

    GBsetDMInfo(model, dm_info);
    GBsetDMInfoRead(model, dm_read_info);
    GBsetDMInfoMustWrite(model, dm_must_write_info);
    GBsetSupportsCopy(model);

    GBsetExit(model, pnml_exit);

    matrix_t *dna_info = malloc(sizeof(matrix_t));
    dm_create(dna_info, pnml_groups, pnml_groups);
    for (int i = 0; i < pnml_groups; i++) {
        for(int j = 0; j < pnml_groups; j++) {
            const int guards_i = pnml_sources[i][0];
            const int guards_j = pnml_sources[j][0];
            for (int g = 0; g < guards_i; g++) {
                const int guard_i = pnml_sources[i][g * 2 + 1];
                for (int h = 0; h < guards_j; h++) {
                    const int guard_j = pnml_sources[j][h * 2 + 1];
                    if (guard_i == guard_j) {
                        dm_set(dna_info, i, j);
                        goto next_dna;
                    }
                }
            }
            next_dna:;
        }
    }
    GBsetDoNotAccordInfo(model, dna_info);

    matrix_t *gnes_info = malloc(sizeof(matrix_t));
    dm_create(gnes_info, pnml_vars, pnml_groups);
    for(int i = 0; i < pnml_groups; i++) {
        const int targets = pnml_targets[i][0];
        for (int t = 0; t < targets; t++) {
            const int target = pnml_targets[i][t * 2 + 1];
            dm_set(gnes_info, target, i);
        }
    }
    GBsetGuardNESInfo(model, gnes_info);

    matrix_t *gnds_info = malloc(sizeof(matrix_t));
    dm_create(gnds_info, pnml_vars, pnml_groups);
    for(int i = 0; i < pnml_groups; i++) {
        const int sources = pnml_sources[i][0];
        for (int s = 0; s < sources; s++) {
            const int source = pnml_sources[i][s * 2 + 1];
            dm_set(gnds_info, source, i);
        }
    }
    GBsetGuardNDSInfo(model, gnds_info);

    matrix_t *ndb_info = malloc(sizeof(matrix_t));
    dm_create(ndb_info, pnml_groups, pnml_groups);
    for (int i = 0; i < pnml_groups; i++) {
        const int sources = pnml_sources[i][0];
        for (int j = 0; j < pnml_groups; j++) {
            const int targets = pnml_targets[j][0];
            for (int s = 0; s < sources; s++) {
                const int source = pnml_sources[i][s * 2 + 1];
                for (int t = 0; t < targets; t++) {
                    const int target = pnml_targets[j][t * 2 + 1];
                    if (source == target) {
                        dm_set(ndb_info, i, j);
                        goto next_ndb;
                    }
                }
            }
            next_ndb:;
        }
    }
    GBsetMatrix(model, LTSMIN_NOT_LEFT_ACCORDS, ndb_info, PINS_STRICT, PINS_INDEX_OTHER, PINS_INDEX_OTHER);
}
Exemplo n.º 2
0
Arquivo: dm.c Projeto: graydon/ltsmin
int
dm_optimize (matrix_t *r, matrix_t *mayw, matrix_t *mustw)
{
    HREassert(
                dm_ncols(r) == dm_ncols(mayw) &&
                dm_nrows(r) == dm_nrows(mayw) &&
                dm_ncols(r) == dm_ncols(mustw) &&
                dm_nrows(r) == dm_nrows(mustw), "matrix sizes do not match");
    
    matrix_t* test = RTmalloc(sizeof(matrix_t));
    dm_create(test, dm_nrows(r), dm_ncols(r));
    dm_copy(r, test);
    dm_apply_or(test, mayw);

    int                 d_rot[dm_ncols (r)];
    permutation_group_t rot;

    int best_i = 0,
    best_j = 0,
    min = cost_ (r, mayw),
    last_min = 0;
    int i, j, c, k, d;

    int firsts[dm_nrows(r)];
    int lasts[dm_nrows(r)];

    while (last_min != min) {
        last_min = min;

        // initialize first and last integers per row
        for (i=0; i<dm_nrows(r); i++) {
            firsts[i] = first_(test,i);
            lasts[i]  = last_(test,i);
        }

        // find best rotation
        for (i = 0; i < dm_ncols (r); i++) {
            for (j = 0; j < dm_ncols (r); j++) {
                if (i != j) {
                    c=estimate_cost(test,i,j,firsts,lasts);
                    if (c < min) {
                        min = c;
                        best_i = i;
                        best_j = j;
                    }
                }
            }
        }

        // rotate
        if (best_i != best_j) {
            d = best_i < best_j ? 1 : -1;
            dm_create_permutation_group (&rot, dm_ncols (r), d_rot);
            for (k = best_i; k != best_j; k += d)
                dm_add_to_permutation_group (&rot, k);
            dm_add_to_permutation_group (&rot, best_j);
            dm_permute_cols (r, &rot);
            dm_permute_cols (mayw, &rot);
            dm_permute_cols (mustw, &rot);
            dm_permute_cols (test, &rot);
            dm_free_permutation_group (&rot);

            DMDBG (printf("best rotation: %d-%d, costs %d\n", best_i, best_j, min));
            DMDBG (dm_print_combined (stdout, r, mayw, mustw));

            best_i = best_j = 0;
        }
    }
    DMDBG (printf ("cost: %d ", cost_ (r, mayw)));
    dm_free(test);
    return 0;
}