Pergunta

I'm implementing a solution for Graph Coloring problem using Microsoft Solver Foundation and F#. Recently I've found this piece of documentation:

http://msdn.microsoft.com/en-us/library/ff826354(v=vs.93).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

which explains how to implement this using C#. I tried to translate this, but there's a problem with adding constraints. In C# it's quite simple:

model.AddConstraints("borders", 
  be != de, be != fr, be != nl, de != fr, de != nl);

However a literate translation to F# doesn't work:

// WRONG !!
model.AddConstraints("borders", 
  be <> de, be <> fr, be <> nl, de <> fr, de <> nl);

After a while I found a function in Term class, which might be used instead:

model.AddConstraints("borders1", Term.op_Inequality(be, de)) |> ignore

However, using this "Term.op_Inequality" is quite inconvenient. Is there a way to solve my problem better ?

Foi útil?

Solução

A quick fix is to redefine != operator. It will override other != operators so it's best to keep it in the smallest scope possible.

For example, I would keep it in a solve function:

let solve args =
    ...
    let (!=) (x: Decision) (y: Decision) = Term.op_Inequality(x, y)

    model.AddConstraints("borders", 
          be != de, be != fr, be != nl, de != fr, de != nl) |> ignore
    ...

If you need multiple overloads of !=, you should wrap Decision in an F# discriminated unions and define all operators in F#'s way.

I use the quick fix above on the Who owns the Zebra example with MSF. The code is available here, which might be helpful for you.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top