Skip to content

Commit 9381e76

Browse files
committed
Merge branch 'master' of https://github.com/xieguigang/sciBASIC
2 parents 7fa542e + 5ee0cd7 commit 9381e76

14 files changed

Lines changed: 746 additions & 63 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Imports Microsoft.VisualBasic.ComponentModel.Collection
2+
3+
Namespace ShapleyValue
4+
5+
''' <summary>
6+
''' represent the following function often written by v
7+
''' 2^N to R
8+
''' for each subset of the set {1.. N} is associated a value
9+
''' per convention v(empty set) = 0
10+
'''
11+
''' @author Franck Benault
12+
'''
13+
''' </summary>
14+
Public Class CharacteristicFunction
15+
16+
Dim v As IDictionary(Of ISet(Of Integer), Double)
17+
18+
Public Overridable ReadOnly Property NbPlayers As Integer
19+
20+
Private Sub New(builder As CharacteristicFunctionBuilder)
21+
NbPlayers = builder.nbPlayers
22+
v = New Dictionary(Of ISet(Of Integer), Double)()
23+
v = builder.v
24+
End Sub
25+
26+
Public Overridable Function getValue(ParamArray coalition As Integer?()) As Double
27+
Dim coalitionSet As ISet(Of Integer) = New HashSet(Of Integer)()
28+
29+
For Each player As Integer In coalition
30+
coalitionSet.Add(player)
31+
Next
32+
33+
Return v(coalitionSet)
34+
End Function
35+
36+
Public Overridable Function getValue(coalitionSet As ISet(Of Integer)) As Double
37+
38+
Return v(coalitionSet)
39+
End Function
40+
41+
Public Overrides Function ToString() As String
42+
Return "CharacteristicFunction [nbPlayers=" & NbPlayers.ToString() & ", v=" & v.ToString() & "]"
43+
End Function
44+
45+
Public Class CharacteristicFunctionBuilder
46+
Friend ReadOnly nbPlayers As Integer
47+
Friend v As IDictionary(Of ISet(Of Integer), Double)
48+
49+
Public Sub New(nbPlayers As Integer)
50+
Me.nbPlayers = nbPlayers
51+
v = New Dictionary(Of ISet(Of Integer), Double)()
52+
End Sub
53+
54+
Public Overridable Function addCoalition(value As Double, ParamArray coalition As Integer?()) As CharacteristicFunctionBuilder
55+
Dim [set] As ISet(Of Integer) = New HashSet(Of Integer)()
56+
For Each player As Integer In coalition
57+
[set].Add(player)
58+
Next
59+
v([set]) = value
60+
Return Me
61+
End Function
62+
63+
Public Overridable Function build() As CharacteristicFunction
64+
Return New CharacteristicFunction(Me)
65+
End Function
66+
67+
End Class
68+
69+
Public Overridable Sub addDummyUser()
70+
Dim newV As IDictionary(Of ISet(Of Integer), Double) = New Dictionary(Of ISet(Of Integer), Double)()
71+
72+
73+
Dim coalitions = v.Keys
74+
Dim coalitionSet As HashSet(Of Integer)
75+
76+
For Each coalition In coalitions
77+
78+
coalitionSet = New HashSet(Of Integer)()
79+
coalitionSet.AddAll(coalition)
80+
coalitionSet.Add(NbPlayers + 1)
81+
82+
newV(coalitionSet) = v(coalition)
83+
Next
84+
85+
coalitionSet = New HashSet(Of Integer)()
86+
coalitionSet.Add(NbPlayers + 1)
87+
v(coalitionSet) = 0.0
88+
89+
For Each coalition In newV.Keys
90+
v(coalition) = newV(coalition)
91+
Next
92+
93+
_NbPlayers += 1
94+
End Sub
95+
96+
End Class
97+
98+
End Namespace
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Imports std = System.Math
2+
3+
Namespace ShapleyValue
4+
5+
'''
6+
''' <summary>
7+
''' @author Franck Benault
8+
'''
9+
''' @version 0.0.2
10+
''' @since 0.0.2
11+
'''
12+
''' </summary>
13+
Public NotInheritable Class CoalitionStrategy
14+
15+
Public Shared ReadOnly SEQUENTIALField As CoalitionStrategy = New CoalitionStrategy("SEQUENTIAL", InnerEnum.SEQUENTIAL)
16+
17+
Public Shared ReadOnly RANDOM As CoalitionStrategy = New CoalitionStrategy("RANDOM", InnerEnum.RANDOM)
18+
19+
Private Shared ReadOnly valueList As List(Of CoalitionStrategy) = New List(Of CoalitionStrategy)()
20+
21+
Shared Sub New()
22+
valueList.Add(SEQUENTIALField)
23+
valueList.Add(RANDOM)
24+
End Sub
25+
26+
Public Enum InnerEnum
27+
SEQUENTIAL
28+
RANDOM
29+
End Enum
30+
31+
Public ReadOnly innerEnumValue As InnerEnum
32+
Private ReadOnly nameValue As String
33+
Private ReadOnly ordinalValue As Integer
34+
Private Shared nextOrdinal As Integer = 0
35+
36+
Private Sub New(name As String, innerEnum As InnerEnum)
37+
nameValue = name
38+
ordinalValue = nextOrdinal
39+
nextOrdinal += 1
40+
innerEnumValue = innerEnum
41+
End Sub
42+
43+
Public ReadOnly Property Sequential As Boolean
44+
Get
45+
Return Equals(SEQUENTIALField)
46+
End Get
47+
End Property
48+
49+
Public Shared Function values() As CoalitionStrategy()
50+
Return valueList.ToArray()
51+
End Function
52+
53+
Public Function ordinal() As Integer
54+
Return ordinalValue
55+
End Function
56+
57+
Public Overrides Function ToString() As String
58+
Return nameValue
59+
End Function
60+
61+
Public Shared Function valueOf(name As String) As CoalitionStrategy
62+
For Each enumInstance In valueList
63+
If Equals(enumInstance.nameValue, name) Then
64+
Return enumInstance
65+
End If
66+
Next
67+
Throw New ArgumentException(name)
68+
End Function
69+
End Class
70+
71+
End Namespace
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Namespace ShapleyValue
2+
3+
Public Class FactorialUtil
4+
5+
Public Shared Function factorial(n As Long) As Long
6+
If n > 20 Then
7+
Throw New ArithmeticException("Capacity exceded for factorial " & n.ToString())
8+
End If
9+
Return If(n > 1, n * factorial(n - 1), 1)
10+
End Function
11+
12+
13+
14+
End Class
15+
16+
End Namespace
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
Namespace ShapleyValue
3+
4+
Public Class Node
5+
6+
Public Overrides Function ToString() As String
7+
Return "Node [value=" & valueField.ToString() & ", nextNode=" & nextNodeField.ToString() & "]"
8+
End Function
9+
10+
Private valueField As NodeValue
11+
12+
Private nextNodeField As Node
13+
Private prevNodeField As Node
14+
15+
Public Sub New(nodeValue As NodeValue, prevNode As Node)
16+
valueField = nodeValue
17+
prevNodeField = prevNode
18+
19+
If valueField.NextValues.Count > 0 Then
20+
Dim values = valueField.NextValues
21+
Dim nextValue As NodeValue = New NodeValue(values)
22+
nextNodeField = New Node(nextValue, Me)
23+
End If
24+
End Sub
25+
26+
Public Overridable ReadOnly Property Value As NodeValue
27+
Get
28+
Return valueField
29+
End Get
30+
End Property
31+
32+
Public Overridable Property NextNode As Node
33+
Get
34+
Return nextNodeField
35+
End Get
36+
Set(value As Node)
37+
nextNodeField = value
38+
39+
End Set
40+
End Property
41+
42+
Public Overridable Sub updateValue()
43+
valueField.updateValue()
44+
End Sub
45+
46+
47+
Public Overridable Property PrevNode As Node
48+
Get
49+
Return prevNodeField
50+
End Get
51+
Set(value As Node)
52+
prevNodeField = value
53+
End Set
54+
End Property
55+
56+
57+
End Class
58+
59+
End Namespace
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Namespace ShapleyValue
2+
3+
Public Class NodeValue
4+
5+
Public Overrides Function ToString() As String
6+
Return "NodeValue [value=" & valueField.ToString() & ", nextValues=" & nextValuesField.ToString() & "]"
7+
End Function
8+
9+
Private valueField As Integer
10+
11+
Private nextValuesField As IList(Of Integer)
12+
13+
Public Sub New(nextValues As IList(Of Integer))
14+
15+
nextValuesField = New List(Of Integer)()
16+
CType(nextValuesField, List(Of Integer)).AddRange(nextValues)
17+
valueField = nextValues(0)
18+
nextValuesField.RemoveAt(valueField)
19+
End Sub
20+
21+
Public Overridable ReadOnly Property Value As Integer
22+
Get
23+
Return valueField
24+
End Get
25+
End Property
26+
27+
Public Overridable ReadOnly Property NextValues As IList(Of Integer)
28+
Get
29+
Return New List(Of Integer)(nextValuesField)
30+
End Get
31+
End Property
32+
33+
Public Overridable Sub updateValue()
34+
If nextValuesField.Count > 0 Then
35+
valueField = nextValuesField(0)
36+
End If
37+
nextValuesField.RemoveAt(valueField)
38+
39+
End Sub
40+
41+
42+
43+
End Class
44+
45+
End Namespace

0 commit comments

Comments
 (0)