Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created December 1, 2012 11:48
Show Gist options
  • Select an option

  • Save adrianseeley/4181763 to your computer and use it in GitHub Desktop.

Select an option

Save adrianseeley/4181763 to your computer and use it in GitHub Desktop.
Ygg- OCR Solver :: a C# Snippet for Cloud Building a Fixed Function Optical Character Recognition Solver
// ygg - OCR solver (swarm friendly, requires mongodb c# driver, and mongod running at the IP specified)
// ~(3/96)% wrong at 30k genes in roughly 24 single core (2.2ghz) compute hours
// adrian@gatosomina.com
// using a cluster at head methodology to control the swarm, read 100 - gen 100 - sync all
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using System.Threading;
namespace ygg
{
class P
{
public static MongoServer s; public static MongoDatabase d; public static MongoCollection g; public static List<C> tr; public static List<C> te;
static void Main(string[] args)
{
i(); for (int t = 0; t < 1; t++) new Thread(new ThreadStart(() => { w(); })).Start(); Console.ReadLine();
}
static void i()
{
tr = new List<C>(); te = new List<C>();
using (FileStream fs = File.Open("train.csv", FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { sr.ReadLine(); while (!sr.EndOfStream) tr.Add(new C(sr.ReadLine())); } }
using (FileStream fs = File.Open("test.csv", FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { sr.ReadLine(); while (!sr.EndOfStream) te.Add(new C(sr.ReadLine())); } }
s = MongoServer.Create("mongodb://192.168.2.17/?safe=true");
d = s.GetDatabase("ocr");
g = d.GetCollection<G>("genes");
}
static G w()
{
cs:
try { if (g.FindOneAs<G>() == null) g.Insert(new G()); }
catch { goto cs; }
rs:
try
{
MongoCursor es = g.FindAllAs<G>().SetLimit(100).SetSkip(0).SetSortOrder(SortBy.Ascending("fitness"));
List<G> oj = new List<G>(); foreach (G q in es) oj.Add(q); oj.Reverse();
int c = oj.Count - 1; int wt = 200;
while (oj.Count < wt) { while (c >= 0 && oj.Count < wt) { G mu = oj[c].mt(1); if (!oj.Contains(mu) && mu.ft <= oj[c].ft) { oj.Add(mu); c = oj.Count - 1; } else c--; } c = oj.Count - 1; }
for (int p = 0; p < oj.Count; p++) g.Save<G>(oj[p]);
goto rs;
}
catch { goto rs; }
return null;
}
}
class G
{
public ObjectId _id { get; set; }
public static Random r = new Random();
public long ft { get; set; }
private List<bool> ex;
public String d
{
get
{
string rt = ""; long b = -1; bool l = false;
for (int e = 0; e < ex.Count; e++) { if (ex[e] == l) b++; else { rt += b + "-"; b = 0; l = !l; } }
rt += b + "-";
return rt;
}
set
{
ex = new List<bool>();
string[] ch = value.Split('-');
bool st = false;
for (int c = 0; c < ch.Length - 1; c++) { long b = long.Parse(ch[c]); while (b >= 0) { ex.Add(st); b--; } st = !st; }
if (ex.Count != 40768) throw new Exception("gtfo");
}
}
public G()
{
ex = new List<bool>();
for (int e = 0; e < 40768; e++) ex.Add(false);
tf();
}
public long to(C o)
{
long t = 0; int e = 0; int f = 0; int v = 0;
for (int x = 0; x < 28; x++) { for (int y = 0; y < 28; y++) { if (ex[e]) if (o.p[x, y] == 0) t++; e++; if (ex[e]) if (o.p[x, y] != 0) t++; e++; } }
for (int d = 0; d < 4; d++) { for (int i = 0; i < 28; i++) { for (int c = 0; c < 28; c++) { if (ex[e]) if (o.ft[f] > c) t++; e++; if (ex[e]) if (o.ft[f] < c) t++; e++; if (ex[e]) if (o.ft[f] == c) t++; e++; if (ex[e]) if (o.ft[f] != c) t++; e++; } f++; } }
for (int d = 0; d < 2; d++) { for (int i = 0; i < 28; i++) { for (int c = 0; c < 28; c++) { if (ex[e]) if (o.vo[v] > c) t++; e++; if (ex[e]) if (o.vo[v] < c) t++; e++; if (ex[e]) if (o.vo[v] == c) t++; e++; if (ex[e]) if (o.vo[v] != c) t++; e++; } v++; } }
return t;
}
public int eu(C c)
{
int v = 2;
return v;
}
public G(G g)
{
ex = new List<bool>();
for (int e = 0; e < g.ex.Count; e++) { ex.Add(g.ex[e]); }
tf();
}
public G mt(int mc)
{
G mu = new G(this);
for (int m = 0; m < mc; m++) { int z = r.Next(0, ex.Count); mu.ex[z] = !mu.ex[z]; }
return mu;
}
public void tf()
{
ft = 0;
long[] hg = new long[10];
long[] lw = new long[10];
for (int i = 0; i < 10; i++) { hg[i] = long.MinValue; lw[i] = long.MaxValue; }
for (int t = 0; t < P.tr.Count; t++) { long ow = to(P.tr[t]); if (ow > hg[P.tr[t].av]) hg[P.tr[t].av] = ow; if (ow < lw[P.tr[t].av]) lw[P.tr[t].av] = ow; }
for (int l = 0; l < 10; l++) { for (int r = l + 1; r < 10; r++) { if (hg[l] <= hg[r] && hg[l] >= lw[r]) ft++; if (lw[l] <= hg[r] && lw[l] >= lw[r]) ft++; } }
}
public void rf()
{
ft = 0;
for (int t = 0; t < P.tr.Count; t++) { if (eu(P.tr[t]) != P.tr[t].av) { ft += 1; } }
}
public override bool Equals(object obj)
{
G gene = (G)obj;
for (int e = 0; e < ex.Count; e++) { if (ex[e] != gene.ex[e]) return false; }
return true;
}
}
class C
{
public const int un = -1;
public int[,] p;
public int[] ft;
public int[] vo;
public int av;
public int ev;
public C(string li)
{
p = new int[28, 28]; ft = new int[28 * 4]; vo = new int[28 * 2]; av = un; ev = un; int s = 0; int v = 0; int f = 0;
string[] ss = li.Split(',');
if (ss.Length == 785) { av = int.Parse(ss[0]); s = 1; }
for (int x = 0; x < 28; x++) { for (int y = 0; y < 28; y++) { p[x, y] = int.Parse(ss[s]); s++; } }
for (int l = 0; l < 28; l++) { int dp = 0; while (dp < 28 && p[dp, l] == 0) { dp++; } ft[f] = dp; f++; }
for (int r = 0; r < 28; r++) { int dp = 0; while (dp < 28 && p[27 - dp, r] == 0) { dp++; } ft[f] = dp; f++; }
for (int u = 0; u < 28; u++) { int dp = 0; while (dp < 28 && p[u, 27 - dp] == 0) { dp++; } ft[f] = dp; f++; }
for (int d = 0; d < 28; d++) { int dp = 0; while (dp < 28 && p[d, dp] == 0) { dp++; } ft[f] = dp; f++; }
for (int lr = 0; lr < 28; lr++) { vo[v] = Math.Max(28 - ft[(0 * 28) + lr] - ft[(1 * 28) + lr], 0); v++; }
for (int ud = 0; ud < 28; ud++) { vo[v] = Math.Max(28 - ft[(2 * 28) + ud] - ft[(3 * 28) + ud], 0); v++; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment