#parser #xml #csp #combinatorial #xcsp3

xcsp3-rust

xcsp3-rust is a library that help constraint programming solvers implemented by Rust to read XCSP3 files

1 unstable release

0.1.0 Sep 14, 2023

#1538 in Algorithms

MIT license

380KB
6K SLoC

xcsp3-rust

xcsp3 xcsp3rust docs.rs MSRV License

Description

This lib is implemented by rust and is licensed under the MIT license.

The purpose of this library is to read XCSP files into rust constraint programming solvers.

You can learn about the semantics of XCSP3 through this site http://xcsp.org/.

I will keep improving this code to support more constraints and fix possible bugs.

If you have something to tell me, feel free to contact me.

Usage

Just add the following dependency to your project's Cargo.toml file:

[dependencies]
xcsp3-rust = "0.1.0"

The library is automatically built and statically linked to your binary.

Example

    fn main()
    {
        let xml_file = ".//instances//my-example.xml";
        let model = XcspXmlModel::from_path(xml_file).unwrap();
        let variable = model.build_variables();
        println!("variables:");
        for v in variable.iter() {
            println!("\t{}", v);
            match v {
                XVariableType::XVariableNone(_) => {}
                XVariableType::XVariableArray(_) => {}
                XVariableType::XVariableInt(_) => {}
                XVariableType::XVariableTree(_) => {}
            }
        }
        println!("constraints:");
        for c in model.build_constraints(&variable).iter_mut() {
            println!("\t{}", c);
            match c {
                XConstraintType::XConstraintNone(_) => {}
                XConstraintType::XExtension(_) => {}
                XConstraintType::XAllDifferent(_) => {}
                XConstraintType::XAllDifferentExcept(_) => {}
                XConstraintType::XInstantiation(_) => {}
                XConstraintType::XAllEqual(_) => {}
                XConstraintType::XOrdered(_) => {}
                XConstraintType::XRegular(_) => {}
                XConstraintType::XMdd(_) => {}
                XConstraintType::XIntention(_) => {}
                XConstraintType::XGroup(_) => {}
                XConstraintType::XSum(_) => {}
                XConstraintType::XMaximum(_) => {}
                XConstraintType::XMinimum(_) => {}
                XConstraintType::XElement(_) => {}
                XConstraintType::XSlide(_) => {}
                XConstraintType::XCount(_) => {}
                XConstraintType::XNValues(_) => {}
                XConstraintType::XCardinality(_) => {}
                XConstraintType::XChannel(_) => {}
                XConstraintType::XCumulative(_) => {}
                XConstraintType::XNoOverlap(_) => {}
                XConstraintType::XStretch(_) => {}
            }
        }
        println!("objectives:");
        for o in model.build_objectives(&variable).iter() {
            println!("\t{}", o);
            match o {
                XObjectivesType::XObjectiveNone(_) => {}
                XObjectivesType::Minimize(e) => match e {
                    XObjective::XObjectiveElement(_) => {}
                    XObjective::XObjectiveExpression(_) => {}
                },
                XObjectivesType::Maximize(e) => match e {
                    XObjective::XObjectiveElement(_) => {}
                    XObjective::XObjectiveExpression(_) => {}
                },
            }
        }
    }

Architecture Graph

main architecture

graph BT
A["XCSP(xml file)"] --serde--> B(XcspXmlModel)
B --parser--> C([XVariableSet])
B --parser--> D([XConstraintSet])
B --parser--> E([XObjectivesSet])
C --reader--> F[/example.rs/]
D --reader--> F
E --reader--> F

XVariableSet

graph LR
    C([XVariableSet]) -.->XVariableType(XVariableType)
    XVariableType -->  XVariableArray(XVariableArray)
    XVariableType -->  XVariableInt(XVariableInt)
    XVariableType -->  XVariableTree(XVariableTree)
    XVariableTree -.domain.->  XDomainInteger(XDomainInteger)
    XVariableInt -.domain.->  XDomainInteger
    XVariableArray -.domain.->  XDomainInteger
    XDomainInteger -.-> XIntegerType(XIntegerType)
    XIntegerType -->IntegerValue(IntegerValue)
    XIntegerType -->IntegerInterval(IntegerInterval)
    XIntegerType -->XIntegerSymbolic(XIntegerSymbolic)

XConstraintSet

graph LR
    D([XConstraintSet]) -.-> XConstraintType(XConstraintType)
    XConstraintType -->  XExtension(XExtension) -.scope.-> Scope(XVarVal)
    XConstraintType --> XAllDifferent(XAllDifferent)-.scope.-> Scope
    XConstraintType --> XAllDifferentExcept(XAllDifferentExcept)-.scope.-> Scope
    XConstraintType --> XInstantiation(XInstantiation)-.scope.-> Scope
    XConstraintType --> XAllEqual(XAllEqual)-.scope.-> Scope
    XConstraintType --> XOrdered(XOrdered)-.scope.-> Scope
    XConstraintType --> XRegular(XRegular)-.scope.-> Scope
    XConstraintType -->XMdd(XMdd)-.scope.-> Scope
    XConstraintType -->XIntention(XIntention)-.scope.-> Scope
    XConstraintType -->XGroup(XGroup)-.scope.-> Scope
    XConstraintType -->XSum(XSum)-.scope.-> Scope
    XConstraintType -->XMaximum(XMaxMin)-.scope.-> Scope
    XConstraintType -->XMinimum(XMaxMin)-.scope.-> Scope
    XConstraintType -->XElement(XElement)-.scope.-> Scope
    XConstraintType -->XSlide(XSlide)-.scope.-> Scope
    XConstraintType -->XCount(XCount)-.scope.-> Scope
    XConstraintType -->XNValues(XNValues)-.scope.-> Scope
    XConstraintType -->XCardinality(XCardinality)-.scope.-> Scope
    XConstraintType -->XChannel(XChannel)-.scope.-> Scope
    XConstraintType -->XCumulative(XCumulative)-.scope.-> Scope
    XConstraintType -->XNoOverlap(XNoOverlap)-.scope.-> Scope
    XConstraintType -->XNoOverlapKDim(XNoOverlap)-.scope.-> Scope
    XConstraintType --> XStretch(XStretch)-.scope.-> Scope
    Scope -->IntVar(IntVar is a variable)
    Scope -->IntVal(IntVal is a value)

XObjectivesSet

graph LR
    E([XObjectivesSet]) -.-> T(XObjectivesType)
%%    XVariableArray(XVariableArray)
    T --> Minimize(Minimize)
    T --> Maximize(Maximize)
    Minimize --> XObjective(XObjective)
    Maximize --> XObjective(XObjective)
    XObjective --> XObjectiveElement(XObjectiveElement)
    XObjective --> XObjectiveExpression(XObjectiveExpression)

License

MIT License

Author

luhan zhen

tip: Maybe my code is not the best, but I will keep improving it to better build our 'CP' community.

Dependencies

~4–12MB
~133K SLoC