Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdisml authored Nov 25, 2019
1 parent d90be62 commit 8df61ec
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
95 changes: 95 additions & 0 deletions Adaline.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class Adaline(input: MutableList<MutableList<Number>>){
var x = input
var y = x.removeAt(input.size-1)
var w = MutableList(x.size){0.0}
var b = 0.0

private fun yin (input:MutableList<Double>):Double{
var yin = 0.0
var i = 0
while (i < w.size){
yin += w[i] * input[i]
i++
}
yin += b
return yin
}

fun compute (input:MutableList<Double>):Int{
return when {
yin(input) >= 0 -> 1
else -> -1
}
}

fun learn (a:Number,delta:Number,successPercent:Number?,debug:Boolean){
var m = 0
var e = 0.0
var es = mutableListOf<Double>()
while (true){
if (m == w.size - 1){
m = 0
es = es.toDoubleArray().toSortedSet().reversed().toMutableList()
if (es.first() <= delta.toDouble()){
if (successPercent == null) {
break
}
}
if (successPercent != null) {
if (test(debug) >= successPercent.toDouble()){
break
}
}
es = mutableListOf()
}
for (i in 0 until y.size){

val line = mutableListOf<Double>()
for (n in 0 until x.size){
line.add(x[n][i].toDouble())
}

if (compute(line).toDouble() != y[i].toDouble()){
for (j in 0 until x.size){
e = w[j]
w[j] += a.toDouble() * x[j][i].toDouble() * (y[i].toDouble() - yin(line))
e -= w[j]
es.add(e)
}
b += a.toDouble() * (y[i].toDouble() - yin(line))
}
if (debug) print(e)
}
m++
}
}

fun test (debug:Boolean):Double{
var wins = 0
var losses = 0

for (i in 0 until y.size){

val line = mutableListOf<Double>()
for (n in 0 until x.size){
line.add(x[n][i].toDouble())
}
if (debug) println(line + " : " + compute(line).toString() + " ?= " + y[i].toString())
if (compute(line).toDouble() == y[i].toDouble())
wins ++
else
losses ++
}
val percent = (wins.toDouble() * 100.0)/(wins.toDouble() + losses.toDouble())
if (debug) println("$wins:$losses = ${percent.toInt()}%")
return percent
}

fun print (){
println(w + "$b")
}

fun print (e:Double){
println(w + "$b" + "error : $e")
}
}
42 changes: 42 additions & 0 deletions App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
fun main () {

// val andPerceptron = Perceptron(mutableListOf(
// mutableListOf<Number>(+1,+1,-1,-1),
// mutableListOf<Number>(+1,-1,+1,-1),
// mutableListOf<Number>(+1,-1,-1,-1)
// ),0.2)
// andPerceptron.learn(1,false)
// andPerceptron.print()
// andPerceptron.test(true)



// val inputPerceptron = Perceptron(mutableListOf(
// mutableListOf<Number>(0.67044,-0.35508,0.10452,0.95826,0.098617,-0.33915,0.23894,0.27873,0.51302,0.1722,-0.01531,0.38949,0.94547,-0.34449,0.67561,0.47814,0.90835,-0.93615,-0.28626,0.32531),
// mutableListOf<Number>(-0.437,-0.53923,0.42226,0.24915,0.18122,0.32088,-0.90489,-0.30243,-0.097319,-0.51819,0.43009,0.71236,-0.43698,0.4621,-0.72447,0.67345,-0.7228,0.17642,-0.26769,0.61352),
// mutableListOf<Number>(1,-1,1,1,1,1,-1,-1,1,-1,1,1,1,1,-1,1,-1,-1,-1,1))
// ,0.2)
// inputPerceptron.learn(1,false)
// inputPerceptron.print()
// inputPerceptron.test(true)


// val andAdaline = Adaline(mutableListOf(
// mutableListOf<Number>( 0, 0, 1, 1),
// mutableListOf<Number>( 0, 1, 0, 1),
// mutableListOf<Number>(-1,-1,-1,+1)
// ))
// andAdaline.learn(0.1,0.1,null,false)
// andAdaline.print()
// andAdaline.test(true)

val inputAdaline = Adaline(mutableListOf(
mutableListOf<Number>(0.67044,-0.35508,0.10452,0.95826,0.098617,-0.33915,0.23894,0.27873,0.51302,0.1722,-0.01531,0.38949,0.94547,-0.34449,0.67561,0.47814,0.90835,-0.93615,-0.28626,0.32531),
mutableListOf<Number>(-0.437,-0.53923,0.42226,0.24915,0.18122,0.32088,-0.90489,-0.30243,-0.097319,-0.51819,0.43009,0.71236,-0.43698,0.4621,-0.72447,0.67345,-0.7228,0.17642,-0.26769,0.61352),
mutableListOf<Number>(1,-1,1,1,1,1,-1,-1,1,-1,1,1,1,1,-1,1,-1,-1,-1,1))
)
inputAdaline.learn(0.01,0.1,100,false)
inputAdaline.print()
inputAdaline.test(true)

}
76 changes: 76 additions & 0 deletions Perceptron.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
class Perceptron(input: MutableList<MutableList<Number>>, var teta:Double){
var x = input
var y = x.removeAt(input.size-1)
var w = MutableList(x.size){0.0}
var b = 0.0

fun compute (input:MutableList<Double>):Int?{
var yin = 0.0
var i = 0
while (i < w.size){
yin += w[i] * input[i]
i++
}
yin += b
return when {
teta < yin -> 1
-teta < yin && yin < teta -> 0
yin < -teta -> -1
else -> null
}
}

fun learn (a:Number,debug:Boolean){
var m = 0
var changed = false
while (true){
if (m == w.size - 1){
m = 0
if (!changed){
break
}
}
changed = false
for (i in 0 until y.size){
val line = mutableListOf<Double>()
for (n in 0 until x.size){
line.add(x[n][i].toDouble())
}
if (compute(line)!!.toDouble() != y[i].toDouble()){
for (j in 0 until x.size){
w[j] += a.toDouble() * x[j][i].toDouble() * y[i].toDouble()
changed = true
}
b += y[i].toDouble()
}
if (debug) print()
}
m++
}
}

fun test (debug:Boolean):Double{
var wins = 0
var losses = 0

for (i in 0 until y.size){

val line = mutableListOf<Double>()
for (n in 0 until x.size){
line.add(x[n][i].toDouble())
}
if (debug) println(line + " : " + compute(line).toString() + " ?= " + y[i].toString())
if (compute(line)!!.toDouble() == y[i].toDouble())
wins ++
else
losses ++
}
val percent = (wins.toDouble() * 100.0)/(wins.toDouble() + losses.toDouble())
if (debug) println("$wins:$losses = ${percent.toInt()}%")
return percent
}

fun print (){
println(w + "$b")
}
}

0 comments on commit 8df61ec

Please sign in to comment.