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

Skip to content

ordering instances with constraints on classes #63

Open
@gabriel-vanzandycke

Description

@gabriel-vanzandycke

I'm trying to order instances based on a list of classes that should occur before and other that should occur after.

This raises an exception saying that the classes cannot be compared with "<", but I'd like to assign them index.

How should the library be used in such case?

Thanks

import constraint as cst
p = cst.Problem()

class A():
    after = []
    before = ["B"]
class B():
    after = ["A"]
    before = ["C"]
class C():
    after = ["B"]
    before = []

l = [A(), B(), C()]

p.addVariables(l, range(len(l)))
for e in l:
    for other_e in l:
        for a in e.after:
            if a in [c.__name__ for c in other_e.__class__.__mro__]:
                p.addConstraint(lambda x,y: x > y, [e, other_e])
        for b in e.after:
            if b in [c.__name__ for c in other_e.__class__.__mro__]:
                p.addConstraint(lambda x,y: x < y, [e, other_e])
p.getSolution()

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