organize examples

This commit is contained in:
acco32
2018-07-05 17:51:49 -07:00
parent 0a822b6617
commit 6ab609f17b
265 changed files with 167 additions and 19598 deletions

View File

@@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "examples", "csharp\examples.csproj", "{0899C5EB-2AD1-49C1-9AB3-735E5B81BF56}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharp", "csharp\csharp.csproj", "{0899C5EB-2AD1-49C1-9AB3-735E5B81BF56}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "examples", "fsharp\fsharp.fsproj", "{B0E80F2F-BDB3-4688-8E27-5678173DC28C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -30,5 +32,17 @@ Global
{0899C5EB-2AD1-49C1-9AB3-735E5B81BF56}.Release|x64.Build.0 = Release|x64
{0899C5EB-2AD1-49C1-9AB3-735E5B81BF56}.Release|x86.ActiveCfg = Release|x86
{0899C5EB-2AD1-49C1-9AB3-735E5B81BF56}.Release|x86.Build.0 = Release|x86
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|x64.ActiveCfg = Debug|x64
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|x64.Build.0 = Debug|x64
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|x86.ActiveCfg = Debug|x86
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Debug|x86.Build.0 = Debug|x86
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|Any CPU.Build.0 = Release|Any CPU
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|x64.ActiveCfg = Release|x64
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|x64.Build.0 = Release|x64
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|x86.ActiveCfg = Release|x86
{B0E80F2F-BDB3-4688-8E27-5678173DC28C}.Release|x86.Build.0 = Release|x86
EndGlobalSection
EndGlobal

View File

@@ -10,12 +10,12 @@ Wherever you have ortools installed, be sure to reference the `Google.OrTools.dl
To reference a particular folder on linux, you can either: explicitly set the **LD_LIBRARY_PATH**; or create a new configuration file with the path of the library folder in `/etc/ld.so.conf.d/` and then run `sudo ldconfig`. The former will set the path on a system level so that you don't have to use the environment.
### MacOS
To reference a particular folder on linux, you can explicitly set the **DYLD_FALLBACK_LIBRARY_PATH**
To reference a particular folder on linux, you can explicitly set the **DYLD_LIBRARY_PATH**
## CSharp
## CSharp/FSharp project examples
By default all the examples are compiled in a console applicaiton with the startup object being the **Classname.Main** so that when compiled the entrypoint will be known.
## FSharp
## NetFx/FSharp compiler examples
TBD
Should you have another **netfx** you can compile individual file examples. Please see readme in the individual folders for the target language.

View File

@@ -0,0 +1,34 @@
# Examples of using or-tools in C#
This file describes how to use the or-tools .NET binary archive in C#
## Execution
Running the examples will involve compiling them, then running them. You can run the following command for your target operating system.
For example you can compile and run `csflow.cs`. This assumes you have the archive library in a folder called `bin`.
### Windows (32 bit)
```
csc /target:exe /out:bin\csflow.exe /platform:x86 /lib:bin /r:Google.OrTools.dll examples\csharp\csflow.cs
bin\csflow.exe
```
### Windows (64 bit)
```
csc /target:exe /out:bin/csflow.exe /platform:x64 /lib:bin /r:Google.OrTools.dll examples\csharp\csflow.cs
bin\csflow.exe
```
### Linux (framework 4.6+ via mono must be installed)
```
mcs /target:exe /out:bin/csflow.exe /platform:anycpu /lib:bin /r:Google.OrTools.dll examples/csharp/csflow.cs
mono bin/csflow.exe
```
### Mac OS X (framework 4.6+ via mono must be installed)
```
mcs /target:exe /out:bin/csflow.exe /platform:anycpu /lib:bin /r:Google.OrTools.dll examples/csharp/csflow.cs
DYLD_FALLBACK_LIBRARY_PATH=lib mono64 bin/csflow.exe
```

View File

@@ -1,201 +1,201 @@
using System;
using System.Collections.Generic;
using Google.OrTools.ConstraintSolver;
namespace OrToolsConstraint {
class Job {
public Job(List<Task> tasks) {
AlternativeTasks = tasks;
}
public Job Successor { get; set; }
public List<Task> AlternativeTasks { get; set; }
}
class Task {
public Task(string name, long duration, long equipment) {
Name = name;
Duration = duration;
Equipment = equipment;
}
public string Name {get; set;}
public long StartTime {get; set;}
public long EndTime {
get {
return StartTime + Duration;
}
}
public long Duration {get; set;}
public long Equipment { get; set; }
public override string ToString() {
return Name + " [ " + Equipment + " ]\tstarts: " + StartTime + " ends:" +
EndTime + ", duration: " + Duration;
}
}
class Prefix : VoidToString
{
public override string Run()
{
return "[TaskScheduling] ";
}
}
class TaskScheduling {
public static List<Job> myJobList = new List<Job>();
public static Dictionary<long, List<IntervalVar>> tasksToEquipment =
new Dictionary<long, List<IntervalVar>>();
public static Dictionary<string, long> taskIndexes =
new Dictionary<string, long>();
public static void InitTaskList() {
List<Task> taskList = new List<Task>();
taskList.Add(new Task("Job1Task0a", 15, 0));
taskList.Add(new Task("Job1Task0b", 25, 1));
taskList.Add(new Task("Job1Task0c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job1Task1a", 25, 0));
taskList.Add(new Task("Job1Task1b", 30, 1));
taskList.Add(new Task("Job1Task1c", 40, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job1Task2a", 20, 0));
taskList.Add(new Task("Job1Task2b", 35, 1));
taskList.Add(new Task("Job1Task2c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task0a", 15, 0));
taskList.Add(new Task("Job2Task0b", 25, 1));
taskList.Add(new Task("Job2Task0c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task1a", 25, 0));
taskList.Add(new Task("Job2Task1b", 30, 1));
taskList.Add(new Task("Job2Task1c", 40, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task2a", 20, 0));
taskList.Add(new Task("Job2Task2b", 35, 1));
taskList.Add(new Task("Job2Task2c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task0a", 10, 0));
taskList.Add(new Task("Job3Task0b", 15, 1));
taskList.Add(new Task("Job3Task0c", 50, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task1a", 50, 0));
taskList.Add(new Task("Job3Task1b", 10, 1));
taskList.Add(new Task("Job3Task1c", 20, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task2a", 65, 0));
taskList.Add(new Task("Job3Task2b", 5, 1));
taskList.Add(new Task("Job3Task2c", 15, 2));
myJobList.Add(new Job(taskList));
myJobList[0].Successor = myJobList[1];
myJobList[1].Successor = myJobList[2];
myJobList[2].Successor = null;
myJobList[3].Successor = myJobList[4];
myJobList[4].Successor = myJobList[5];
myJobList[5].Successor = null;
myJobList[6].Successor = myJobList[7];
myJobList[7].Successor = myJobList[8];
myJobList[8].Successor = null;
}
public static int GetTaskCount() {
int c = 0;
foreach (Job j in myJobList)
foreach (Task t in j.AlternativeTasks) {
taskIndexes[t.Name] = c;
c++;
}
return c;
}
public static int GetEndTaskCount() {
int c = 0;
foreach (Job j in myJobList)
if (j.Successor == null)
c += j.AlternativeTasks.Count;
return c;
}
static void Main(string[] args) {
InitTaskList();
int taskCount = GetTaskCount();
Solver solver = new Solver("ResourceConstraintScheduling");
IntervalVar[] tasks = new IntervalVar[taskCount];
IntVar[] taskChoosed = new IntVar[taskCount];
IntVar[] makeSpan = new IntVar[GetEndTaskCount()];
int endJobCounter = 0;
foreach (Job j in myJobList) {
IntVar[] tmp = new IntVar[j.AlternativeTasks.Count];
int i = 0;
foreach (Task t in j.AlternativeTasks) {
long ti = taskIndexes[t.Name];
taskChoosed[ti] = solver.MakeIntVar(0, 1, t.Name + "_choose");
tmp[i++] = taskChoosed[ti];
tasks[ti] = solver.MakeFixedDurationIntervalVar(
0, 100000, t.Duration, false, t.Name + "_interval");
if (j.Successor == null)
makeSpan[endJobCounter++] = tasks[ti].EndExpr().Var();
if (!tasksToEquipment.ContainsKey(t.Equipment))
tasksToEquipment[t.Equipment] = new List<IntervalVar>();
tasksToEquipment[t.Equipment].Add(tasks[ti]);
}
solver.Add(IntVarArrayHelper.Sum(tmp) == 1);
}
List<SequenceVar> all_seq = new List<SequenceVar>();
foreach (KeyValuePair<long, List<IntervalVar>> pair in tasksToEquipment) {
DisjunctiveConstraint dc = solver.MakeDisjunctiveConstraint(
pair.Value.ToArray(), pair.Key.ToString());
solver.Add(dc);
all_seq.Add(dc.SequenceVar());
}
IntVar objective_var = solver.MakeMax(makeSpan).Var();
OptimizeVar objective_monitor = solver.MakeMinimize(objective_var, 1);
DecisionBuilder sequence_phase =
solver.MakePhase(all_seq.ToArray(), Solver.SEQUENCE_DEFAULT);
DecisionBuilder objective_phase =
solver.MakePhase(objective_var, Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
DecisionBuilder main_phase = solver.Compose(sequence_phase, objective_phase);
const int kLogFrequency = 1000000;
VoidToString prefix = new Prefix();
SearchMonitor search_log =
solver.MakeSearchLog(kLogFrequency, objective_monitor, prefix);
SolutionCollector collector = solver.MakeLastSolutionCollector();
collector.Add(all_seq.ToArray());
collector.AddObjective(objective_var);
if (solver.Solve(main_phase, search_log, objective_monitor, null, collector))
Console.Out.WriteLine("Optimal solution = " + collector.ObjectiveValue(0));
else
Console.Out.WriteLine("No solution.");
}
}
}
using System;
using System.Collections.Generic;
using Google.OrTools.ConstraintSolver;
namespace OrToolsConstraint {
class Job {
public Job(List<Task> tasks) {
AlternativeTasks = tasks;
}
public Job Successor { get; set; }
public List<Task> AlternativeTasks { get; set; }
}
class Task {
public Task(string name, long duration, long equipment) {
Name = name;
Duration = duration;
Equipment = equipment;
}
public string Name {get; set;}
public long StartTime {get; set;}
public long EndTime {
get {
return StartTime + Duration;
}
}
public long Duration {get; set;}
public long Equipment { get; set; }
public override string ToString() {
return Name + " [ " + Equipment + " ]\tstarts: " + StartTime + " ends:" +
EndTime + ", duration: " + Duration;
}
}
class Prefix : VoidToString
{
public override string Run()
{
return "[TaskScheduling] ";
}
}
class TaskScheduling {
public static List<Job> myJobList = new List<Job>();
public static Dictionary<long, List<IntervalVar>> tasksToEquipment =
new Dictionary<long, List<IntervalVar>>();
public static Dictionary<string, long> taskIndexes =
new Dictionary<string, long>();
public static void InitTaskList() {
List<Task> taskList = new List<Task>();
taskList.Add(new Task("Job1Task0a", 15, 0));
taskList.Add(new Task("Job1Task0b", 25, 1));
taskList.Add(new Task("Job1Task0c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job1Task1a", 25, 0));
taskList.Add(new Task("Job1Task1b", 30, 1));
taskList.Add(new Task("Job1Task1c", 40, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job1Task2a", 20, 0));
taskList.Add(new Task("Job1Task2b", 35, 1));
taskList.Add(new Task("Job1Task2c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task0a", 15, 0));
taskList.Add(new Task("Job2Task0b", 25, 1));
taskList.Add(new Task("Job2Task0c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task1a", 25, 0));
taskList.Add(new Task("Job2Task1b", 30, 1));
taskList.Add(new Task("Job2Task1c", 40, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job2Task2a", 20, 0));
taskList.Add(new Task("Job2Task2b", 35, 1));
taskList.Add(new Task("Job2Task2c", 10, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task0a", 10, 0));
taskList.Add(new Task("Job3Task0b", 15, 1));
taskList.Add(new Task("Job3Task0c", 50, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task1a", 50, 0));
taskList.Add(new Task("Job3Task1b", 10, 1));
taskList.Add(new Task("Job3Task1c", 20, 2));
myJobList.Add(new Job(taskList));
taskList = new List<Task>();
taskList.Add(new Task("Job3Task2a", 65, 0));
taskList.Add(new Task("Job3Task2b", 5, 1));
taskList.Add(new Task("Job3Task2c", 15, 2));
myJobList.Add(new Job(taskList));
myJobList[0].Successor = myJobList[1];
myJobList[1].Successor = myJobList[2];
myJobList[2].Successor = null;
myJobList[3].Successor = myJobList[4];
myJobList[4].Successor = myJobList[5];
myJobList[5].Successor = null;
myJobList[6].Successor = myJobList[7];
myJobList[7].Successor = myJobList[8];
myJobList[8].Successor = null;
}
public static int GetTaskCount() {
int c = 0;
foreach (Job j in myJobList)
foreach (Task t in j.AlternativeTasks) {
taskIndexes[t.Name] = c;
c++;
}
return c;
}
public static int GetEndTaskCount() {
int c = 0;
foreach (Job j in myJobList)
if (j.Successor == null)
c += j.AlternativeTasks.Count;
return c;
}
static void Main(string[] args) {
InitTaskList();
int taskCount = GetTaskCount();
Solver solver = new Solver("ResourceConstraintScheduling");
IntervalVar[] tasks = new IntervalVar[taskCount];
IntVar[] taskChoosed = new IntVar[taskCount];
IntVar[] makeSpan = new IntVar[GetEndTaskCount()];
int endJobCounter = 0;
foreach (Job j in myJobList) {
IntVar[] tmp = new IntVar[j.AlternativeTasks.Count];
int i = 0;
foreach (Task t in j.AlternativeTasks) {
long ti = taskIndexes[t.Name];
taskChoosed[ti] = solver.MakeIntVar(0, 1, t.Name + "_choose");
tmp[i++] = taskChoosed[ti];
tasks[ti] = solver.MakeFixedDurationIntervalVar(
0, 100000, t.Duration, false, t.Name + "_interval");
if (j.Successor == null)
makeSpan[endJobCounter++] = tasks[ti].EndExpr().Var();
if (!tasksToEquipment.ContainsKey(t.Equipment))
tasksToEquipment[t.Equipment] = new List<IntervalVar>();
tasksToEquipment[t.Equipment].Add(tasks[ti]);
}
solver.Add(IntVarArrayHelper.Sum(tmp) == 1);
}
List<SequenceVar> all_seq = new List<SequenceVar>();
foreach (KeyValuePair<long, List<IntervalVar>> pair in tasksToEquipment) {
DisjunctiveConstraint dc = solver.MakeDisjunctiveConstraint(
pair.Value.ToArray(), pair.Key.ToString());
solver.Add(dc);
all_seq.Add(dc.SequenceVar());
}
IntVar objective_var = solver.MakeMax(makeSpan).Var();
OptimizeVar objective_monitor = solver.MakeMinimize(objective_var, 1);
DecisionBuilder sequence_phase =
solver.MakePhase(all_seq.ToArray(), Solver.SEQUENCE_DEFAULT);
DecisionBuilder objective_phase =
solver.MakePhase(objective_var, Solver.CHOOSE_FIRST_UNBOUND,
Solver.ASSIGN_MIN_VALUE);
DecisionBuilder main_phase = solver.Compose(sequence_phase, objective_phase);
const int kLogFrequency = 1000000;
VoidToString prefix = new Prefix();
SearchMonitor search_log =
solver.MakeSearchLog(kLogFrequency, objective_monitor, prefix);
SolutionCollector collector = solver.MakeLastSolutionCollector();
collector.Add(all_seq.ToArray());
collector.AddObjective(objective_var);
if (solver.Solve(main_phase, search_log, objective_monitor, null, collector))
Console.Out.WriteLine("Optimal solution = " + collector.ObjectiveValue(0));
else
Console.Out.WriteLine("No solution.");
}
}
}

View File

@@ -0,0 +1,263 @@
//
// Copyright 2012 Hakan Kjellerstrand
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Linq;
using Google.OrTools.ConstraintSolver;
public class APuzzle
{
/**
*
* From "God plays dice"
* "A puzzle"
* http://gottwurfelt.wordpress.com/2012/02/22/a-puzzle/
* And the sequel "Answer to a puzzle"
* http://gottwurfelt.wordpress.com/2012/02/24/an-answer-to-a-puzzle/
*
* This problem instance was taken from the latter blog post.
* (Problem 1)
*
* """
* 8809 = 6
* 7111 = 0
* 2172 = 0
* 6666 = 4
* 1111 = 0
* 3213 = 0
* 7662 = 2
* 9312 = 1
* 0000 = 4
* 2222 = 0
* 3333 = 0
* 5555 = 0
* 8193 = 3
* 8096 = 5
* 7777 = 0
* 9999 = 4
* 7756 = 1
* 6855 = 3
* 9881 = 5
* 5531 = 0
*
* 2581 = ?
* """
*
* Note:
* This model yields 10 solutions, since x4 is not
* restricted in the constraints.
* All solutions has x assigned to the correct result.
*
*
* (Problem 2)
* The problem stated in "A puzzle"
* http://gottwurfelt.wordpress.com/2012/02/22/a-puzzle/
* is
* """
* 8809 = 6
* 7662 = 2
* 9312 = 1
* 8193 = 3
* 8096 = 5
* 7756 = 1
* 6855 = 3
* 9881 = 5
*
* 2581 = ?
* """
* This problem instance yields two different solutions of x,
* one is the same (correct) as for the above problem instance,
* and one is not.
* This is because here x0,x1,x4 and x9 are underdefined.
*
*
*/
private static void Solve(int p = 1)
{
Solver solver = new Solver("APuzzle");
Console.WriteLine("\nSolving p{0}", p);
//
// Data
//
int n = 10;
//
// Decision variables
//
IntVar x0 = solver.MakeIntVar(0, n-1, "x0");
IntVar x1 = solver.MakeIntVar(0, n-1, "x1");
IntVar x2 = solver.MakeIntVar(0, n-1, "x2");
IntVar x3 = solver.MakeIntVar(0, n-1, "x3");
IntVar x4 = solver.MakeIntVar(0, n-1, "x4");
IntVar x5 = solver.MakeIntVar(0, n-1, "x5");
IntVar x6 = solver.MakeIntVar(0, n-1, "x6");
IntVar x7 = solver.MakeIntVar(0, n-1, "x7");
IntVar x8 = solver.MakeIntVar(0, n-1, "x8");
IntVar x9 = solver.MakeIntVar(0, n-1, "x9");
IntVar[] all = {x0,x1,x2,x3,x4,x5,x6,x7,x8,x9};
// The unknown, i.e. 2581 = x
IntVar x = solver.MakeIntVar(0, n-1, "x");
//
// Constraints
//
// Both problem are here shown in two
// approaches:
// - using equations
// - using a a matrix and Sum of each row
if (p == 1) {
// Problem 1
solver.Add(x8+x8+x0+x9 == 6);
solver.Add(x7+x1+x1+x1 == 0);
solver.Add(x2+x1+x7+x2 == 0);
solver.Add(x6+x6+x6+x6 == 4);
solver.Add(x1+x1+x1+x1 == 0);
solver.Add(x3+x2+x1+x3 == 0);
solver.Add(x7+x6+x6+x2 == 2);
solver.Add(x9+x3+x1+x2 == 1);
solver.Add(x0+x0+x0+x0 == 4);
solver.Add(x2+x2+x2+x2 == 0);
solver.Add(x3+x3+x3+x3 == 0);
solver.Add(x5+x5+x5+x5 == 0);
solver.Add(x8+x1+x9+x3 == 3);
solver.Add(x8+x0+x9+x6 == 5);
solver.Add(x7+x7+x7+x7 == 0);
solver.Add(x9+x9+x9+x9 == 4);
solver.Add(x7+x7+x5+x6 == 1);
solver.Add(x6+x8+x5+x5 == 3);
solver.Add(x9+x8+x8+x1 == 5);
solver.Add(x5+x5+x3+x1 == 0);
// The unknown
solver.Add(x2+x5+x8+x1 == x);
} else if (p == 2) {
// Another representation of Problem 1
int[,] problem1 = {
{8,8,0,9, 6},
{7,1,1,1, 0},
{2,1,7,2, 0},
{6,6,6,6, 4},
{1,1,1,1, 0},
{3,2,1,3, 0},
{7,6,6,2, 2},
{9,3,1,2, 1},
{0,0,0,0, 4},
{2,2,2,2, 0},
{3,3,3,3, 0},
{5,5,5,5, 0},
{8,1,9,3, 3},
{8,0,9,6, 5},
{7,7,7,7, 0},
{9,9,9,9, 4},
{7,7,5,6, 1},
{6,8,5,5, 3},
{9,8,8,1, 5},
{5,5,3,1, 0}
};
for(int i = 0; i < problem1.GetLength(0); i++) {
solver.Add( (from j in Enumerable.Range(0, 4)
select all[problem1[i,j]]
).ToArray().Sum() == problem1[i,4] );
}
solver.Add(all[2]+all[5]+all[8]+all[1] == x);
} else if (p == 3) {
// Problem 2
solver.Add(x8+x8+x0+x9 == 6);
solver.Add(x7+x6+x6+x2 == 2);
solver.Add(x9+x3+x1+x2 == 1);
solver.Add(x8+x1+x9+x3 == 3);
solver.Add(x8+x0+x9+x6 == 5);
solver.Add(x7+x7+x5+x6 == 1);
solver.Add(x6+x8+x5+x5 == 3);
solver.Add(x9+x8+x8+x1 == 5);
// The unknown
solver.Add(x2+x5+x8+x1 == x);
} else {
// Another representation of Problem 2
int[,] problem2 = {
{8,8,0,9, 6},
{7,6,6,2, 2},
{9,3,1,2, 1},
{8,1,9,3, 3},
{8,0,9,6, 5},
{7,7,5,6, 1},
{6,8,5,5, 3},
{9,8,8,1, 5}
};
for(int i = 0; i < problem2.GetLength(0); i++) {
solver.Add( (from j in Enumerable.Range(0, 4)
select all[problem2[i,j]]
).ToArray().Sum() == problem2[i,4] );
}
solver.Add(all[2]+all[5]+all[8]+all[1] == x);
}
//
// Search
//
DecisionBuilder db = solver.MakePhase(all,
Solver.INT_VAR_DEFAULT,
Solver.INT_VALUE_DEFAULT);
solver.NewSearch(db);
int c = 0;
while (solver.NextSolution()) {
Console.Write("x: {0} x0..x9: ", x.Value());
for(int i = 0; i < n; i++) {
Console.Write(all[i].Value() + " ");
}
Console.WriteLine();
}
Console.WriteLine("\nSolutions: {0}", solver.Solutions());
Console.WriteLine("WallTime: {0}ms", solver.WallTime());
Console.WriteLine("Failures: {0}", solver.Failures());
Console.WriteLine("Branches: {0} ", solver.Branches());
solver.EndSearch();
}
public static void Main(String[] args)
{
for(int p = 1; p <= 4; p++) {
Solve(p);
}
}
}

Some files were not shown because too many files have changed in this diff Show More