2019-06-20 13:17:23 +02:00
|
|
|
package machine_learning.perceptron;
|
|
|
|
|
2019-06-30 23:59:49 +02:00
|
|
|
import machine_learning.DataClass;
|
2021-10-10 16:07:56 +02:00
|
|
|
import machine_learning.Vector;
|
|
|
|
import org.junit.jupiter.api.*;
|
2019-06-20 13:17:23 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
|
|
class PerceptronTest
|
|
|
|
{
|
|
|
|
List<Vector> positives;
|
|
|
|
List<Vector> negatives;
|
2019-06-27 00:11:59 +02:00
|
|
|
Perceptron perceptron;
|
2019-06-20 13:17:23 +02:00
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
void initLearnData()
|
|
|
|
{
|
2019-06-23 13:48:53 +02:00
|
|
|
double biasUnit = 1d;
|
2019-06-20 13:17:23 +02:00
|
|
|
this.positives = new ArrayList<>(List.of(
|
2019-06-27 00:02:21 +02:00
|
|
|
new Vector(8d, 4d, biasUnit),
|
|
|
|
new Vector(8d, 6d, biasUnit),
|
|
|
|
new Vector(9d, 2d, biasUnit),
|
|
|
|
new Vector(9d, 5d, biasUnit))
|
2019-06-20 13:17:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
this.negatives = new ArrayList<>(List.of(
|
2019-06-27 00:02:21 +02:00
|
|
|
new Vector(6d, 1d, biasUnit),
|
|
|
|
new Vector(7d, 3d, biasUnit),
|
|
|
|
new Vector(8d, 2d, biasUnit),
|
|
|
|
new Vector(9d, 0d, biasUnit))
|
2019-06-20 13:17:23 +02:00
|
|
|
);
|
2019-06-27 00:11:59 +02:00
|
|
|
|
|
|
|
this.perceptron = new Perceptron();
|
|
|
|
this.perceptron.learn(this.positives, this.negatives);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void shouldClassifyVectorCorrectAsNegative()
|
|
|
|
{
|
|
|
|
var vector = new Vector(0d, 0d, 1d);
|
|
|
|
|
|
|
|
var actualClass = this.perceptron.classify(vector);
|
|
|
|
var expectedClass = DataClass.NEGATIVE;
|
|
|
|
|
2021-10-10 16:07:56 +02:00
|
|
|
Assertions.assertEquals(expectedClass, actualClass);
|
2019-06-20 13:17:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2019-06-27 00:11:59 +02:00
|
|
|
void shouldClassifyVectorCorrectAsPositive()
|
2019-06-20 13:17:23 +02:00
|
|
|
{
|
2019-06-27 00:11:59 +02:00
|
|
|
var vector = new Vector(9d, 3d, 1d);
|
|
|
|
|
|
|
|
var actualClass = this.perceptron.classify(vector);
|
|
|
|
var expectedClass = DataClass.POSITIVE;
|
|
|
|
|
2021-10-10 16:07:56 +02:00
|
|
|
Assertions.assertEquals(expectedClass, actualClass);
|
2019-06-20 13:17:23 +02:00
|
|
|
}
|
|
|
|
}
|