void *doWork(void *args) { //int id = (long) args; //printf("Thread %d started.\n", id); int row = (long) args; while(row < C->numRows){ //printf("Thread %d multiplying row %d\n", id, row); multiplyRow(C, A, B, row); row += numThreads; } return NULL; }
void Tableau::preparePhase1() { m_slackCount = getInequalityCount(); m_artificialCount = getConstraintCount(); const int constraintCount = getConstraintCount(); int slackIndex = 1; for(int row = 1; row <= constraintCount; row++) { for(UINT index = 1; index <= m_slackCount; index++) { // Clear Slack matrix slackVar(row,index) = 0; } for(UINT index = 1; index <= m_artificialCount; index++) { // Clear Artificial matrix artificalVar(row,index) = 0; } artificalVar(row,row) = 1; Real slackFactor = getSlackFactor(m_table[row].m_relation); if(slackFactor != 0) { // Set Slack[row,slackIndex] slackVar(row,slackIndex) = slackFactor; slackIndex++; } if(getRightSide(row) < 0) { multiplyRow(row,-1); } } // Set temporary object function, with cost factors = 1 for all artificial variables and 0 for the other variables for(int row = 1; row <= constraintCount; row++) { int c = m_table[row].m_basisVariable = getArtificialColumn(row); objectFactor(c) = 1; } for(int col = 1; col <= getXCount(); col++) { objectFactor(col) = 0; } for(UINT index = 1; index <= m_slackCount; index++) { objectFactor(getSlackColumn(index)) = 0; } }
/** * Changes this matrix into its \c ref. * * \internal * * \return True on success. */ bool RMatrix::ref(int startRow) { int pr, pc; // row which has elements most left becomes first row if ((pr = getPivotRow(startRow)) == -1) { return false; } swapRows(startRow, pr); // where is the pivot element in the 1st row? if ((pc = getPivotCol(startRow)) == -1) { return false; } multiplyRow(startRow, 1.0 / m[startRow][pc]); for (int rc = startRow + 1; rc < rows; ++rc) { addRow(rc, -m[rc][pc], startRow); } if (startRow < rows) { ref(startRow + 1); } return true; }
void Tableau::pivot(size_t pivotRow, size_t pivotColumn, bool primalSimplex) { TableauRow &row = m_table[pivotRow]; const int leaveBasis = row.m_basisVariable; const int enterBasis = (int)pivotColumn; row.m_basisVariable = (int)pivotColumn; const Real &factor = row.m_a[pivotColumn]; const String enteringVarName = getVariableName(enterBasis); const String leavingVarName = getVariableName(leaveBasis); if(isTracing(TRACE_PIVOTING)) { if(primalSimplex) { trace(_T("pivot(%2s,%2s). %s -> %s. Cost[%s]:%-15.10lg Tab[%2s,%2s]=%-15.10lg Minimum:%-15.10lg") ,FSZ(pivotRow),FSZ(pivotColumn) ,enteringVarName.cstr(),leavingVarName.cstr() ,enteringVarName.cstr(),getDouble(getObjectFactor(pivotColumn)) ,FSZ(pivotRow),FSZ(pivotColumn),getDouble(factor) ,getDouble(getObjectValue())); } else { trace(_T("pivot(%2s,%2s). %s -> %s. %s=B[%2s]:%-15.10lg Tab[%2s,%2s]=%-15.10lg Minimum:%-15.10lg") ,FSZ(pivotRow),FSZ(pivotColumn) ,enteringVarName.cstr(),leavingVarName.cstr() ,leavingVarName.cstr(),FSZ(pivotRow),getDouble(getRightSide(pivotRow)) ,FSZ(pivotRow),FSZ(pivotColumn),getDouble(factor) ,getDouble(getObjectValue())); } } multiplyRow(pivotRow,1.0/factor); for(int dstRow = 0; dstRow <= getConstraintCount(); dstRow++) { if(dstRow != (int)pivotRow) { addRowsToGetZero(dstRow,pivotRow,pivotColumn); } } }
void Tableau::addConstraint(size_t xIndex, SimplexRelation relation, const Real &rightSide) { if(xIndex < 1 || (int)xIndex >= getXCount()) { throwException(_T("addConstraint:Invalid xIndex=%s. Valid interval=[1..%s]"), FSZ(xIndex), FSZ(getXCount())); } Real currentValue = 0; int bvRow = -1; for(size_t i = 1; i < m_table.size(); i++) { if(m_table[i].m_basisVariable == (int)xIndex) { currentValue = getRightSide(i); bvRow = (int)i; break; } } String varName = getVariableName(xIndex); switch(relation) { case EQUALS : if(currentValue == rightSide) { trace(_T("addConstraint:No need to add constraint %s = %-16lg. %s already has the specified value."), varName.cstr(), getDouble(rightSide), varName.cstr()); return; } else if(currentValue < rightSide) { relation = GREATERTHAN; } else { relation = LESSTHAN; } break; case LESSTHAN : if(currentValue <= rightSide) { trace(_T("addConstraint:No need to add constraint %s <= %-16lg. %s=%-16lg."), varName.cstr(), getDouble(rightSide), varName.cstr(), getDouble(currentValue)); return; } break; case GREATERTHAN: if(currentValue >= rightSide) { trace(_T("addConstraint:No need to add constraint %s >= %-16lg. %s=%-16lg."), varName.cstr(), getDouble(rightSide), varName.cstr(), getDouble(currentValue)); return; } break; } if(isTracing(TRACE_ITERATIONS)) { trace(_T("Add constraint %s %s %lg"), varName.cstr(), getRelationString(relation), getDouble(rightSide)); } m_table.add(TableauRow(getMaxColumnCount())); const int newRowIndex = getConstraintCount(); addSlackColumn(); TableauRow &newRow = m_table[newRowIndex]; newRow.m_a[xIndex] = 1; slackVar(newRowIndex,m_slackCount) = getSlackFactor(relation); setRightSide(newRowIndex,rightSide); newRow.m_basisVariable = getSlackColumn(m_slackCount); objectFactor(newRow.m_basisVariable) = 1; // if(isTracing()) trace(_T("addConstraint before normalizing:\n %s"),toString().cstr()); if(bvRow >= 0) { addRowsToGetZero(newRowIndex,bvRow,xIndex); } if(getRightSide(newRowIndex) > 0) multiplyRow(newRowIndex,-1); // objectValue() += getRightSide(newRowIndex); }