Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error in ABC example #65

Open
Open
@kai-pischke

Description

@kai-pischke

The code in examples/abc.py has a bug. The minvalue variable is actually never updated in the loop so it returns the wrong solution. The optimal solution to the problem is actually a=1, b=9, c=9 which achieves a value of 199/19=10.47 (better than 999/27=37) .

def solve():
    problem = Problem()
    problem.addVariables("abc", range(1, 10))
    problem.getSolutions()
    minvalue = 999 / (9 * 3)
    minsolution = {}
    for solution in problem.getSolutions():
        a = solution["a"]
        b = solution["b"]
        c = solution["c"]
        value = (a * 100 + b * 10 + c) / (a + b + c)
        if value < minvalue:
            minsolution = solution
    return minvalue, minsolution

Easy fix. Just need to remember to update minvalue.

if value < minvalue:
    minvalue = value
    minsolution = solution

The test case has the wrong solution too so will need to update that as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions