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.

    using Microsoft.SolverFoundation.Common;
    using 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.

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

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

    Domain wheelType = Domain.Enum("AllSeason", "Snow", "OffRoad");
    Domain bodyType = Domain.Enum("sedan", "coupe", "convertible");
    Domain transmissionType = Domain.Enum("auto", "manual");
    Domain engineType = 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.

    Decision wheelKind =
      new Decision(wheelType, "wheelKind");
    Decision tireSize =
      new Decision(Domain.IntegerRange(15, 21), "tireSize");
    Decision rimSize =
      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.

    Decision engine = new Decision(engineType, "engine");
    Decision body = new Decision(bodyType, "body");
    Decision transmission = 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.

    SubmodelInstance[] wheels = new SubmodelInstance[4];
    Decision[] wheelKinds = new Decision[4];
    for (int i = 0; i < 4; i++) {
      wheels[i] = wheelModel.CreateInstance("wheels_" + i.ToString());
      wheelKinds[i] = wheels[i][wheelKind];
    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.

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

    Solution solution = context.Solve();
    Report report = solution.GetReport();
    Console.Write("{0}", report);
  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



    Algorithm: Any

    Variable Selection: Any

    Value Selection: Any

    Move Selection: Any

    Backtrack Count: 0

    ===Solution Details===



    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

© 2015 Microsoft