How to: Create Submodels to Solve a Model
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
-
Create a Console Application named SFSSubmodel.
-
Add a reference to Microsoft Solver Foundation on the .NET tab.
-
Add the following Imports or using statements to the top of the Program code file.
-
In the Main method, add the following code to get the context environment for a solver and create a new model.
-
Create the submodel for a car and another submodel for the wheels of the car.
-
Define the domains for the type of wheels, body, and transmissions that are allowed for the car configurations.
-
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)
-
Create three decisions for the type of engine, body, and transmission. Then, add the decisions to the car model.
-
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))
-
Instantiate a car model and add a constraint that the engine must have 8 cylinders and the wheel type must be offroad.
-
Solve the model and get the report.
-
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