Ejemplo n.º 1
0
def convert_task(base_system: RestrictionSystem, divider: ObjectiveFunction):
    if len(base_system.__objective_function__.coefficients) != len(divider.coefficients):
        return None

    base_system = base_system.to_canonical_form()
    divider = copy.deepcopy(divider)
    new_variable_count  = len(base_system.__objective_function__.coefficients)
    new_variable_count -= len(divider.coefficients)
    for _ in range(new_variable_count):
        divider.coefficients.append(0)

    system = RestrictionSystem()
    function = system.__objective_function__
    for _ in range(len(divider.coefficients)):
        system.add_variable(Variable())
    function.purpose = Purpose.MAX
    function.coefficients = copy.deepcopy(base_system.__objective_function__.coefficients)
    function.coefficients.append(base_system.__objective_function__.free_member)
    function.free_member = 0

    for base_row in base_system.__rows__:
        row = system.create_restriction()
        row.coefficients = copy.deepcopy(base_row.coefficients)
        row.coefficients.append(-base_row.free_member)
        row.free_member = 0

    row = system.create_restriction()
    row.coefficients = copy.deepcopy(divider.coefficients)
    row.coefficients.append(divider.free_member)
    row.free_member = 1

    return system
Ejemplo n.º 2
0
def __create_branches(table: SimplexTable):
    if integer_check(table):
        return []
    result = []
    base_system = create_restriction_system(table)

    for i in range(len(base_system.__rows__)):
        row = base_system.__rows__[i]
        basis = table.__basis__[i]
        value = round(row.free_member, 7)
        fraction = value - math.floor(value)
        if fraction < 1e-7 or value < 0:
            continue

        system = copy.deepcopy(base_system)
        new_row = system.create_restriction()
        new_row.free_member = math.floor(value)
        new_row.sign = Sign.LESS_OR_EQUAL
        new_row.coefficients = [
            1 if basis == j else 0 for j in range(len(row.coefficients))
        ]

        result.append((copy.deepcopy(system), basis))
        new_row.free_member += 1
        new_row.sign = Sign.MORE_OR_EQUAL
        result.append((system, basis))

    return result
Ejemplo n.º 3
0
def create_branches(elem: TreeElem, req_variables: List[int]):
    if integer_check(elem.table):
        return []
    if not elem.__valid_solution__():
        return []

    result = []
    base_system = create_restriction_system(elem.table)

    for i in range(len(base_system.__rows__)):
        row = base_system.__rows__[i]
        basis = elem.table.__basis__[i]
        value = round(row.free_member, 7)
        if basis is None:
            return []
        if basis not in req_variables:
            continue

        lower_bound = elem.variables[basis].lower_bound
        if lower_bound is not None:
            if lower_bound > value:
                continue

        upper_bound = elem.variables[basis].upper_bound
        if upper_bound is not None:
            if upper_bound < value:
                continue

        fraction = value - math.floor(value)
        if fraction < 1e-7 or value < 0:
            continue

        system = copy.deepcopy(base_system)
        new_row = system.create_restriction()
        new_row.free_member = math.floor(value)
        new_row.sign = Sign.LESS_OR_EQUAL
        new_row.coefficients = [
            1 if basis == j else 0 for j in range(len(row.coefficients))
        ]

        new_elem = TreeElem(system)
        for i in range(len(elem.variables)):
            new_elem.variables[i] = copy.deepcopy(elem.variables[i])
        new_elem.variables[basis].upper_bound = new_row.free_member
        if new_elem.__valid_solution__():
            result.append(new_elem)

        new_row.free_member += 1
        new_row.sign = Sign.MORE_OR_EQUAL
        new_elem = TreeElem(system)
        for i in range(len(elem.variables)):
            new_elem.variables[i] = copy.deepcopy(elem.variables[i])
        new_elem.variables[basis].lower_bound = new_row.free_member
        if new_elem.__valid_solution__():
            result.append(new_elem)

    return result
Ejemplo n.º 4
0
def system_prepare(system: RestrictionSystem):
    rows: List[RestrictionRow] = []
    for v in system.__rows__:
        if v.sign == Sign.EQUALS:
            rows.append(v)
            v.sign = Sign.LESS_OR_EQUAL
    for v in rows:
        row = system.create_restriction()
        row.coefficients = [-value for value in v.coefficients]
        row.free_member = -v.free_member
        row.sign = Sign.LESS_OR_EQUAL
Ejemplo n.º 5
0
def create_slice(table: SimplexTable):
    system = create_restriction_system(table)
    row = __find_best_fraction__(table)
    if row is None:
        return system

    new_row = system.create_restriction()
    for i in range(len(row.coefficients)):
        new_row.coefficients.append(__fraction_part__(row.coefficients[i]))
    new_row.free_member = __fraction_part__(row.free_member)
    new_row.sign = Sign.MORE_OR_EQUAL

    return system
Ejemplo n.º 6
0
def create_restriction_system(table: SimplexTable):
    system = RestrictionSystem()
    for _ in range(len(table.__objective_function__.coefficients)):
        system.add_variable(Variable())

    for row in table.__rows__:
        system_row = system.create_restriction()
        system_row.free_member = row.free_member
        system_row.coefficients = copy.deepcopy(row.coefficients)
        system_row.sign = Sign.EQUALS
    function = system.__objective_function__
    function.coefficients = [
        -v for v in table.__objective_function__.coefficients
    ]
    function.free_member = -table.__objective_function__.free_member
    function.purpose = Purpose.MAX

    return system
Ejemplo n.º 7
0
# row.coefficients = [10, 3, 1, 0, 0]
# row.free_member = 93

# row = system.create_restriction()
# row.coefficients = [14, -5, 0, -1, 0]
# row.free_member = 26

# row = system.create_restriction()
# row.coefficients = [2, -9, 0, 0, -1]
# row.free_member = 18
#-----------------------------------------
    function.free_member = 0
    function.coefficients = [1, 2, -1, 0, 0]
    function.purpose = Purpose.MAX

    row = system.create_restriction()
    row.coefficients = [7, 4, 1, 0, 0]
    row.free_member = 25

    row = system.create_restriction()
    row.coefficients = [3, -4, 0, 1, 0]
    row.free_member = 10

    row = system.create_restriction()
    row.coefficients = [-1, 3, 0, 0, 1]
    row.free_member = 3
    #-----------------------------------------
    # function.free_member = 0
    # function.coefficients = [10, -5, 0, 4, 0]
    # function.purpose = Purpose.MAX