Exemplo n.º 1
0
Tour * tour_mutate(Tour *old_tour) {
    town_index_t i, num_vertexes;
    TownPool *tp;
    Tour * new_tour;

    num_vertexes = old_tour->town_list->config.num_vertexes;
    tp           = tp_new(num_vertexes);
    new_tour     = new_empty_tour(old_tour->town_list, num_vertexes);

    #pragma omp parallel for
    for (i=0; i<num_vertexes; i++){
        new_tour->tour[i] = old_tour->tour[i];
    }

    {
        town_index_t i1, i2;
        i1 = (town_index_t)tp_random_town(tp);
        i2 = (town_index_t)tp_random_town(tp);
        new_tour->tour[i1] = old_tour->tour[i2];
        new_tour->tour[i2] = old_tour->tour[i1];
    }

    new_tour->length = tour_length(new_tour);

    tp_destroy(tp);
    return new_tour;
}
Exemplo n.º 2
0
static PyObject*
nb_subtract (PyObject* o1, PyObject* o2) {
    float o1w = 0, o1h = 0, o1d = 0;
    float o2w = 0, o2h = 0, o2d = 0;
    PyObject *args, *result;
    PySoy_atoms_Size_Object *so1, *so2;

    //* parse object attributes
    if (!PySoy_atoms_Size_Check(o1) || !PySoy_atoms_Size_Check(o2)) {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    so1 = (PySoy_atoms_Size_Object*)o1;
    o1w = soy_atoms_size_get_width(so1->g);
    o1h = soy_atoms_size_get_height(so1->g);
    o1d = soy_atoms_size_get_depth(so1->g);

    so2 = (PySoy_atoms_Size_Object*)o2;
    o2w = soy_atoms_size_get_width(so2->g);
    o2h = soy_atoms_size_get_height(so2->g);
    o2d = soy_atoms_size_get_depth(so2->g);

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1w - o2w, o1h - o2h, o1d - o2d);

    // create result object
    result = tp_new(&PySoy_atoms_Size_Type, args, NULL);

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 3
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    float o1w = 0, o1h = 0, o1d = 0;
    float o2w = 0, o2h = 0, o2d = 0;
    PyObject *args, *result;
    PySoy_atoms_Size_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1w = o1h = o1d = PyLong_AsDouble(o1);
    }
    else if (PyFloat_Check(o1)) {
        o1w = o1h = o1d = PyFloat_AsDouble(o1);
    }
    else if (PySoy_atoms_Size_Check(o1)) {
        so1 = (PySoy_atoms_Size_Object*)o1;
        o1w = soy_atoms_size_get_width(so1->g);
        o1h = soy_atoms_size_get_height(so1->g);
        o1d = soy_atoms_size_get_depth(so1->g);
    }
    else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2w = o2h = o2d = PyLong_AsDouble(o2);
    }
    else if (PyFloat_Check(o2)) {
        o2w = o2h = o2d = PyFloat_AsDouble(o2);
    }
    else if (PySoy_atoms_Size_Check(o2)) {
        so2 = (PySoy_atoms_Size_Object*)o2;
        o2w = soy_atoms_size_get_width(so2->g);
        o2h = soy_atoms_size_get_height(so2->g);
        o2d = soy_atoms_size_get_depth(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1w * o2w, o1h * o2h, o1d * o2d);

    // create result object
    result = tp_new(&PySoy_atoms_Size_Type, args, NULL);

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 4
0
static PyObject*
nb_multiply (PyObject* o1, PyObject* o2) {
    double o1x, o1y, o1z = 0;
    double o2x, o2y, o2z = 0;
    PyObject *args, *result;
	PySoy_atoms_Rotation_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
	  o1x = o1y = o1z = PyLong_AsDouble(o1);	  
	} else if (PyFloat_Check(o1)) {
	  o1x = o1y = o1z = PyFloat_AsDouble(o1);	  
	} else if (PySoy_atoms_Rotation_Check(o1)) {
	  so1 = (PySoy_atoms_Rotation_Object*)o1;
	  o1x = soy_atoms_rotation_get_alpha(so1->g);
	  o1y = soy_atoms_rotation_get_beta(so1->g);
	  o1z = soy_atoms_rotation_get_gamma(so1->g);
	} else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
	  o2x = o2y = o2z = PyLong_AsDouble(o2);	  
	} else if (PyFloat_Check(o2)) {
	  o2x = o2y = o2z = PyFloat_AsDouble(o2);	  
	} else if (PySoy_atoms_Rotation_Check(o2)) {
	  so2 = (PySoy_atoms_Rotation_Object*)o2;
	  o2x = soy_atoms_rotation_get_alpha(so2->g);
	  o2y = soy_atoms_rotation_get_beta(so2->g);
	  o2z = soy_atoms_rotation_get_gamma(so2->g);
	} else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1x * o2x, o1y * o2y, o1z * o2z);

    // create result object
    result = tp_new(&PySoy_atoms_Rotation_Type, args, NULL);

    // decref args tuple
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 5
0
static PyObject*
nb_add (PyObject* o1, PyObject* o2) {
    float o1x = 0, o1y = 0, o1z = 0;
    float o2x = 0, o2y = 0, o2z = 0;
    PyObject *args, *result;
    PySoy_atoms_Position_Object *so1, *so2;

    // parse object 1
    if (PyLong_Check(o1)) {
        o1x = o1y = o1z = PyLong_AsDouble(o1);
    } else if (PyFloat_Check(o1)) {
        o1x = o1y = o1z = PyFloat_AsDouble(o1);
    } else if (PySoy_atoms_Position_Check(o1)) {
        so1 = (PySoy_atoms_Position_Object*)o1;
        o1x = soy_atoms_position_get_x(so1->g);
        o1y = soy_atoms_position_get_y(so1->g);
        o1z = soy_atoms_position_get_z(so1->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // parse object 2
    if (PyLong_Check(o2)) {
        o2x = o2y = o2z = PyLong_AsDouble(o2);
    } else if (PyFloat_Check(o2)) {
        o2x = o2y = o2z = PyFloat_AsDouble(o2);
    } else if (PySoy_atoms_Position_Check(o2)) {
        so2 = (PySoy_atoms_Position_Object*)o2;
        o2x = soy_atoms_position_get_x(so2->g);
        o2y = soy_atoms_position_get_y(so2->g);
        o2z = soy_atoms_position_get_z(so2->g);
    } else {
        PyErr_SetString(PyExc_TypeError, "unsupported operand type(s)");
        return NULL;
    }

    // build args with calculated values
    args = Py_BuildValue("((fff))", o1x + o2x, o1y + o2y, o1z + o2z);

    // create result object
    result = tp_new(&PySoy_atoms_Position_Type, args, NULL);

    // decref args tuple and tmp
    Py_DECREF(args);

    // return calculated result
    return result;
}
Exemplo n.º 6
0
Tour * tour_new(TownList *towns) {
    town_index_t i, num_vertexes = towns->config.num_vertexes;
    TownPool *tp = tp_new(num_vertexes);
    if (!tp)
        exit(1);
    Tour * newtour = new_empty_tour(towns, num_vertexes);
    
    for (i=0; i<num_vertexes; i++){
        newtour->tour[i] = tp_random_town(tp);
    }
    

    newtour->length = tour_length(newtour);
    tp_destroy(tp);
    return newtour;
}