示例#1
0
    def __init__(self, argProblemString, argOptions):
        self.options = argOptions
        # First load data common to all problems
        print("  Builder constructor")
        self.problemItems = argProblemString.rstrip('\n').split('|')
        # print( self.problemItems )
        self.problemName = self.problemItems[0].rpartition('/')[-1]
        print("    Building {0}-problem: {1}".format(
            self.options.currentProblemType, self.problemName))

        # Then load problem specific data or fail, if an unknown problem was given
        if self.options.currentProblemType == "QAP":
            assert len(self.problemItems) == 4
            self.problemSize = int(self.problemItems[1])
            self.flowMatrix = Matrix.FromSSV(self.problemItems[2],
                                             self.problemSize,
                                             self.problemSize)
            # print( self.flowMatrix )
            self.distanceMatrix = Matrix.FromSSV(self.problemItems[3],
                                                 self.problemSize,
                                                 self.problemSize)
            # print( self.distanceMatrix )
        else:
            raise ValueError("Invalid problem type encountered in 'Builder'")
        print("      Problem size: {0}".format(self.problemSize))


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
示例#2
0
文件: tests.py 项目: markuspg/PrSi
def main():
    # Check for correct building of matrices
    builder = Builder(
        "test|3|1;2;3;4;5;6;7;8;9|1;10;100;1000;10000;1000;100;10;1", "QAP")
    assert builder.flowMatrix.matrix == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    assert builder.distanceMatrix.matrix == [[1, 10, 100], [1000, 10000, 1000],
                                             [100, 10, 1]]
    print("[CHECK] Matrices were successfully constructed")

    # Check if builder fails if an unknown problem was encountered
    try:
        builder = Builder(
            "test|3|1;2;3;4;5;6;7;8;9|1;10;100;1000;10000;1000;100;10;1",
            "FAP")
    except ValueError:
        print("[CHECK] Unknown problem was successfully caught")

    # Check for correct creation of QAP
    builder = Builder(
        "test|3|1;2;3;4;5;6;7;8;9|1;10;100;1000;10000;1000;100;10;1", "QAP")
    qap = QAP(builder)
    assert qap.problemName == "test"
    assert qap.problemSize == 3
    assert qap.flowMatrix.matrix == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    assert qap.distanceMatrix.matrix == [[1, 10, 100], [1000, 10000, 1000],
                                         [100, 10, 1]]
    print("[CHECK] QAP was successfully constructed")

    # Check for correct objective function value calculation
    assert qap.CalculateObjectiveValue(
        [1, 2, 3]
    ) == 50010, "Objective value calculation failed for assignment '[ 1, 2, 3 ]'"
    assert qap.CalculateObjectiveValue(
        [3, 2, 1]
    ) == 51000, "Objective value calculation failed for assignment '[ 3, 2, 1 ]'"
    assert qap.CalculateObjectiveValue(
        [1, 3, 2]
    ) == 6081, "Objective value calculation failed for assignment '[ 1, 3, 2 ]'"
    print("[CHECK] All QAP objective value calculations where correct")

    assert qap.ConvertRandomKeysToSolution([0.05, 0.91, 0.95]) == [
        1, 2, 3
    ], "Random key conversion failed for an equivalent of '[ 1, 2, 3 ]'"
    assert qap.ConvertRandomKeysToSolution([0.05, 0.66, 0.65]) == [
        1, 3, 2
    ], "Random key conversion failed for an equivalent of '[ 1, 3, 2 ]'"
    assert qap.ConvertRandomKeysToSolution([0.99, 0.5, 0.0001]) == [
        3, 2, 1
    ], "Random key conversion failed for an equivalent of '[ 3, 2, 1 ]'"
    assert qap.ConvertRandomKeysToSolution([0.5, 0.5, 0.0001]) == [
        2, 3, 1
    ], "Random key conversion failed for an equivalent of '[ 2, 3, 1 ]'"
    print(
        "[CHECK] Converting QAP random keys to solutions yielded valid results"
    )

    testRandomKeysSolution = qap.CreateRandomRandomKeys()
    assert len(
        testRandomKeysSolution
    ) == qap.problemSize, "Random random keys solution vector has an invalid size"
    for item in testRandomKeysSolution:
        assert isinstance(
            item, float
        ), "Wrong type of element in random random keys solution vector"

    # Test the Matrix class using the construction method from parameters
    testmatrix = Matrix.FromParameters(None, 3, 5)
    # Check the amount of rows
    assert len(testmatrix.matrix) == 3, "Wrong quantity of rows"
    # Check the amount of columns
    assert len(testmatrix.matrix[0]) == 5, "Wrong quantity of columns"
    # Test the Matrix class using the construction method from semicolon separated values
    testmatrix = Matrix.FromSSV("1;2;3;4;5;6;7;8;9;10;11;12", 3, 4)
    # Check the amount of rows
    assert len(testmatrix.matrix) == 3, "Wrong quantity of rows"
    # Check the amount of columns
    assert len(testmatrix.matrix[0]) == 4, "Wrong quantity of columns"
    assert testmatrix.GetValue(1, 0) == 5, "Wrong value in matrix"
    assert testmatrix.GetValue(2, 3) == 12, "Wrong value in matrix"
    # Test the Matrix class using the standard constructor
    testmatrix = Matrix([[10, 8, 31], [14, 25, 6], [72, 85, 19], [10, 10, 12]])
    # Re-check the amount of rows
    assert len(testmatrix.matrix) == 4, "Wrong quantity of rows"
    # Re-check the amount of columns
    assert len(testmatrix.matrix[0]) == 3, "Wrong quantity of columns"
    assert testmatrix.GetValue(0, 0) == 10
    assert testmatrix.GetValue(2, 1) == 85
    # print( testmatrix )
    testmatrix.SetValue(1, 2, 48)
    # print( testmatrix )
    assert testmatrix.GetValue(1, 2) == 48
    assert testmatrix.GetMinimumValue() == (8, 0, 1)
    print("[CHECK] Matrix tests completed successfully")

    return 0