Skip to content

Commit

Permalink
Initial Import
Browse files Browse the repository at this point in the history
  • Loading branch information
xu-shawn committed Aug 1, 2024
0 parents commit ed92e36
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.shawn.games</groupId>
<artifactId>Cortex</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Cortex</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
98 changes: 98 additions & 0 deletions src/main/java/org/shawn/games/Cortex/DataLoader/DataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.shawn.games.Cortex.DataLoader;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;

public class DataLoader
{
DataInputStream dataStream;

public DataLoader(String filePath)
{
this.dataStream = new DataInputStream(getClass().getResourceAsStream(filePath));
}

public class ConvertedData
{
public long occ;
public byte[] pcs;
public short score;
public byte result;
public byte ksq;
public byte opp_ksq;
public byte[] extra;

public ConvertedData()
{
this.pcs = new byte[16];
this.extra = new byte[3];
}

public ArrayList<FeaturePair> toFeatureSet()
{
ArrayList<FeaturePair> features = new ArrayList<>();

long occupancy = this.occ;
int pieceCount = 0;

while (occupancy != 0)
{
int square = Long.numberOfTrailingZeros(occupancy);

// Two pieces are stored into upper 4 bits and lower 4 bits
int piece = (this.pcs[pieceCount / 2] >> (4 * (pieceCount & 1))) & 0b1111;

occupancy &= occupancy - 1;
pieceCount ++;

features.add(new FeaturePair(piece, square));
}

return features;
}
}

private static short toLittleEndian(short input)
{
return (short) (((input & 0xFF) << 8) | ((input & 0xFF00) >> 8));
}

private static long toLittleEndian(long input)
{
return Long.reverseBytes(input);
}

public ConvertedData load()
{
ConvertedData data = new ConvertedData();

try
{
data.occ = toLittleEndian(dataStream.readLong());

for (int i = 0; i < 16; i++)
{
data.pcs[i] = dataStream.readByte();
}

data.score = toLittleEndian(dataStream.readShort());
data.result = dataStream.readByte();
data.ksq = dataStream.readByte();
data.opp_ksq = dataStream.readByte();

for (int i = 0; i < 3; i++)
{
data.extra[i] = dataStream.readByte();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

return data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.shawn.games.Cortex.DataLoader;

public record FeaturePair(int piece, int square)
{

}
19 changes: 19 additions & 0 deletions src/main/java/org/shawn/games/Cortex/Inputs/Unbucketed768.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.shawn.games.Cortex.Inputs;

import java.util.*;
import java.util.stream.Collectors;

import org.shawn.games.Cortex.DataLoader.FeaturePair;

public class Unbucketed768
{
public static int featurePairToIndex(FeaturePair fp)
{
return (fp.piece() > 5 ? fp.piece() - 2 : fp.piece()) * 64 + fp.square();
}

public static List<Integer> featureSetToIndicies(ArrayList<FeaturePair> fp)
{
return fp.stream().map(Unbucketed768::featurePairToIndex).collect(Collectors.toList());
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/shawn/games/Cortex/LR/ConstantLR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.shawn.games.Cortex.LR;

public class ConstantLR implements LRScheduler
{
final float lr;

public ConstantLR(float lr)
{
this.lr = lr;
}

@Override
public void advance()
{
}

@Override
public float get()
{
return lr;
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/shawn/games/Cortex/LR/LRScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.shawn.games.Cortex.LR;

public interface LRScheduler
{
public void advance();
public float get();
}
35 changes: 35 additions & 0 deletions src/main/java/org/shawn/games/Cortex/LR/LinearLR.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.shawn.games.Cortex.LR;

public class LinearLR implements LRScheduler
{
final double startLR;
final double gamma;
final int step;
int counter;

public LinearLR(double startLR, double gamma, int step)
{
this.startLR = startLR;
this.gamma = gamma;
this.step = step;
this.counter = 0;
}

@Override
public void advance()
{
counter++;
}

@Override
public float get()
{
double lr = startLR;
for (int i = 0; i < counter; i += step)
{
lr *= gamma;
}
return (float) lr;
}

}
Loading

0 comments on commit ed92e36

Please sign in to comment.