-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConstraint.cs
172 lines (148 loc) · 4.28 KB
/
Constraint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* @(#)Constraint.cs
*/
using System;
using System.ComponentModel;
using System.Linq;
namespace Cream
{
public enum ConstraintTypes
{
Hard, Soft
}
/// <summary> An abstract class for constraints.
/// A constraint is a component of a constraint network.
/// See Network for example programs to construct constraints and
/// Add them to a constraint network.
/// </summary>
/// <seealso cref="Network">
/// </seealso>
/// <since> 1.0
/// </since>
/// <version> 1.0, 01/12/08
/// Updated 1.1 05/01/08
/// </version>
///
/// <author> Based on Java Solver by : Naoyuki Tamura ([email protected])
/// C#: Ali Hmer ([email protected])
/// </author>
public abstract class Constraint
{
//virtual protected internal Condition Condition
//{
//}
protected internal Network Network { get; set; }
[DefaultValue(-1)]
protected internal int Index { get; set; }
[DefaultValue(ConstraintTypes.Hard)]
protected internal ConstraintTypes CType { get; set; }
public bool IsHard
{
get
{
return CType == ConstraintTypes.Hard ? true : false;
}
}
public bool IsSoft
{
get
{
return CType == ConstraintTypes.Soft ? true : false;
}
}
[DefaultValue(0)]
public int Weight { get; set; }
/// <summary> Constructor.
/// (for invocation by subclass constructors, typically implicit)
/// </summary>
protected internal Constraint(Network net)
{
Network = net;
Index = -1;
Network.ADD(this);
CType = ConstraintTypes.Hard;
}
/// <summary> Constructor.
/// (for invocation by subclass constructors, typically implicit)
/// </summary>
protected internal Constraint(Network net, ConstraintTypes cType)
{
Network = net;
Index = -1;
Network.ADD(this);
CType = cType;
}
/// <summary> Constructor.
/// (for invocation by subclass constructors, typically implicit)
/// </summary>
protected internal Constraint(Network net, ConstraintTypes cType, int weight)
{
Network = net;
Index = -1;
Weight = weight;
Network.ADD(this);
CType = cType;
}
/// <summary> Creates a Copy of this constraint for a new network <tt>net</tt>.</summary>
/// <returns> the Copy of this constraint
/// </returns>
protected internal abstract Constraint Copy(Network net);
protected internal virtual void ClearCondition()
{
}
protected internal virtual Condition ExtractCondition()
{
return null;
}
protected internal abstract bool IsModified();
protected internal abstract bool Satisfy(Trail trail);
protected internal abstract bool IsSatisfied();
protected internal static Variable Copy(Variable v0, Network net)
{
int j = v0.Index;
return net.GetVariable(j);
}
protected internal static Variable[] Copy(Variable[] v0, Network net)
{
var v = new Variable[v0.Length];
for (int i = 0; i < v0.Length; i++)
{
int j = v0[i].Index;
v[i] = net.GetVariable(j);
}
return v;
}
protected internal static bool IsModified(Variable[] v)
{
return v.Any(t => t.IsModified());
}
protected internal static String ToString(Variable[] v)
{
String s = "";
if (v != null)
{
String delim = "";
foreach (Variable t in v)
{
s += (delim + t);
delim = ",";
}
}
return "{" + s + "}";
}
protected internal static String ToString(int[] a)
{
var s = "";
if (a != null)
{
var delim = "";
foreach (var t in a)
{
s += (delim + t);
delim = ",";
}
}
return "{" + s + "}";
}
}
}