Skip to content

Commit

Permalink
- Fixed puzzle generator to output shuffle setting
Browse files Browse the repository at this point in the history
 - Added score mode setting to puzzle generator
 - Fixed `Number` casting `double` via `int`
 - Added a new constant compressor pass which attempts to convert large constants into `Atan(X)+Y` (Atan can create some huge outputs with very small inputs)
  • Loading branch information
martindevans committed Jun 1, 2020
1 parent fb2729d commit 0b61a2d
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 12 deletions.
16 changes: 15 additions & 1 deletion Yolol.Analysis/TreeVisitor/Reduction/ConstantCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Yolol.Execution.Extensions;
using Yolol.Grammar.AST.Expressions;
using Yolol.Grammar.AST.Expressions.Binary;
using Yolol.Grammar.AST.Expressions.Unary;

namespace Yolol.Analysis.TreeVisitor.Reduction
{
Expand All @@ -16,7 +17,8 @@ protected override BaseExpression Visit(ConstantNumber con)

var replacements = new[] {
SmallestExponents(con.Value),
BestFraction(con.Value)
BestFraction(con.Value),
AtanOffset(con.Value),
};

var shortestLength = int.MaxValue;
Expand Down Expand Up @@ -156,5 +158,17 @@ private static BaseExpression BestFraction(Number number)
else
return new ConstantNumber(number);
}

private static BaseExpression? AtanOffset(Number number)
{
var atan = new ArcTan(new ConstantNumber(number)).TryStaticEvaluate();
if (!atan.HasValue)
return null;
var atanFloor = (long)atan.Value.Number.Value;

var offset = number - atan.Value;

return new Add(new ConstantNumber(offset.Number), new Tangent(new ConstantNumber(atanFloor)));
}
}
}
5 changes: 5 additions & 0 deletions Yolol/Execution/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public static implicit operator Number(int i)
return new Number(i);
}

public static explicit operator Number(double i)
{
return SafeNew((decimal)i);
}

public static implicit operator Number(decimal d)
{
return SafeNew(d);
Expand Down
20 changes: 18 additions & 2 deletions YololEmulator.Tests/Ladder/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public static class Generator
{
private class Data
{
[JsonProperty("shuffle")]
public bool Shuffle { get; set; }

[JsonProperty("mode")]
public ScoreMode Mode { get; set; }

[JsonProperty("in")]
public Dictionary<string, Value>[] In { get; set; }

Expand All @@ -35,11 +41,21 @@ public IVariable Get(string name)
}
}

public static void YololLadderGenerator(List<Dictionary<string, Value>> input, List<Dictionary<string, Value>> output, bool shuffle = true)
public enum ScoreMode
{
Unknown = 0,

BasicScoring = 1,
Approximate = 2,
}

public static void YololLadderGenerator(List<Dictionary<string, Value>> input, List<Dictionary<string, Value>> output, bool shuffle = true, ScoreMode mode = ScoreMode.BasicScoring)
{
var d = new Data() {
In = input.ToArray(),
Out = output.ToArray()
Out = output.ToArray(),
Shuffle = shuffle,
Mode = mode
};

Console.WriteLine(JsonConvert.SerializeObject(d, new YololValueConverter()));
Expand Down
10 changes: 2 additions & 8 deletions YololEmulator.Tests/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Yolol.Analysis;
using Yolol.Analysis.TreeVisitor;
using Yolol.Analysis.TreeVisitor.Reduction;
using Yolol.Grammar;
using Yolol.Grammar.AST.Expressions;
using Yolol.Grammar.AST.Statements;

namespace YololEmulator.Tests
Expand Down Expand Up @@ -133,13 +135,5 @@ public void EStmtDecomposition()
Console.WriteLine(line);

}

[TestMethod]
public void MethodName()
{
var str = "5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199";

Console.WriteLine(string.Join(",", str.Split(", ").Reverse()));
}
}
}
83 changes: 83 additions & 0 deletions YololEmulator.Tests/SAT/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,89 @@ public void Z3()
}
}

[TestMethod]
public void Superoptimise_Attempt2()
{
using var ctx = new Context();
using var solver = ctx.MkSolver();
solver.Set("timeout", 100);

// :o = (i%a * i%b * i%c * i%d * i%e * i%f)>g

// Setup params we want to know
var a = (IntExpr)ctx.MkConst("a", ctx.IntSort);
var b = (IntExpr)ctx.MkConst("b", ctx.IntSort);
var c = (IntExpr)ctx.MkConst("c", ctx.IntSort);
var d = (IntExpr)ctx.MkConst("d", ctx.IntSort);
var e = (IntExpr)ctx.MkConst("e", ctx.IntSort);
var f = (IntExpr)ctx.MkConst("f", ctx.IntSort);
var g = (IntExpr)ctx.MkConst("g", ctx.IntSort);

solver.Assert(a > 0 & a < 20);
solver.Assert(b > 0 & b < 20);
solver.Assert(c > 0 & c < 20);
solver.Assert(d > 0 & d < 20);
solver.Assert(e > 0 & e < 20);
solver.Assert(f > 0 & f < 20);
solver.Assert(g >= 0 & g < 1);

//solver.Assert(ctx.MkEq(a, ctx.MkInt(2)));
//solver.Assert(ctx.MkEq(b, ctx.MkInt(3)));
solver.Assert(ctx.MkEq(c, ctx.MkInt(5)));
solver.Assert(ctx.MkEq(d, ctx.MkInt(7)));
solver.Assert(ctx.MkEq(e, ctx.MkInt(11)));
solver.Assert(ctx.MkEq(f, ctx.MkInt(13)));
solver.Assert(ctx.MkEq(g, ctx.MkInt(0)));

// Setup cases
BoolExpr Equation(int i)
{
var ii = ctx.MkInt(i);
var aa = ctx.MkRem(ii, a);
var bb = ctx.MkRem(ii, b);
var cc = ctx.MkRem(ii, c);
var dd = ctx.MkRem(ii, d);
var ee = ctx.MkRem(ii, e);
var ff = ctx.MkRem(ii, f);

var expr = (aa * bb * cc * dd * ee * ff);
return expr > g;
}

for (var i = 14; i < 201; i++)
solver.Assert(ctx.MkEq(ctx.MkBool(IsPrime(i)), Equation(i)));

static bool IsPrime(int number)
{
if (number <= 1) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;

var boundary = (int)Math.Floor(Math.Sqrt(number));

for (int i = 3; i <= boundary; i+=2)
if (number % i == 0)
return false;

return true;
}

if (solver.Check() == Status.SATISFIABLE)
{
var av = (IntNum)solver.Model.Eval(a);
var bv = (IntNum)solver.Model.Eval(b);
var cv = (IntNum)solver.Model.Eval(c);
var dv = (IntNum)solver.Model.Eval(d);
var ev = (IntNum)solver.Model.Eval(e);
var fv = (IntNum)solver.Model.Eval(f);
var gv = (IntNum)solver.Model.Eval(g);

Console.WriteLine($":o = (i%{av}+i%{bv}+i%{cv}+i%{dv}+i%{ev}+i%{fv})>{gv}");
}

Console.WriteLine(solver.Check());
}

enum Op
{
And, Or, Xor
Expand Down
47 changes: 47 additions & 0 deletions YololEmulator/AzurethiPrimes.lol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
p="100010101100"
:o=p---p>:done++goto2
i=:i:o=i%2*i%3*i%5*i%7*i%11*(i!=169)>:done++goto3

/--------//--------//--------//--------//--------//--------//--------/

p="10100010101100"
:o=p---p>:done++goto2
i=:i:o=i%2*i%3*i%5*i%7*i%11*i%13>:done++goto3

/--------//--------//--------//--------//--------//--------//--------/

v="0000"w="1000"x=w+10z=x-10+"00"y="0"+z+z+10p="010"+x+z+v+"1"+y+"0"+y
p+=z+v+"10"+z+w+z+v+v+x+x+w+z+"00"+z+w+z+"10"+w+z+"1"+y+"0010"+w+z
p+="10"+z+x+x+x+"101100"
:o=p---p>0:done=1goto4

/--------//--------//--------//--------//--------//--------//--------/

x=100010z=x-10y="0"+z+z+10p="010"+x+z+"00001"+y+"0"+y+z+"000010"+z
p+="1000"+z+"00000000"+x+x+"1000"+z+"00"+z+"1000"+z+"101000"+z+"1"+y
p+="00101000"+z+"10"+z+x+x+x+"101100"
:o=p---p>0:done=1goto4

/--------//--------//--------//--------//--------//--------//--------/

p="010100010100000000010100000100000100010000010000010100000000010100"
p+="00010001000000000000010001010001010001000000010000010001000001010"
p+="00100000101000001000001000101000100000101000001000101000101000101"
p+="01100"
:o=p---p>0:done=1goto5

/--------//--------//--------//--------//--------//--------//--------/

:o=0:done=1p="199,197,193,191,181,179,173,167,163,157,151,149,139,137"
:o=0:done=1p+=",131,127,113,109,107,103,101,97,89,83,79,73,71,67,61,5"
:o=1:done=1p+="9,53,47,43,41,37,31,29,23,19,17,13,11,7,5"b=10
:o=1:done=1s="98743"
:o=0:done=1n=0j=0
c=p---p x/=(c>".")d=5*(c>4)+2*(s>s-c)d+=c>d d+=c>d n+=d*b^j++goto6
:done=1 :o=n==:i goto7-:o*2

/--------//--------//--------//--------//--------//--------//--------/

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131,
137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199
5 changes: 4 additions & 1 deletion YololEmulator/YololEmulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
</ItemGroup>

<ItemGroup>
<None Update="test.lol">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="binary_search_number_parser.lol">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -64,7 +67,7 @@
<None Update="atanimpossible.lol">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="test.lol">
<None Update="AzurethiPrimes.lol">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="sorting1.lol">
Expand Down

0 comments on commit 0b61a2d

Please sign in to comment.