def test_allBadSpecsAreBad(spec): badSpecs = getAllBadSpecs(spec, 15) for badSpec in badSpecs: foundry = Foundry( Optimize()).applyAndTrackSpec(badSpec).applyAndTrackSpec(spec) assert foundry.check() == unsat assert len(foundry.getReasonsForUnsat()) != 0
class Generator(): def __init__(self, spec: Spec): self.foundry = Foundry(Optimize()) self.foundry.applySpec(spec) self.spec = spec def createNew(self): try: pitches = self.foundry.extractPitches(self.spec.line) except Exception: return [] self.foundry.apply(distinctFromExample(self.spec.line, [ConstPitch(x) for x in pitches])) return pitches
def getAllBadSpecs(spec: Spec, maxCount=None, doShuffle=True, filterOutUnsatisfiable=True) -> List[Spec]: badSpecs = [] constraintsToInclude = powset(spec.constraints) constraintsToInvert = constraintsToInclude[::-1] constraintsToInclude = constraintsToInclude[:-1] constraintsToInvert = constraintsToInvert[:-1] if doShuffle: constraintsToInclude, constraintsToInvert = shuffle( constraintsToInclude, constraintsToInvert) if maxCount is None: maxCount = len(constraintsToInclude) else: maxCount = min(maxCount, len(constraintsToInclude)) for i in range(maxCount): constraints = [ pitchesLetterValueValid(spec.line) ] # Needed ensure that the spec doesn't cheat on the gamut constraint constraints += constraintsToInclude[i] constraints += [x.inv() for x in constraintsToInvert[i]] badSpec = Spec(spec.line, constraints, spec.maximisations, spec.minimisations) foundry = Foundry(Optimize()).applySpec(badSpec) if filterOutUnsatisfiable: if foundry.check() == sat: badSpecs.append(badSpec) else: badSpecs.append(badSpec) return badSpecs
def foundry(): return Foundry(Optimize())
def test_s3ValidWorksOnInvalidS3GoodCF(foundry, goodCF, s3GoodCF, s3MaxCounts): badSpecs = getAllBadSpecs(s3GoodCF, s3MaxCounts) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) s3GoodCF = foundry.extractPitches(badSpec.line) assert checkS3(goodCF, s3GoodCF).isValid() == False
def test_badCFIsS3Sat(s3BadCF): foundry = Foundry(Optimize()) foundry.applySpec(s3BadCF) assert foundry.check() == sat
def test_s2ValidWorksOnInvalidS2BadCF(foundry, badCF, s2BadCF, s2MaxCounts): badSpecs = getAllBadSpecs(s2BadCF, s2MaxCounts) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) s2BadCF = foundry.extractPitches(badSpec.line) assert checkS2(badCF, s2BadCF).isValid() == False
def test_s1ValidWorksOnInvalidS1BadCF(foundry, badCF, s1BadCF, maxCount): badSpecs = getAllBadSpecs(s1BadCF, maxCount) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) s1BadCF = foundry.extractPitches(badSpec.line) assert checkS1(badCF, s1BadCF).isValid() == False
def test_repairS2WorksFromGoodCF(foundry, goodCF, s2GoodCF, s2MaxCounts, highSpeciesGamut): badSpecs = getAllBadSpecs(s2GoodCF, s2MaxCounts) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) s2GoodCF = foundry.extractPitches(badSpec.line) assert checkS2(goodCF, repairS2(goodCF, s2GoodCF, highSpeciesGamut)).isValid() == True
def test_defaultsAreSatisfiable(spec): assert Foundry(Optimize()).applySpec(spec).check() == sat
def test_repairS1WorksFromBadCF(foundry, badCF, s1BadCF, maxCount, highSpeciesGamut): badSpecs = getAllBadSpecs(s1BadCF, maxCount) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) s1BadCF = foundry.extractPitches(badSpec.line) assert checkS1(badCF, repairS1(badCF, s1BadCF, highSpeciesGamut)).isValid() == True
def test_repairCFWorksOnBrokenCF(foundry, cantus, maxCount, cantusGamut): badSpecs = getAllBadSpecs(cantus, maxCount) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) cantus = foundry.extractPitches(badSpec.line) assert checkCF(repairCF(cantus, cantusGamut)).isValid()
def __init__(self, spec: Spec): self.foundry = Foundry(Optimize()) self.foundry.applySpec(spec) self.spec = spec
def test_allBadSpecsSatisfiable(spec): badSpecs = getAllBadSpecs(spec, 15) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) assert foundry.check() == sat
def test_cfValidWorksOnInvalid(foundry, cantus, maxCount): badSpecs = getAllBadSpecs(cantus, maxCount) for badSpec in badSpecs: foundry = Foundry(Optimize()).applySpec(badSpec) cantus = foundry.extractPitches(badSpec.line) assert checkCF(cantus).isValid() == False
def workingCF(cantus): f = Foundry(Optimize()) f.applySpec(cantus) return f.extractPitches(cantus.line)
def test_goodCFIsS1Sat(s1GoodCF): foundry = Foundry(Optimize()) foundry.applySpec(s1GoodCF) assert foundry.check() == sat
def foundry(opt): return Foundry(opt)