From b93a87abf2ff30d0d6e4f87a0957b39c0724f75c Mon Sep 17 00:00:00 2001 From: iliya <iliya.saroukhanian@etu.hesge.ch> Date: Mon, 1 Apr 2024 17:23:20 +0200 Subject: [PATCH] cleanup: refactored Aliya's ideas --- src/simplexe.py | 57 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/simplexe.py b/src/simplexe.py index 2247d32..c6631f1 100644 --- a/src/simplexe.py +++ b/src/simplexe.py @@ -255,19 +255,32 @@ class Simplexe: # to iterate over basic variables do: for rowId, baseColId in enumerate(self.__basicVariables): - if self.RHS[rowId] / self.__tableau[rowId][baseColId] < 0: + if self.__getBasicVariableValue(rowId, baseColId) < 0: return False - # raise Exception( - # 'Not implemented', 'Simplexe.__isSolutionFeasible: missing code to be implemented.') return True # returns index of entering col Id # return None or -1 if there is pivot def __selectEnteringColumn(self): # find an entering column for pivot => return -1 if none is found! - raise Exception( - 'Not implemented', 'Simplexe.__selectEnteringColumn: missing code to be implemented.') + + # print(self.__tableau[0]) + # for rowId, baseColId in enumerate(self.__basicVariables): + # print(self.__tableau[rowId, baseColId]) + # + + # This maybe ain't that dumb but it works half the time which is weird + # if self.__PivotCount > self.AMatrix.shape[1] - 1: + # return -1 + # + # return self.__PivotCount + + if np.size(self.__tableau[-1, :-1][np.where(self.__tableau[-1, :-1] + < 0)]) == 0: + return -1 + + return np.argmin(self.__tableau[-1, :-1]) # returns leaving row ID # return None or -1 if there is pivot (sets optimisation status accoringly before returning) @@ -276,8 +289,21 @@ class Simplexe: # iterate using : # rowCount = self.TableauRowCount - 2 if self.IsPhaseI else self.TableauRowCount - 1 # for index in range(rowCount): - raise Exception( - 'Not implemented', 'Simplexe.__selectLeavingRow: missing code to be implemented.') + + rowCount = self.TableauRowCount - 2 if self.IsPhaseI else \ + self.TableauRowCount - 1 + + lhs = self.__tableau[:rowCount, pivotColId] + rhs = self.__tableau[:rowCount, -1] + + ratios = rhs / lhs + + candidates = np.where(ratios > 0, ratios, np.inf) + + if len((np.unique(candidates))) == 0: + return -1 + + return candidates.argmin() def __pivotTableau(self, pivotIDs): if pivotIDs is None or pivotIDs[0] < 0 or pivotIDs[1] < 0: @@ -302,8 +328,21 @@ class Simplexe: # iterate using # pivotRow = self.__tableau[pivotRowId, :] / pivotVal # for rowId in range(self.TableauRowCount): - raise Exception( - 'Not implemented', 'Simplexe.__pivotTableau: missing code to be implemented.') + + # Calculer la ligne du pivot normalisée (avoir la valeur 1 à la colonne du pivot) + pivotRow = self.__tableau[pivotRowId, :] / pivotVal + + # Pour chaque ligne autre que le pivot + for rowId in range(self.TableauRowCount): + if rowId != pivotRowId: + # Mettre la colonne du pivot à 0 + multiplier = self.__tableau[rowId, pivotColId] + self.__tableau[rowId, :] -= multiplier * pivotRow + + self.__tableau[pivotRowId, :] = pivotRow + + # raise Exception( + # 'Not implemented', 'Simplexe.__pivotTableau: missing code to be implemented.') # For a given unfeasible initial problem, this method creates the corresponding Tableau of the Phase I, then launches the optimization of Phase I def initPhaseI(self, simplexe): -- GitLab