Skip to content

Commit

Permalink
feat: improved run.jl script
Browse files Browse the repository at this point in the history
  • Loading branch information
jaantollander committed Dec 2, 2021
1 parent 343e222 commit cb2c9a5
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 111 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Jaan Tollander de Balsch"]
version = "0.2.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[compat]
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,16 @@ The [`src/nxn.mzn`](./src/nxn.mzn) file contains the MiniZinc constraint program

The [`src/plots.jl`](./src/plots.jl) file contains the plotting code.

Each `nxn.sh` shell script inside [`scripts`](./scripts/) contains the command for running the model for the grid of given size.
Use [`script/run.jl`](./scripts/run.jl) Julia script for running the model for the grid of given size.

Use [`script/plot.jl`](./scripts/plot.jl) Julia script to plot results.

The [`results`](./results/) directory contains the output from the shell scripts and the generated SVG plots for each grid size and taxicab distance in format `<grid>/<distance>/<id>.svg`.


## Instructions
We can begin by installing MiniZinc and adding it to our PATH environment variable. The, we can run the appropriate shell file from the `src` directory and write the output to `results/3x3.txt` file. For example:

```bash
./scripts/3x3.sh > results/3x3.txt
```

Alternatively, we can run the MiniZinc model directly.
We can begin by installing MiniZinc and adding it to our PATH environment variable. The, we can run the appropriate shell file from the `script` directory and write the output to `results/3x3.txt` file. For example:

```bash
minizinc src/nxn.mzn -s -a --solver gecode -D "n=3;"
julia scripts/run.jl -n 3 --minizinc `which minizinc`
```
5 changes: 0 additions & 5 deletions scripts/2x2.sh

This file was deleted.

6 changes: 0 additions & 6 deletions scripts/3x3.sh

This file was deleted.

7 changes: 0 additions & 7 deletions scripts/5x5.sh

This file was deleted.

49 changes: 38 additions & 11 deletions scripts/run.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
using LockPatternComplexity
using ArgParse

n = 4 # 2, 4, 6
N, xs, ys, ds = line_types(n)
minizinc = "/home/jaan/bin/MiniZincIDE-2.5.5-bundle-linux-x86_64/bin/minizinc"
cmd = `$minizinc src/nxn_gen.mzn
-D "n=$n;N=$N;xs=$(string(xs));ys=$(string(ys));ds=$(string(ds));"
--solver chuffed
--statistics
--all-solutions
--two-pass`

run(cmd)
function data(n::Int)
N, xs, ys, ds = line_types(n)
return "n=$n;N=$N;xs=$(xs);ys=$(ys);ds=$(ds);"
end

function command(n::Int, minizinc, solver, flags)::Cmd
d = data(n)
return `$minizinc src/nxn.mzn -D $d --solver $solver $flags`
end

s = ArgParseSettings()

@add_arg_table s begin
"-n"
help = "grid size"
arg_type = Int
default = 3
"--minizinc"
help = "full path to MiniZinc. Use command `which minizinc` to find it out."
arg_type = String
default = "/home/jaan/bin/MiniZincIDE-2.5.5-bundle-linux-x86_64/bin/minizinc"
end

args = parse_args(s)
n = args["n"]
minizinc = args["minizinc"]

function cmd(n)
n == 2 && return command(2, minizinc, "gecode", ["--all-solutions", "--statistics"])
n == 3 && return command(3, minizinc, "gecode", ["--all-solutions", "--statistics"])
n == 4 && return command(4, minizinc, "chuffed", ["--all-solutions", "--statistics", "--two-pass"])
n == 5 && return command(5, minizinc, "chuffed", ["--all-solutions", "--statistics", "--two-pass"])
n == 6 && return command(6, minizinc, "chuffed", ["--all-solutions", "--statistics", "--two-pass"])
error("")
end

run(cmd(n))
32 changes: 12 additions & 20 deletions src/nxn.mzn
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,22 @@ array[1..m-1] of var int: dx = [x[p[i]] - x[p[i+1]] | i in 1..m-1];
array[1..m-1] of var int: dy = [y[p[i]] - y[p[i+1]] | i in 1..m-1];

% Maximum complexity constraints.
% Enforce that there exists one of each line type.
predicate line(int: x1, int: y1, int: k) = exists(i in 1..m-1, c in 1..k)(
(dx[i] = c*x1 /\ dy[i] = c*y1) \/ (dx[i] = -c*x1 /\ dy[i] = -c*y1)
);

% Greatest common divisor
function int: gcd(int: a, int: b) = max(
[i | i in 1..min(a, b) where (a mod i) = 0 /\ (b mod i) = 0]
);

constraint line(0, 1, n-1);
constraint line(1, 0, n-1);
constraint line(1, 1, n-1);
constraint line(1, -1, n-1);
constraint forall(x1 in 1..n-2, y1 in (x1+1)..n-1 where gcd(x1, y1)=1)(
let {int: d = (n-1) div y1} in
line(x1, y1, d) /\
line(y1, x1, d) /\
line(x1, -y1, d) /\
line(y1, -x1, d)
);
int: N;
array[1..N] of int: xs;
array[1..N] of int: ys;
array[1..N] of int: ds;
constraint count(i in 1..N)(line(xs[i], ys[i], ds[i])) = min(N, m-1);
% constraint forall(i in 1..N)(line(xs[i], ys[i], ds[i]));

% Maximize distance
% Find max complexity patterns
solve satisfy;

% Print output
output ["pattern=\(p)", "\n", "distance=\(sum(i in 1..m-1)(abs(dx[i]) + abs(dy[i])))"];

% Maximize distance
% var int: distance = sum(i in 1..m-1)(abs(dx[i]) + abs(dy[i]));
% solve maximize distance;
% output ["pattern=\(p)", "\n", "distance=\(distance)"];
53 changes: 0 additions & 53 deletions src/nxn_gen.mzn

This file was deleted.

0 comments on commit cb2c9a5

Please sign in to comment.