Changeset 3541:077af526c09b in orange


Ignore:
Timestamp:
04/17/07 10:55:41 (7 years ago)
Author:
miha <miha.stajdohar@…>
Branch:
default
Convert:
22d22a349f58a5cbee51f4f41e345a17fc6798e5
Message:

Numpy is now used in optimization class

Location:
source/orangeom
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • source/orangeom/networkoptimization.cpp

    r3532 r3541  
    2121 
    2222#include "ppp/networkoptimization.ppp" 
    23 #include "graph.hpp" 
    24  
    25 TNetworkOptimization::TNetworkOptimization(int _nVertices, double **_pos, int _nLinks, int **_links) 
    26 { 
    27     nVertices = _nVertices; 
    28     nLinks = _nLinks; 
    29     pos = _pos; 
    30     links = _links; 
     23 
     24TNetworkOptimization::TNetworkOptimization() 
     25{ 
     26    import_array(); 
     27     
     28    nVertices = 0; 
     29    nLinks = 0; 
    3130 
    3231    k = 1; 
     
    4544#endif 
    4645 
    47 void TNetworkOptimization::setData(int _nVertices, double **_pos, int _nLinks, int **_links) 
    48 { 
    49     int i; 
    50  
    51     /* 
    52     for (i = 0; i < nVertices; i++) 
    53     { 
    54         free(pos[i]); 
    55     } 
    56     */ 
    57  
    58     for (i = 0; i < nLinks; i++) 
    59     { 
    60         free(links[i]); 
    61     } 
    62      
    63     if (pos != NULL) 
    64     { 
    65     cout << "set 1" << endl; 
    66     if (pos[0] != NULL) 
    67         free(pos[0]); 
    68     cout << "set 2" << endl; 
    69     if (pos[1] != NULL) 
    70         free(pos[1]); 
    71     cout << "set 3" << endl; 
    72     free(pos); 
    73     } 
    74     free(links); 
    75  
    76     nVertices = _nVertices; 
    77     nLinks = _nLinks; 
    78     pos = _pos; 
    79     links = _links; 
    80 } 
    81  
    8246TNetworkOptimization::~TNetworkOptimization() 
    8347{ 
     
    8953 
    9054    free(links); 
    91  
    92     if (pos != NULL) 
    93     { 
    94         if (pos[0] != NULL) 
    95             free(pos[0]); 
    96  
    97         if (pos[1] != NULL) 
    98             free(pos[1]); 
    99  
    100         free(pos); 
    101     }    
     55    free_Carrayptrs(pos); 
    10256} 
    10357 
     
    12074    return t * 0.98; 
    12175} 
    122 /* 
    123 void dumpCoordinates(double **pos, int columns, int rows) 
    124 { 
     76 
     77void TNetworkOptimization::dumpCoordinates() 
     78{ 
     79    int rows = nVertices; 
     80    int columns = 2; 
     81 
    12582    for (int i = 0; i < rows; i++) 
    12683    { 
     
    13390    } 
    13491} 
    135 */ 
     92 
     93 
     94void TNetworkOptimization::random() 
     95{ 
     96    srand(time(NULL)); 
     97    int i; 
     98    for (i = 0; i < nVertices; i++) 
     99    { 
     100        pos[i][0] = rand() % width; 
     101        pos[i][1] = rand() % height; 
     102    } 
     103} 
     104 
    136105void TNetworkOptimization::fruchtermanReingold(int steps) 
    137106{ 
     
    178147            for (int u = v + 1; u < nVertices; u++) 
    179148            { 
    180                 double difX = pos[0][v] - pos[0][u]; 
    181                 double difY = pos[1][v] - pos[1][u]; 
     149                double difX = pos[v][0] - pos[u][0]; 
     150                double difY = pos[v][1] - pos[u][1]; 
    182151 
    183152                double dif = sqrt(difX * difX + difY * difY); 
     
    207176            // cout << "     v: " << v << " u: " << u << " w: " << edge->weights << endl; 
    208177             
    209             double difX = pos[0][v] - pos[0][u]; 
    210             double difY = pos[1][v] - pos[1][u]; 
     178            double difX = pos[v][0] - pos[u][0]; 
     179            double difY = pos[v][1] - pos[u][1]; 
    211180 
    212181            double dif = sqrt(difX * difX + difY * difY); 
     
    231200                dif = 1; 
    232201 
    233             pos[0][v] = pos[0][v] + ((disp[v][0] / dif) * min(fabs(disp[v][0]), temperature)); 
    234             pos[1][v] = pos[1][v] + ((disp[v][1] / dif) * min(fabs(disp[v][1]), temperature)); 
     202            pos[v][0] = pos[v][0] + ((disp[v][0] / dif) * min(fabs(disp[v][0]), temperature)); 
     203            pos[v][1] = pos[v][1] + ((disp[v][1] / dif) * min(fabs(disp[v][1]), temperature)); 
    235204 
    236205            //pos[v][0] = min((double)width,  max((double)0, pos[v][0])); 
     
    248217 
    249218    free(disp); 
     219    //dumpCoordinates(); 
    250220} 
    251221 
     
    253223#include "orange_api.hpp" 
    254224 
    255 int convert_(TGraphAsList *graph, PyObject *pyxcoors, PyObject *pyycoors, double **&pos, int &nLinks, int **&links) 
    256 { 
    257     int nRows; 
    258     double *xCoor; 
    259     double *yCoor; 
    260      
    261     numericToDouble(pyxcoors, xCoor, nRows); 
    262     numericToDouble(pyycoors, yCoor, nRows); 
    263  
    264     if (graph->nVertices != nRows) 
    265       return 1; 
    266  
    267     pos = (double**)malloc(2 * sizeof (double)); 
    268  
    269     if (pos == NULL) 
    270     { 
    271         cerr << "Couldn't allocate memory\n"; 
    272         exit(1); 
    273     } 
    274  
    275     pos[0] = (double *)malloc(graph->nVertices * sizeof(double)); 
    276     pos[1] = (double *)malloc(graph->nVertices * sizeof(double)); 
    277  
    278     if ((pos[0] == NULL) || (pos[1] == NULL)) 
    279     { 
    280         cerr << "Couldn't allocate memory\n"; 
    281         exit(1); 
    282     } 
    283  
    284     //int count = 0; 
    285     int i = 0; 
    286     for (i = 0; i < graph->nVertices; i++) 
    287     { 
    288         pos[0][i] = (double)xCoor[i]; 
    289         pos[1][i] = (double)yCoor[i]; 
    290     } 
     225PyObject *NetworkOptimization_new(PyTypeObject *type, PyObject *args, PyObject *keyw) BASED_ON (Orange, "(Graph) -> None")  
     226{ 
     227    PyObject *pygraph; 
     228     
     229    if (PyArg_ParseTuple(args, "O:GraphOptimization", &pygraph)) 
     230    { 
     231        TGraphAsList *graph = &dynamic_cast<TGraphAsList &>(PyOrange_AsOrange(pygraph).getReference()); 
     232 
     233        if (graph->nVertices < 2) 
     234          PYERROR(PyExc_AttributeError, "graph has less than two nodes", NULL); 
     235 
     236        //return WrapNewOrange(new TGraphOptimization(graph->nVertices, pos, nLinks, links), type); 
     237        return WrapNewOrange(new TNetworkOptimization(), type); 
     238    } 
     239    else 
     240    { 
     241        return WrapNewOrange(new TNetworkOptimization(), type); 
     242    } 
     243} 
     244/* ==== Free a double *vector (vec of pointers) ========================== */  
     245void TNetworkOptimization::free_Carrayptrs(double **v)  { 
     246    free((char*) v); 
     247} 
     248 
     249/* ==== Allocate a double *vector (vec of pointers) ====================== 
     250    Memory is Allocated!  See void free_Carray(double ** )                  */ 
     251double **TNetworkOptimization::ptrvector(long n)  { 
     252    double **v; 
     253    v=(double **)malloc((size_t) (n*sizeof(double))); 
     254    if (!v)   { 
     255        printf("In **ptrvector. Allocation of memory for double array failed."); 
     256        exit(0);  } 
     257    return v; 
     258} 
     259 
     260/* ==== Create Carray from PyArray ====================== 
     261    Assumes PyArray is contiguous in memory. 
     262    Memory is allocated!                                    */ 
     263double **TNetworkOptimization::pymatrix_to_Carrayptrs(PyArrayObject *arrayin)  { 
     264    double **c, *a; 
     265    int i,n,m; 
     266     
     267    n = arrayin->dimensions[0]; 
     268    m = arrayin->dimensions[1]; 
     269    c = ptrvector(n); 
     270    a = (double *) arrayin->data;  /* pointer to arrayin data as double */ 
     271     
     272    for (i = 0; i < n; i++) 
     273    { 
     274        c[i] = a + i * m; 
     275    } 
     276 
     277    return c; 
     278} 
     279 
     280void TNetworkOptimization::setGraph(TGraphAsList *graph) 
     281{ 
     282    int v, l; 
     283    for (l = 0; l < nLinks; l++) 
     284    { 
     285        free(links[l]); 
     286    } 
     287 
     288    free(links); 
     289    free_Carrayptrs(pos); 
     290 
     291    nVertices = graph->nVertices; 
     292    int dims[2]; 
     293    dims[0] = nVertices; 
     294    dims[1] = 2; 
     295     
     296    coors = (PyArrayObject *) PyArray_FromDims(2, dims, NPY_DOUBLE); 
     297    pos = pymatrix_to_Carrayptrs(coors); 
     298    random(); 
     299 
     300    //dumpCoordinates(); 
    291301 
    292302    links = NULL; 
    293303    nLinks = 0; 
    294304 
    295     int v = 0; 
    296305    for (v = 0; v < graph->nVertices; v++) 
    297306    { 
     
    350359        } 
    351360    } 
    352  
    353     return 0; 
    354 } 
    355  
    356 PyObject *NetworkOptimization_new(PyTypeObject *type, PyObject *args, PyObject *keyw) BASED_ON (Orange, "(Graph, xCoordinates, yCoordinates) -> None")  
     361} 
     362 
     363PyObject *NetworkOptimization_setGraph(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(Graph) -> None") 
    357364{ 
    358365    PyObject *pygraph; 
    359     PyObject *pyxcoors; 
    360     PyObject *pyycoors; 
    361  
    362     /* 
    363     if (PyArg_ParseTuple(args, "OOO:GraphOptimization", &pygraph, &pyxcoors, &pyycoors)) 
    364     { 
    365         TGraphAsList *graph = &dynamic_cast<TGraphAsList &>(PyOrange_AsOrange(pygraph).getReference()); 
    366  
    367         if (graph->nVertices < 2) 
    368           PYERROR(PyExc_AttributeError, "graph has less than two nodes", NULL); 
    369  
    370         int nLinks; 
    371         int **links; 
    372         double **pos;  
    373  
    374         convert(graph, pyxcoors, pyycoors, pos, nLinks, links); 
    375  
    376         return WrapNewOrange(new TGraphOptimization(graph->nVertices, pos, nLinks, links), type); 
    377     } 
    378     else 
    379     { 
    380     /**/ 
    381         return WrapNewOrange(new TNetworkOptimization(0, NULL, 0, NULL), type); 
    382     //} 
    383 } 
    384  
    385 PyObject *NetworkOptimization_newData(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(Graph, xCoordinates, yCoordinates) -> None") 
    386 { 
    387     PyObject *pygraph; 
    388     PyObject *pyxcoors; 
    389     PyObject *pyycoors; 
    390  
    391     if (!PyArg_ParseTuple(args, "OOO:NetworkOptimization.newData", &pygraph, &pyxcoors, &pyycoors)) 
     366 
     367    if (!PyArg_ParseTuple(args, "O:NetworkOptimization.setGraph", &pygraph)) 
    392368        return NULL; 
    393369 
    394370    TGraphAsList *graph = &dynamic_cast<TGraphAsList &>(PyOrange_AsOrange(pygraph).getReference()); 
    395371 
    396     int nLinks; 
    397     int **links; 
    398     double **pos;  
    399  
    400     convert_(graph, pyxcoors, pyycoors, pos, nLinks, links); 
    401  
    402372    CAST_TO(TNetworkOptimization, graphOpt); 
    403      
    404     graphOpt->arrayX = (PyArrayObject *)pyxcoors; 
    405     graphOpt->arrayY = (PyArrayObject *)pyycoors; 
    406     graphOpt->setData(graph->nVertices, pos, nLinks, links); 
    407      
     373    graphOpt->setGraph(graph); 
     374 
    408375    RETURN_NONE; 
    409376} 
    410377 
    411 PyObject *NetworkOptimization_fruchtermanReingold(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(steps) -> None") 
     378PyObject *NetworkOptimization_fruchtermanReingold(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "(steps, temperature) -> temperature") 
    412379{ 
    413380    int steps; 
     
    418385 
    419386    CAST_TO(TNetworkOptimization, graph); 
     387 
    420388    graph->temperature = temperature; 
    421389    graph->fruchtermanReingold(steps); 
    422  
    423     int i; 
    424     for (i = 0; i < graph->nVertices; i++) 
    425     { 
    426         *(double *)(graph->arrayX->data + i * graph->arrayX->strides[0]) = graph->pos[0][i]; 
    427         *(double *)(graph->arrayY->data + i * graph->arrayY->strides[0]) = graph->pos[1][i]; 
    428     } 
    429  
    430     return Py_BuildValue("OOd", graph->arrayX, graph->arrayY, graph->temperature); 
     390     
     391    return Py_BuildValue("d", graph->temperature); 
     392} 
     393 
     394PyObject *NetworkOptimization_getCoors(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "() -> Coors") 
     395{ 
     396    CAST_TO(TNetworkOptimization, graph);    
     397    return Py_BuildValue("O", graph->coors); 
     398} 
     399 
     400PyObject *NetworkOptimization_random(PyObject *self, PyObject *args) PYARGS(METH_VARARGS, "() -> None") 
     401{ 
     402    CAST_TO(TNetworkOptimization, graph); 
     403 
     404    graph->random(); 
     405     
     406    RETURN_NONE; 
    431407} 
    432408 
  • source/orangeom/networkoptimization.hpp

    r3532 r3541  
    1919*/ 
    2020 
    21 #ifndef __GRAPHOPTIMIZATION_HPP 
    22 #define __GRAPHOPTIMIZATION_HPP 
     21#ifndef __NETWORKOPTIMIZATION_HPP 
     22#define __NETWORKOPTIMIZATION_HPP 
     23 
     24#include "Python.h" 
     25 
     26#ifdef _MSC_VER 
     27  /* easier to do some ifdefing here than needing to define a special 
     28     include in every project that includes this header */ 
     29  #include "../lib/site-packages/numpy/core/include/numpy/arrayobject.h" 
     30#else 
     31  #include <numpy/arrayobject.h> 
     32#endif 
    2333 
    2434#include <math.h> 
    2535#include <stdio.h> 
     36#include <stdlib.h> 
     37#include <time.h> 
    2638#include <iostream> 
    2739#include "px/orangeom_globals.hpp" 
    2840#include "root.hpp" 
    2941#include "numeric_interface.hpp" 
     42#include "graph.hpp" 
    3043 
    3144using namespace std; 
     
    3649    __REGISTER_CLASS 
    3750 
    38     TNetworkOptimization(int nVertices, double **pos, int nLinks, int **links); 
     51    TNetworkOptimization(); 
    3952    ~TNetworkOptimization(); 
    4053     
     54    void random(); 
    4155    void fruchtermanReingold(int steps); 
    42     void setData(int nVertices, double **pos, int nLinks, int **links); 
    4356    double getTemperature() {return temperature;} 
    4457    void setTemperature(double t) {temperature = t;} 
     58    void setGraph(TGraphAsList *graph); 
     59    void dumpCoordinates(); 
     60 
     61    double **ptrvector(long n); 
     62    double **pymatrix_to_Carrayptrs(PyArrayObject *arrayin); 
     63    void free_Carrayptrs(double **v); 
    4564 
    4665    float k; //PR 
     
    4968    int width; //P 
    5069    int height; //PR 
     70    PyArrayObject *coors; 
    5171 
    5272    int nLinks; 
    5373    int nVertices; 
    5474    int **links; 
     75 
    5576    double **pos; 
    56  
    57     PyArrayObject *arrayX; 
    58     PyArrayObject *arrayY; 
    59  
    6077    double attractiveForce(double x); 
    6178    double repulsiveForce(double x); 
Note: See TracChangeset for help on using the changeset viewer.