def testrunGetProbGapToOpt(self, testrun, problemid): """ Return the gap between found an solufile-solution """ optsol = testrun.problemGetOptimalSolution(problemid) status = testrun.problemGetSoluFileStatus(problemid) pb = testrun.getProblemDataById(problemid, Key.PrimalBound) if status == 'opt' or status == 'best': return misc.getGap(float(pb), float(optsol)) else: return misc.FLOAT_INFINITY
def calculateGaps(self): """ Calculate and store primal and dual gap """ for testrun in self.getTestRuns(): for problemid in testrun.getProblemIds(): optval = testrun.getProblemDataById(problemid, Key.OptimalValue) if optval is not None: for key in [Key.PrimalBound, Key.DualBound]: val = testrun.getProblemDataById(problemid, key) if val is not None: gap = misc.getGap(val, optval, True) # subtract 'Bound' and add 'Gap' from Key thename = key[:-5] + "Gap" testrun.addDataById(thename, gap, problemid)
def writeSolufile(self): """ Write a solu file based on the parsed results """ # ## collect data solufiledata = {} for testrun in self.getTestRuns(): for probname in testrun.getProblemIds(): pb = testrun.getProblemDataById(probname, Key.PrimalBound) db = testrun.getProblemDataById(probname, Key.DualBound) if pb is None or db is None: continue status = '=unkn=' infinite = (pb >= misc.FLOAT_INFINITY or pb <= -misc.FLOAT_INFINITY) sense = 0 if pb < db: sense = 1 else: sense = -1 if not infinite and misc.getGap(pb, db, True) <= self.gaptol: status = '=opt=' elif not infinite: status = '=best=' elif pb == db: status = '=inf=' currentsolufileentry = solufiledata.get(probname) if currentsolufileentry == None: solufiledata[probname] = (status, pb) else: solustatus, solupb = currentsolufileentry if solustatus == '=best=': assert sense != 0 if not infinite and sense * (solupb - pb) < 0 or status == '=opt=': solufiledata[probname] = (status, pb) elif solustatus == '=unkn=': solufiledata[probname] = (status, pb) # # write solufiledata to file newsolufilename = 'newsolufile.solu' f = open(newsolufilename, 'w') for prob in sorted(list(solufiledata.keys()), reverse = False): solustatus, solupb = solufiledata.get(prob) f.write("%s %s" % (solustatus, prob)) if solustatus in ['=best=', '=opt=']: f.write(" %g" % solupb) f.write("\n") f.close()
def determineStatusForUnknProblem(self, testrun, problemid): """ Determine status for a problem for which we don't know anything about the feasibility or optimality """ pb = testrun.getProblemDataById(problemid, Key.PrimalBound) db = testrun.getProblemDataById(problemid, Key.DualBound) solverstatus = testrun.getProblemDataById(problemid, Key.SolverStatus) if solverstatus: code = Key.solverToProblemStatusCode(solverstatus) elif pb is not None: code = Key.ProblemStatusCodes.Better elif misc.getGap(pb, db) < self.gaptol: code = Key.ProblemStatusCodes.SolvedNotVerified else: code = Key.ProblemStatusCodes.Unknown return code
def calculateGaps(self): """ Calculate and store primal and dual gap """ # use validation reference bounds for storing primal and dual gaps if self.validation is None: return for testrun in self.getTestRuns(): for problemid in testrun.getProblemIds(): optval = self.validation.getReferencePb(testrun.getProblemDataById(problemid, Key.ProblemName)) if optval is not None: for key in [Key.PrimalBound, Key.DualBound]: val = testrun.getProblemDataById(problemid, key) if val is not None: gap = misc.getGap(val, optval, True) # subtract 'Bound' and add 'Gap' from Key thename = key[:-5] + "Gap" testrun.addDataById(thename, gap, problemid)
def validateDual(self, pb, db): """validate the relative gap between the primal and dual bound if dual validation is enabled """ if self.validatedual: return misc.getGap(pb, db) < self.gaptol return True