Skip to content

IndexError when finding no solutions while using the --intermediate flag #201

@kristofferNorr

Description

@kristofferNorr

I think I found a bug which occurs when using the flags --intermediate, and not finding any solutions. objective(self) in src/minizinc/result.py picks out the last element in a list inside getattr-function. I think this causes a bug as when we have no solution, solve() in src/minizinc/instance.py returns an empty list which results in IndexError: list index out of range.

Here is the code in result.py, I think a simple if-statement is enough to fix it.

    @property
    def objective(self) -> Optional[Union[int, float]]:
        """Returns objective of the solution

        Returns the objective of the solution when possible. If no solutions
        have been found or the problem did not have an objective, then None is
        returned instead.

        Returns:
            Optional[Union[int, float]]: best objective found or None

        """
        if self.solution is not None:
            if isinstance(self.solution, list):
                return getattr(self.solution[-1], "objective", None) # <----- here is the bug
            else:
                return getattr(self.solution, "objective", None)
        else:
            return None

Please let me know if I have just misunderstood something (and it is not an actual bug).

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