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

Skip to content

ordering instances with constraints on classes #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gabriel-vanzandycke opened this issue May 7, 2021 · 0 comments
Open

ordering instances with constraints on classes #63

gabriel-vanzandycke opened this issue May 7, 2021 · 0 comments

Comments

@gabriel-vanzandycke
Copy link

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant