0

Trying to create a Product Configurator using Google OR tools. I got a few examples working, but cant figure out how to create string domains and access their values.

var colorValues = new string[] { "Red", "Green", "Blue" };
var sizeValues = new string[] { "Small", "Medium", "Large" };
IntVar color = model.NewIntVar(0, colorValues.Length - 1, "color");
IntVar size = model.NewIntVar(0, sizeValues.Length - 1, "size");

I need to create a constraint like,

  • If Color is Red then Size is Small
  • If Color is Blue then Size is not Large

How can I do this using

model.add(....)

Or some other method using Google OR Tools.

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
Sunny
  • 932
  • 8
  • 22

1 Answers1

0

I stumbled across the same problem and there is a documentation about this. The method you are looking for is channeling: https://developers.google.com/optimization/cp/channeling

It boils down to this (using a bool as the commentors said):

    // Declare our two primary variables.
    IntVar x = model.NewIntVar(0, 10, "x");
    IntVar y = model.NewIntVar(0, 10, "y");

    // Declare our intermediate boolean variable.
    BoolVar b = model.NewBoolVar("b");

    // Implement b == (x >= 5).
    model.Add(x >= 5).OnlyEnforceIf(b);
    model.Add(x < 5).OnlyEnforceIf(b.Not());

    // Create our two half-reified constraints.
    // First, b implies (y == 10 - x).
    model.Add(y == 10 - x).OnlyEnforceIf(b);
    // Second, not(b) implies y == 0.
    model.Add(y == 0).OnlyEnforceIf(b.Not());
Benjamin
  • 68
  • 7