void Group::setModel(Model* newmodel) { if(!newmodel) newmodel = &getDefaultModel(); if(model == newmodel) return; // empty and change model empty(); decrementChildReference(model); incrementChildReference(newmodel); model = newmodel; // recreate data delete[] particleData; delete[] particleCurrentParams; delete[] particleExtendedParams; particleData = new Particle::ParticleData[pool.getNbReserved()]; particleCurrentParams = new float[pool.getNbReserved() * model->getSizeOfParticleCurrentArray()]; particleExtendedParams = new float[pool.getNbReserved() * model->getSizeOfParticleExtendedArray()]; pool.clear(); // Destroys all the buffers destroyAllBuffers(); }
Group::Group(Model* m, uint32 capacity) : Registerable(), Transformable(), model(m != NULL ? m : &getDefaultModel()), renderer(NULL), friction(0.0f), gravity(Vector3D(0,0,0)), pool(capacity), sortingEnabled(false), distanceComputationEnabled(false), NumOfBufferedParticles(0), fupdate(NULL), fbirth(NULL), fdeath(NULL), boundingBoxEnabled(false) { particleData = new Particle::ParticleData[capacity]; particleCurrentParams = new float[capacity * model->getSizeOfParticleCurrentArray()]; particleExtendedParams = new float[capacity * model->getSizeOfParticleExtendedArray()]; }
int main (int argc, const char *argv[]) { const char prefix[] = "cbc_driverC_sos.cpp:main(): "; const int VERBOSE = 4; Cbc_Model * model, * model2; double time1; char modelName[80]; int numberIntegers; int * integerVariable; if (VERBOSE>0) printf("%s begin\n",prefix); if (VERBOSE>0) printf("%s Version %g\n",prefix,Cbc_getVersion()); /* set model using the local routine for reading an MPS file */ model = getDefaultModel(argc, argv); model2 = NULL; /* used to keep model around */ /* This clause ought to set the initial basis, but does not yet work. */ { int i; int row_length = Cbc_getNumRows(model); int col_length = Cbc_getNumCols(model); int rim_length = row_length + col_length; int elem_length = Cbc_getNumElements(model); int * cbc_rowStatus = NULL; int * cbc_columnStatus = NULL; if (0) { fprintf(stdout,"%s row_length = %i\n", prefix, row_length); fprintf(stdout,"%s col_length = %i\n", prefix, col_length); fprintf(stdout,"%s rim_length = %i\n", prefix, rim_length); fprintf(stdout,"%s elem_length = %i\n", prefix, elem_length); fflush(stdout); } /* print solution status variables */ if (0) { if (cbc_rowStatus) { for (i = 0; i < row_length; i++) { printf("%s cbc_rowStatus[%i] = %i\n", prefix, i, cbc_rowStatus[i]); fflush(stdout); } } else { fprintf(stdout,"%s cbc_rowStatus = %p\n", prefix, (void*)cbc_rowStatus); fflush(stdout); } if (cbc_columnStatus) { for (i = 0; i < row_length; i++) { fprintf(stdout,"%s cbc_rowStatus[%i] = %i\n", prefix, i, cbc_columnStatus[i]); fflush(stdout); } } else { fprintf(stdout,"%s cbc_columnStatus = %p\n", prefix, (void*)cbc_columnStatus); fflush(stdout); } } } /* Save model as a clone (does not work as of 2004). */ model2 = Cbc_clone(model); /* Store which variables are integer as defined in the MPS file. They will be set to Continuous before adding the SOS constraints. */ { int i; int numberColumns=Cbc_getNumCols(model); numberIntegers = 0; integerVariable = malloc(numberColumns*sizeof(int[1])); for ( i=0;i<numberColumns;i++) { if (Cbc_isInteger(model,i)) { integerVariable[numberIntegers++]=i; if (VERBOSE>3) printf("%s integerVariable[%i] = %i\n",prefix, numberIntegers-1, integerVariable[numberIntegers-1]); } } } Cbc_problemName(model,80,modelName); /* Solve the MPS version of the problem */ if (1) { if (VERBOSE>1) { printf("%s Solve MPS version of the problem\n",prefix); printf("%s Optimization Direction = %g (1 - minimize, -1 - maximize, 0 - ignore)\n", prefix, Cbc_optimizationDirection(model)); } /* Cbc_setLogLevel(model, VERBOSE); // 4 is verbose */ time1 = Cbc_cpuTime(model) ; Cbc_branchAndBound(model); if (VERBOSE>1) printf("Model %s has %d rows and %d columns\n", modelName,Cbc_numberRows(model),Cbc_numberColumns(model)); if (VERBOSE>1) printf("%s Solving model %s took %g seconds, %i nodes with objective %g\n", prefix, modelName, (Cbc_cpuTime(model)-time1), Cbc_getNodeCount(model), Cbc_getObjValue(model)); if (VERBOSE>0) (!Cbc_status(model)) ? printf(" Finished\n") : printf(" Not finished\n"); if (VERBOSE>2) printSolution(model); } { /* SOS specification data specify numberSets, numPoints, and whichRanges explicitly NOTE: These need to be commented according to the MPS file. Example_1_1: Cbc0004I MPS reads 1081 only columns out of 1085 Then the 4th range goes out of bounds Example_2: Cbc0006I The LP relaxation is infeasible or too expensive Mod_RT_1: Cbc0006I The LP relaxation is infeasible or too expensive */ const int numberSets = 1; /* dummy // 4; // Example_1_1 // 2; // Example_2 // 2; // Mod_RT_1 // 2; // SITH */ const int numPoints = 1; /* dummy // 257; // Example_1_1 // 256; // Example_2 // 257; // Mod_RT_1 // 257; // SITH */ const int whichRanges[1][2] = { /* counting from zero? */ {0,0} /* dummy */ /* {56,312}, {313, 569}, {572, 828}, {829, 1085} // Example_1_1 {48, 303}, {304, 559} // Example_2 {45, 301}, {302, 558} // Mod_RT_1 {45, 301}, {302, 558} // SITH */ }; /* the rest is determined parametrically */ int *len = malloc(numberSets* sizeof(int[1])); int **which = malloc(numberSets* sizeof(int[1])); int setNum, pointNum; double *weights; int i, j; for (setNum = 0; setNum < numberSets; setNum++) { len[setNum] = whichRanges[setNum][1] - whichRanges[setNum][0] + 1; if (len[setNum] != numPoints) { printf("%s ERROR: len[%i] (%i) != numPoints (%i)\n", prefix, setNum, len[setNum], numPoints); return 1; } which[setNum] = malloc(numPoints*sizeof(int[1])); for (j = 0; j < len[setNum]; j++) which[setNum][j] = whichRanges[setNum][0] + j; /* Example_2 */ } weights = malloc(numPoints*sizeof(double[1])); for (pointNum = 0; pointNum < numPoints; pointNum++) weights[pointNum] = pointNum+1; /* Now use SOS2 NOTE: Only enable this if known good SOS Specification (above) */ if (1) { if (VERBOSE>1) printf("%s Use SOS2\n",prefix); /* Restore model */ Cbc_deleteModel(model); if (0) { model = Cbc_clone(model2); } else { model = getDefaultModel(argc, argv); } /* Cbc_setLogLevel(model, 4); // 4 is verbose */ if (VERBOSE>1) printf("%s Model %s has %d rows and %d columns\n", prefix, modelName,Cbc_numberRows(model),Cbc_numberColumns(model)); /* make SOS cuts */ for (i=0;i<numberIntegers;i++) { int iColumn = integerVariable[i]; /* Stop being integer */ Cbc_setContinuous(model, iColumn); } /* add cut (0 - use dense, 1 - use sparse (TODO: test this)) */ if (0) for (i=0;i<numberSets;i++) { printf( "%s calling Cbc_addSOS(), len[%i] = %i, which[%i][0] = %i, which[%i][%i] = %i,\n weights[0] = %g, weights[%i] = %g\n", prefix, i, len[i], i, which[i][0], i, len[i]-1, which[i][len[i]-1], weights[0], len[i]-1, weights[len[i]-1]); /* Cbc_addSOS_Sparse(model,len[i],which[i],weights,i,2); */ } else { int numObjects = numberSets; /* cannot pass const int */ Cbc_addSOS_Dense(model, numObjects, len, (const int* const *)which, (const double*)weights, 2); } } Cbc_setOptimizationDirection(model, 1); /* 1 minimize */ if (VERBOSE>1) { printf("%s Solve MPS version of the problem\n",prefix); printf("%s Optimization Direction = %g (1 - minimize, -1 - maximize, 0 - ignore)\n", prefix, Cbc_optimizationDirection(model)); } if (VERBOSE>1) printf("%s calling Cbc_scaling()\n",prefix); Cbc_scaling(model,1); time1 = Cbc_cpuTime(model) ; if (VERBOSE>1) printf("%s calling Cbc_initialSolve()\n",prefix); Cbc_initialSolve(model); if (VERBOSE>3) Cbc_printModel(model,prefix); if (VERBOSE>1) printf("%s calling Cbc_branchAndBound()\n",prefix); Cbc_branchAndBound(model); if (VERBOSE>1) printf("%s %s took %g seconds, %i nodes with objective %g\n", prefix, modelName, (Cbc_cpuTime(model)-time1), Cbc_getNodeCount(model), Cbc_getObjValue(model)); if (VERBOSE>0) (!Cbc_status(model)) ? printf(" Finished\n") : printf(" Not finished\n"); if (VERBOSE>2) printSolution(model); } if (VERBOSE>1) printf("%s Log Level %i\n", prefix, Cbc_logLevel(model)); if (VERBOSE>0) printf("%s return 0\n",prefix); return 0; } /* main() */