Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to: Create Submodels to Solve a Model

Solver Foundation 3.0

You can create a model hierarchy by using submodels to model a complex problem. The advantage of using submodels is that you can add further goals and constraints to define submodels. The following example demonstrates how to use models and submodels to simulate a car. First, you create a car model, which has an engine, transmission, and four wheels. Next, you create a wheel submodel that requires four identical wheels in a car model, and each wheel has a rim and a tire that must match in size.

To create submodels in a model

  1. Create a Console Application named SFSSubmodel.

  2. Add a reference to Microsoft Solver Foundation on the .NET tab.

  3. Add the following Imports or using statements to the top of the Program code file.

    
    Imports Microsoft.SolverFoundation.Common
    Imports Microsoft.SolverFoundation.Services
    
    
    
  4. In the Main method, add the following code to get the context environment for a solver and create a new model.

    
    Dim context As SolverContext = SolverContext.GetContext()
    Dim defaultModel As Model = context.CreateModel()
    
    
    
  5. Create the submodel for a car and another submodel for the wheels of the car.

    
    Dim carModel As Model = defaultModel.CreateSubModel("carModel")
    Dim wheelModel As Model = carModel.CreateSubModel("wheelModel")
    
    
    
  6. Define the domains for the type of wheels, body, and transmissions that are allowed for the car configurations.

    
    Dim wheelType As Domain = Domain.[Enum]("AllSeason", "Snow", "OffRoad")
    Dim bodyType As Domain = Domain.[Enum]("sedan", "coupe", "convertible")
    Dim transmissionType As Domain = Domain.[Enum]("auto", "manual")
    Dim engineType As Domain = Domain.[Set](4, 6, 8)
    
    
    
  7. Create three decisions for the type of wheel, the size of the tire, and the size of the rim. Next, add these decisions to the wheel submodel. Then, add a constraint to the wheel model that stipulates that the size of the tire must be equal to the size of the rim.

    
    Dim wheelKind As New Decision(wheelType, "wheelKind")
    Dim tireSize As New Decision(Domain.IntegerRange(15, 21), "tireSize")
    Dim rimSize As New Decision(Domain.IntegerRange(15, 21), "rimSize")
    wheelModel.AddDecisions(wheelKind, tireSize, rimSize)
    wheelModel.AddConstraint("SizeMatching", tireSize = rimSize)
    
    
    
  8. Create three decisions for the type of engine, body, and transmission. Then, add the decisions to the car model.

    
    Dim engine As New Decision(engineType, "engine")
    Dim body As New Decision(bodyType, "body")
    Dim transmission As New Decision(transmissionType, "transmission")
    carModel.AddDecisions(engine, body, transmission)
    
    
    
  9. Create four instances of the wheel submodel and four decisions about the wheel type. Assign an instance of the wheel model to a wheel type decision. Then, add a constraint to the car model that the wheel decisions must be identical.

    
    Dim wheels As SubmodelInstance() = New SubmodelInstance(3) {}
    Dim wheelKinds As Decision() = New Decision(3) {}
    For i As Integer = 0 To 3
        wheels(i) = wheelModel.CreateInstance("wheels_" & i.ToString())
        wheelKinds(i) = wheels(i)(wheelKind)
    Next
    carModel.AddConstraints("KindMatching", Model.Equal(wheelKinds))
    
    
    
  10. Instantiate a car model and add a constraint that the engine must have 8 cylinders and the wheel type must be offroad.

    
    Dim myCar As SubmodelInstance = carModel.CreateInstance("CompactCar")
    defaultModel.AddConstraint("EngineChoice", myCar(engine) = 8)
    defaultModel.AddConstraint("WheelChoice", Model.Implies(myCar(engine) >= 6, myCar(wheels(0))(wheelKind) = "OffRoad"))
    
    
    
  11. Solve the model and get the report.

    
    Dim solution As Solution = context.Solve()
    
    Dim report As Report = solution.GetReport()
    Console.Write("{0}", report)
    Console.ReadLine()
    
    
    
  12. Press F5 to build and run the code.

    The command window shows the following results.

    ===Solver Foundation Service Report===

    Date: Date

    Version: Version

    Model Name: Default

    Capabilities Applied: CP

    Solve Time (ms): 89

    Total Time (ms): 262

    Solve Completion Status: Feasible

    Solver Selected: Microsoft.SolverFoundation.Solvers.ConstraintSystem

    Directives:

    Microsoft.SolverFoundation.Services.Directive

    Algorithm: Any

    Variable Selection: Any

    Value Selection: Any

    Move Selection: Any

    Backtrack Count: 0

    ===Solution Details===

    Goals:

    Decisions:

    CompactCar.wheels_0.wheelKind: OffRoad

    CompactCar.wheels_0.tireSize: 15

    CompactCar.wheels_0.rimSize: 15

    CompactCar.wheels_1.wheelKind: OffRoad

    CompactCar.wheels_1.tireSize: 15

    CompactCar.wheels_1.rimSize: 15

    CompactCar.wheels_2.wheelKind: OffRoad

    CompactCar.wheels_2.tireSize: 15

    CompactCar.wheels_2.rimSize: 15

    CompactCar.wheels_3.wheelKind: OffRoad

    CompactCar.wheels_3.tireSize: 15

    CompactCar.wheels_3.rimSize: 15

    CompactCar.engine: 8

Show:
© 2015 Microsoft