Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. /*parameters used for the initialazation,fUCProblemInstance is a class connected with the main problem which is used to pass informations to the sub problem */
  2. const Index horizonLen = fUCProblemInstance->GetHorizonLen();
  3. const Index numNodes = horizonLen + 1;
  4. const Index numArcs = horizonLen * 3;
  5. /*MCFCplex() is the class of the solver and fMCFClass is a pointer to the class MCFClass which is a general class containing general information (virtual functions, indexes, etc) for all the possible solvers of the subproblems, except from MCFCplex there are 3 more possible solvers with different classes, */
  6. fMCFClass = new MCFCplex();
  7.  
  8. /* basic initialization of values for the nodes (starting,ending), the bounds, the deficits and the size of the problem*/
  9. fDualSolution.fHydroDuals.Resize(numNodes, numNodes);
  10. tFlows upperBounds(numArcs, F_INF);
  11. tFlows deficits(numNodes, 0);
  12. tNodes startingNodes(numArcs, InINF);
  13. tNodes endingNodes(numArcs, InINF);
  14.  
  15. Index i;
  16.  
  17. // Seting arcs
  18. fFirstA2 = numNodes-1;
  19. fLastA2 = 2*(numNodes-1);
  20. fFirstA3 = 2*(numNodes-1);
  21. fLastA3 = 3*(numNodes-1);
  22.  
  23. for (i=0; i<numNodes-1; ++i)
  24. {
  25. assert (startingNodes[i]==InINF);
  26. assert (endingNodes[i]==InINF);
  27. assert (startingNodes[i + fFirstA2]==InINF);
  28. assert (endingNodes[i + fFirstA2]==InINF);
  29. assert (startingNodes[i + fFirstA3]==InINF);
  30. assert (endingNodes[i + fFirstA3]==InINF);
  31.  
  32. startingNodes[i] = i; // A_1 arcs
  33. endingNodes[i] = i+1;
  34.  
  35. startingNodes[i + fFirstA2] = i; // A_2 arcs
  36. endingNodes[i + fFirstA2] = numNodes-1;
  37.  
  38. startingNodes[i + fFirstA3] = i; // A_3 arcs
  39. endingNodes[i + fFirstA3] = numNodes-1;
  40. }
  41.  
  42.  
  43. // Seting deficits
  44. deficits[0] = -(fHydroUnit->GetInitialFlood() + fHydroUnit->GetInflow()[0]);
  45. deficits[horizonLen] += -deficits[0];
  46. for (i=1; i<horizonLen; ++i)
  47. {
  48. deficits[i] = -fHydroUnit->GetInflow()[i];
  49. deficits[horizonLen] += -deficits[i];
  50. }
  51. // Adjust lower bound scaling
  52. deficits[0] += fHydroUnit->GetMinFlood();
  53. deficits[horizonLen] -= fHydroUnit->GetMinFlood();
  54.  
  55. // Setting upperbounds (and lowerbounds)
  56. for (i=0; i<horizonLen; ++i)
  57. {
  58. upperBounds[i] = fHydroUnit->GetMaxFlood() - fHydroUnit->GetMinFlood();
  59. upperBounds[i + horizonLen] = fHydroUnit->GetMaxUsage();
  60. upperBounds[i + 2*horizonLen] = fHydroUnit->GetMaxSpillage();
  61. }
  62.  
  63. /*After the initialization of all the parts the solver is also initialized in function LoadNet*/
  64. fMCFClass->LoadNet(numNodes, numArcs, numNodes, numArcs,
  65. &upperBounds[0], NULL, &deficits[0],
  66. &startingNodes[0], &endingNodes[0]);
  67. }
  68.  
  69. void MCFCplex::LoadNet( cIndex nmx , cIndex mmx , cIndex pn , cIndex pm ,
  70. cFRow pU , cCRow pC , cFRow pDfct , cIndex_Set pSn ,
  71. cIndex_Set pEn )
  72. {
  73. // allocating and deallocating memory- - - - - - - - - - - - - - - - - - - -
  74. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  75.  
  76. if( ( mmax && nmax ) && ( ( nmx != nmax ) || ( mmx != mmax ) ) ) {
  77. MemDeAlloc();
  78. nmax = mmax = 0;
  79. }
  80.  
  81. if( ( mmx && nmx ) && ( ( nmx != nmax ) || ( mmx != mmax ) ) ) {
  82. nmax = nmx;
  83. mmax = mmx;
  84. MemAlloc();
  85. }
  86.  
  87. if( ( ! nmax ) || ( ! mmax ) ) // just sit down in the corner and wait
  88. return;
  89.  
  90. // now setting up data - - - - - - - - - - - - - - - - - - - - - - - - - - -
  91. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  92.  
  93. n = pn;
  94. m = pm;
  95.  
  96. // setup data structures for arc creation/deletion- - - - - - - - - - - - -
  97.  
  98. VectAssign( ArcPos , FNumber( 0 ) , FreePos = m );
  99. VectAssign( ArcPos + m , FNumber( Inf<FNumber>() ), mmax - m );
  100. #endif
  101.  
  102. // create and set up temporary data structures - - - - - - - - - - - - - - -
  103.  
  104. int* stn = new int[ m ];
  105. int* enn = new int[ m ];
  106.  
  107. for( Index i = m ; i-- ; ) {
  108. stn[ i ] = pSn[ i ] MINUSONE;
  109. enn[ i ] = pEn[ i ] MINUSONE;
  110. }
  111.  
  112.  
  113. double* sup = new double[ n ];
  114.  
  115. if( pDfct )
  116. VectMAssign( sup , pDfct , n ); // invert the sign of deficits
  117. else
  118. VectAssign( sup , double( 0 ) , n );
  119.  
  120. double* upc = new double[ m ];
  121.  
  122. if( pU )
  123. for( Index i = m ; i-- ; )
  124. if( pU[ i ] == Inf<FNumber>() )
  125. upc[ i ] = CPX_INFBOUND;
  126. else
  127. upc[ i ] = double( pU[ i ] );
  128. else
  129. VectAssign( upc , CPX_INFBOUND , m );
  130.  
  131. double* obj = new double[ m ];
  132.  
  133. if( pC )
  134. for( Index i = m ; i-- ; )
  135. if( pC[ i ] == Inf<CNumber>() ) {
  136. #if( DYNMC_MCF_CPX )
  137. ArcPos[ i ] = pU[ i ];
  138. #endif
  139. obj[ i ] = upc[ i ] = 0;
  140. }
  141. else
  142. obj[ i ] = double( pC[ i ] );
  143. else
  144. VectAssign( obj , double( 0 ) , m );
  145.  
  146.  
  147. // load internal structure of Cplex- - - - - - - - - - - - - - - - - - - - -
  148.  
  149. status = CPXNETcopynet( env , net , CPX_MIN , n , sup , NULL , m , stn ,
  150. enn , NULL , upc , obj , NULL );
  151.  
  152. assert( ! status );
  153.  
  154. // setup QP data - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  155.  
  156. qp = NULL; // problem is initially Network
  157. QPMthd = qpNSimplex; // set default QP solving method to Network Simplex
  158.  
  159.  
  160. // delete temporaries- - - - - - - - - - - - - - - - - - - - - - - - - - - -
  161.  
  162. delete[] obj;
  163. delete[] upc;
  164. delete[] sup;
  165. delete[] enn;
  166. delete[] stn;
  167. status = MCFClass::kUnSolved;
  168.  
  169. } // end( MCFCplex::LoadNet )
  170.  
  171. template<class T1, class T2>
  172. static inline void VectMAssign( T1 *g1 , const T2 *g2 , MCFCplex::Index n )
  173. {
  174. // g1 := - g2
  175.  
  176. for( ; n-- ; )
  177. *(g1++) = - *(g2++);
  178. }
  179.  
  180. template<class T>
  181. static inline void VectAssign( T *const g , const T x , MCFCplex::cIndex n )
  182. {
  183. // g[ i ] = x for each i = 0 .. n - 1
  184.  
  185. for( T *tg = g + n ; tg > g ; )
  186. *(--tg) = x;
  187. }
  188.  
  189. void MCFCplex::MemAlloc( void )
  190. {
  191. // create a new Cplex problem- - - - - - - - - - - - - - - - - - - - - - - -
  192.  
  193. int ts;
  194. net = CPXNETcreateprob( env , &ts , "NET" );
  195.  
  196. // create data structures for arc creation/deletion- - - - - - - - - - - - -
  197.  
  198. ArcPos = new FNumber[ mmax ];
  199.  
  200. // if necessary, resize static members - - - - - - - - - - - - - - - - - - -
  201.  
  202. if( mmax > nmaxarc ) {
  203. if( nmaxarc ) {
  204. delete[] ChangeUB;
  205. delete[] val;
  206. delete[] ind;
  207. }
  208.  
  209. ind = new int[ nmaxarc = mmax ];
  210. val = new double[ nmaxarc ];
  211. ChangeUB = new char[ nmaxarc ];
  212.  
  213. VectAssign( ChangeUB , 'U' , nmaxarc );
  214. }
  215. } // end( MemAlloc )
  216.  
  217. /*--------------------------------------------------------------------------*/
  218.  
  219. void MCFCplex::MemDeAlloc( void )
  220. {
  221. delete[] ArcPos;
  222. if( net )
  223. CPXNETfreeprob( env , &net );
  224. else {
  225. CPXfreeprob( env , &qp );
  226. delete[] Startn;
  227. delete[] Endn;
  228. }
  229. } // end( MemDeAlloc )
  230.  
  231. void MCFCplex::SolveMCF( void )
  232. {
  233.  
  234. if( net ) {
  235. CPXNETprimopt( env , net ); // call the network simplex- - - - - - - - - -
  236. status = CPXNETgetstat( env , net );
  237. }
  238.  
  239. else {
  240. CPXqpopt( env , qp ); // call the QP solver - - - - - - - - - - - - -
  241. status = CPXgetstat( env , qp );
  242. }
  243.  
  244. switch( status ) {
  245. case( CPX_OPTIMAL ): status = MCFClass::kOK;
  246. break;
  247. case( CPX_INForUNBD ):
  248. case( CPX_INFEASIBLE ): status = MCFClass::kUnfeasible;
  249. break;
  250. case( CPX_UNBOUNDED ): status = MCFClass::kUnbounded;
  251. break;
  252. default: status = MCFClass::kStopped;
  253. }
  254.  
  255. } // end( MCFCplex::SolveMCF )
  256.  
  257. Invalid read of Size 8 [PID 963]:
  258. by OX5522AA: CPXNETprimopt
  259. by 0X4b17C0: MCFCplex::SolveMCF (MCFCPLEX.cpp:442)
  260. .
  261. .
  262. . The path continues until it reaches the main function from where function is originally called
  263.  
  264. Adress 0x66ed440 is 16 bytes after a block of size 192 free'd [PID: 2024]
  265. at 0x4C2AF50: operator delete[](void*)(vg_replace_malloc.c:537)
  266. by 0x4302FA: MCFCplex::Loadnet(unsigned int, unsigned int, unsigned int, unsigned int, double const*, double const*, double const*, unsigned int const*, unsigned int const*)(MCFCPLEX.cpp:442)
  267. .
  268. .
  269. . The path continues until it reaches the main function from where function is originally called
  270.  
  271. delete[] obj;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement