OU blog

Personal Blogs

neil

more groups and solitaire

Visible to anyone in the world
Edited by Neil Anderson, Friday, 20 Jan 2012, 18:39

Since my last post I've been thinking. Some messing around found me a pattern, a rather nice one for my purposes. Some background. 

I represent, even triangular Boards, as a HashMap of Pegs, some of these Pegs aren't valid. The hashCode() is just...

return (row * 10) + col;

Boards have rows and cols int properties, it doesn't make sense to rotate a Board if these are different. [Reflexion is another matter which I'll tackle later.]

When I started to think about rotation of a Board things got a little complicated. Take a ∏/2 anti-clockwise rotation. The Peg at 02 goes to 40, 05 goes to 10 etc.. Suddenly it hit me!

Take any row

10 11 12 13 14 15 16

Reverse it

16 15 14 13 12 11 10

Transpose the digits of the reversal

61 51 41 31 21 11 1

And map...

10 11 12 13 14 15 16

61 51 41 31 21 11 01

Which is the right answer and is pretty easy to programme. [Pseudo-code, I need to prove this one, I can't see any boundaries to test against.]

((Board.getRows() - Peg.getCol()) * (Peg.getRow() * 10)) + Peg.getRow();

So now I can programme a ∏/2 anti-clockwise rotation, which means I can compose to get the other rotations. Then I can loop through the pegMaps to see if they are equal (in the sense that they can be rotated onto one-another) and I can compare Boards up-to rotation.

I've come across something like this before; with Groups, conjugate cosets, permutations and equivelence. [I really do need to do some work on my Group Theory, because I know that I'm not describing this properly. I see the pattern, but I've forgotten what it's called, and means.]

Still I was right, there's more Groups to Solitaire than we thought.

 

Permalink Add your comment
Share post
neil

solitaire

Visible to anyone in the world
Edited by Neil Anderson, Wednesday, 7 Dec 2011, 21:32

We get into groups.

Well, I do.

Permalink 1 comment (latest comment by Roo N, Wednesday, 7 Dec 2011, 22:23)
Share post
neil

solitaire

Visible to anyone in the world
In my madness I've decided to air my thoughts about solitaire. This may prove to be a trial to both me and you, my gentle reader. Still done now.
Permalink Add your comment
Share post
neil

Got it I think

Visible to anyone in the world
Edited by Neil Anderson, Wednesday, 10 Aug 2011, 15:20

It's a mess, needs optimized, I need to pseudo-overload it and tidy up my messes, but hey, I have a function that composes permutations!

<html>
<head>
<title>Permutation</title>
</head>
<body>
<script type="text/javascript">
/*
Signature:
Array [Array int || int] X int -->boolean
post:
true if the int is in the permutation
*/

function contains(arr, val){
var found = false, over = false, ret = false
var ix = 0;
while(!found && !over){
if(typeof arr[ix] == 'object'){    //a cycle
var subArr = contains(arr[ix], val);
ret = subArr;
found = subArr;
ix++;
}
else{   
if(arr[ix] == val){
ret = true;
found = true;
}
else if(ix++ >= arr.length){
over = true;
}
}
}
return ret;
}


/*
Signature:
Array int X int --> int
Post:
the int after val
the first int in the array if val is the last member of the array
val if the array doesn't contain int
*/

function findNextInCycle(arr, val){
var len = arr.length;
for(var i = 0; i < len; i++){
if(arr[i] == val){ //we have the value
if(i == len - 1){ //end of the permutation return the first
return arr[0];
}
else{ //return the next value
return arr[i + 1];
}
}
}
return val; //val didn't exist in the permutation
}

/*
Signature:
Array [Array int || int] X int --> boolean
Pre:
Either an array of int representing a single cycle
or an array of array(int) with any fixed points excluded
ie [1,2,3] or [[1,3], [2,4]] but not [[1,2], 3, 4]
the last should be [1,2]
Post:
the int after val
the first int in the array if val is the last member of the array
val if the array doesn't contain int
*/

//uses findNextInCycle
function findNextInPerm(arr, val){
if(contains(arr, val)){
for(var i = 0; i < arr.length; i++){
if(typeof arr[i] == 'object'){ //an array hand it off to findNextInCycle
if(contains(arr[i], val)){
return findNextInCycle(arr[i], val);
} //otherwise we carry on through the cycles
}
else{ //hand the whole thing off to findNextInCycle
return findNextInCycle(arr, val);   
}
}
}
else{
return val;
}
}


/*
Signature:
Array [Array int] --> Array int || Array [Array int]
Post:
If the argument contains only a single array it will return that
Otherwise it will return an array with all single length cycles removed
*/

function fixCycles(arr){
if(arr.length == 1){
return arr[0];
}
else{
var ret = [];
for (cycle in arr){
if(arr[cycle].length > 1){
ret.push(arr[cycle]);
}
}
return ret;
}
}



/*
Signature:
Array [Array int || int] X Array [Array int || int] --> Array Array int
Pre:
The arrays must be either of int representing single cycles
or arrays of array(int) with any fixed points excluded
ie for either array [1,2,3] or [[1,3], [2,4]] but not [[1,2], 3, 4]
the last should be [1,2]
Post:
a composition of permutations
Use the fixCycles function to change this into a single permutaion

Note:
We need len because we may not have all numbers may appear in the perms
*/

function composePerms(first, second, len){
var ret = [], cache = [];
for(var i = 1; i <= len; i++){
if(!contains(ret, i)){
var at = i;
while(!contains(cache, at)){
cache.push(at);
at = findNextInPerm(first, findNextInPerm(second, at));
}   
ret.push(cache);
cache = []; //reset
}
}
return ret;       
}

var x = fixCycles(composePerms([1,2,4,3,5], [1,2,3,5,4], 5));
for(i in x){
document.writeln(x[i]);
} //1 4 2 5 3

// -->
</script>
</body>
</html>

Now I need to write the unit tests, and a test suite.

Permalink 2 comments (latest comment by Neil Anderson, Wednesday, 10 Aug 2011, 20:09)
Share post
neil

failure

Visible to anyone in the world
Edited by Neil Anderson, Tuesday, 9 Aug 2011, 19:27

Today I tried to write some Javascript that would allow me to compose permutations; I got an idea that I wanted to bulk test.

[I should be working on analysis but my mind and fingers itch for groups.]

It was surprisingly hard—because I tackled it stupidly, I didn't write a specification and I've lost my test harness somewhere along the road.

The beast is still in the myth stage.

I will manage this, but my uselessness, after all this study, annoys me hugely. And why do I rely on code [that I can't write] to confirm something that I should be able to prove

Permalink Add your comment
Share post
neil

Colouring

Visible to anyone in the world
Edited by Neil Anderson, Saturday, 30 Jul 2011, 12:38

I should be doing analysis if I'm doing maths, but I've become obsessed by colouring problems. I'm determined to find a general algorithm.

The problem involves a lot of subtleties; take, for example a hexagon, how many ways are there to divide it into 2, 3, 4, 6, equal areas, can we divide into 5? Are primes involved here?

For the first time in ages I'm working at something for myself, and working at maths with a view to writing a programme. This is good.

I won't manage this, but sometimes the exercise is more important than the result. And in the back of my head I'm thinking solitaire.

Permalink 2 comments (latest comment by Neil Anderson, Sunday, 31 Jul 2011, 05:52)
Share post
neil

Sunny day thoughts

Visible to anyone in the world
Much the same as my usual ones.
Permalink 6 comments (latest comment by Neil Anderson, Wednesday, 6 Jul 2011, 20:35)
Share post

This blog might contain posts that are only visible to logged-in users, or where only logged-in users can comment. If you have an account on the system, please log in for full access.

Total visits to this blog: 252598