Ignore:
Timestamp:
Feb 7, 2003 5:39:18 PM (17 years ago)
Author:
forrest
Message:

Adding Network matrix and PlusMinusOne?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Test/unitTest.cpp

    r115 r118  
    2424#include "ClpPrimalColumnDantzig.hpp"
    2525#include "ClpParameters.hpp"
     26#include "ClpNetworkMatrix.hpp"
     27#include "ClpPlusMinusOneMatrix.hpp"
    2628
    2729#include "Presolve.hpp"
     
    792794  }
    793795 
     796  // test network
     797  {   
     798    std::string fn = mpsDir+"input.130";
     799    int numberColumns;
     800    int numberRows;
     801   
     802    FILE * fp = fopen(fn.c_str(),"r");
     803
     804    if (!fp) {
     805      fprintf(stderr,"Unable to open file input.130 in mpsDir directory\n");
     806      exit(1);
     807    }
     808    int problem;
     809    char temp[100];
     810    // read and skip
     811    fscanf(fp,"%s",temp);
     812    assert (!strcmp(temp,"BEGIN"));
     813    fscanf(fp,"%*s %*s %d %d %*s %*s %d %*s",&problem, &numberRows,
     814           &numberColumns);
     815    // scan down to SUPPLY
     816    while (fgets(temp,100,fp)) {
     817      if (!strncmp(temp,"SUPPLY",6))
     818        break;
     819    }
     820    if (strncmp(temp,"SUPPLY",6)) {
     821      fprintf(stderr,"Unable to find SUPPLY\n");
     822      exit(2);
     823    }
     824    // get space for rhs
     825    double * lower = new double[numberRows];
     826    double * upper = new double[numberRows];
     827    int i;
     828    for (i=0;i<numberRows;i++) {
     829      lower[i]=0.0;
     830      upper[i]=0.0;
     831    }
     832    // ***** Remember to convert to C notation
     833    while (fgets(temp,100,fp)) {
     834      int row;
     835      int value;
     836      if (!strncmp(temp,"ARCS",4))
     837        break;
     838      sscanf(temp,"%d %d",&row,&value);
     839      upper[row-1]=-value;
     840      lower[row-1]=-value;
     841    }
     842    if (strncmp(temp,"ARCS",4)) {
     843      fprintf(stderr,"Unable to find ARCS\n");
     844      exit(2);
     845    }
     846    // number of columns may be underestimate
     847    int * head = new int[2*numberColumns];
     848    int * tail = new int[2*numberColumns];
     849    int * ub = new int[2*numberColumns];
     850    int * cost = new int[2*numberColumns];
     851    // ***** Remember to convert to C notation
     852    numberColumns=0;
     853    while (fgets(temp,100,fp)) {
     854      int iHead;
     855      int iTail;
     856      int iUb;
     857      int iCost;
     858      if (!strncmp(temp,"DEMAND",6))
     859        break;
     860      sscanf(temp,"%d %d %d %d",&iHead,&iTail,&iCost,&iUb);
     861      iHead--;
     862      iTail--;
     863      head[numberColumns]=iHead;
     864      tail[numberColumns]=iTail;
     865      ub[numberColumns]=iUb;
     866      cost[numberColumns]=iCost;
     867      numberColumns++;
     868    }
     869    if (strncmp(temp,"DEMAND",6)) {
     870      fprintf(stderr,"Unable to find DEMAND\n");
     871      exit(2);
     872    }
     873    // ***** Remember to convert to C notation
     874    while (fgets(temp,100,fp)) {
     875      int row;
     876      int value;
     877      if (!strncmp(temp,"END",3))
     878        break;
     879      sscanf(temp,"%d %d",&row,&value);
     880      upper[row-1]=value;
     881      lower[row-1]=value;
     882    }
     883    if (strncmp(temp,"END",3)) {
     884      fprintf(stderr,"Unable to find END\n");
     885      exit(2);
     886    }
     887    printf("Problem %d has %d rows and %d columns\n",problem,
     888           numberRows,numberColumns);
     889    fclose(fp);
     890    ClpSimplex  model;
     891    // now build model
     892   
     893    double * objective =new double[numberColumns];
     894    double * lowerColumn = new double[numberColumns];
     895    double * upperColumn = new double[numberColumns];
     896   
     897    double * element = new double [2*numberColumns];
     898    int * start = new int[numberColumns+1];
     899    int * row = new int[2*numberColumns];
     900    start[numberColumns]=2*numberColumns;
     901    for (i=0;i<numberColumns;i++) {
     902      start[i]=2*i;
     903      element[2*i]=-1.0;
     904      element[2*i+1]=1.0;
     905      row[2*i]=head[i];
     906      row[2*i+1]=tail[i];
     907      lowerColumn[i]=0.0;
     908      upperColumn[i]=ub[i];
     909      objective[i]=cost[i];
     910    }
     911    // Create Packed Matrix
     912    CoinPackedMatrix matrix;
     913    int * lengths = NULL;
     914    matrix.assignMatrix(true,numberRows,numberColumns,
     915                        2*numberColumns,element,row,start,lengths);
     916    // load model
     917   
     918    model.loadProblem(matrix,
     919                      lowerColumn,upperColumn,objective,
     920                      lower,upper);
     921   
     922    model.factorization()->maximumPivots(200+model.numberRows()/100);
     923    model.createStatus();
     924    double time1 = cpuTime();
     925    model.dual();
     926    std::cout<<"Network problem, ClpPackedMatrix took "<<cpuTime()-time1<<" seconds"<<std::endl;
     927    ClpPlusMinusOneMatrix plusMinus(matrix);
     928    assert (plusMinus.getIndices()); // would be zero if not +- one
     929    model.loadProblem(plusMinus,
     930                      lowerColumn,upperColumn,objective,
     931                      lower,upper);
     932   
     933    model.factorization()->maximumPivots(200+model.numberRows()/100);
     934    model.createStatus();
     935    time1 = cpuTime();
     936    model.dual();
     937    std::cout<<"Network problem, ClpPlusMinusOneMatrix took "<<cpuTime()-time1<<" seconds"<<std::endl;
     938    ClpNetworkMatrix network(numberColumns,head,tail);
     939    model.loadProblem(network,
     940                      lowerColumn,upperColumn,objective,
     941                      lower,upper);
     942   
     943    model.factorization()->maximumPivots(200+model.numberRows()/100);
     944    model.createStatus();
     945    time1 = cpuTime();
     946    model.dual();
     947    std::cout<<"Network problem, ClpNetworkMatrix took "<<cpuTime()-time1<<" seconds"<<std::endl;
     948    delete [] lower;
     949    delete [] upper;
     950    delete [] head;
     951    delete [] tail;
     952    delete [] ub;
     953    delete [] cost;
     954    delete [] objective;
     955    delete [] lowerColumn;
     956    delete [] upperColumn;
     957  }
     958 
    794959}
Note: See TracChangeset for help on using the changeset viewer.