コード例 #1
0
            public static void VerifyGroupActionAssociaty()
            {
                for (int caseIdx = 0; caseIdx < 100; caseIdx++)
                {
                    CubeAction a = CubeAction.Random(Utils.GlobalRandom.Next(1, 20));
                    CubeAction b = CubeAction.Random(Utils.GlobalRandom.Next(1, 15));
                    CubeAction c = CubeAction.Random(Utils.GlobalRandom.Next(1, 10));

                    CubeAction ab = a.Mul(b);
                    CubeAction bc = b.Mul(c);

                    CubeState origState = CubeAction.RandomCube(Utils.GlobalRandom.Next(1, 20));

                    CubeState a_b_c_state = new CubeState(origState);
                    c.Act(a_b_c_state);
                    b.Act(a_b_c_state);
                    a.Act(a_b_c_state);

                    CubeState ab_c_state = new CubeState(origState);
                    c.Act(ab_c_state);
                    ab.Act(ab_c_state);

                    CubeState a_bc_state = new CubeState(origState);
                    bc.Act(a_bc_state);
                    a.Act(a_bc_state);

                    Utils.DebugAssert(a_b_c_state.Equals(ab_c_state));
                    Utils.DebugAssert(a_b_c_state.Equals(a_bc_state));
                }
            }
コード例 #2
0
    protected void DetermineNotChangedAndDeltas( Context context, Context.ConnectionData connectionData, ushort currentSequence, int numCubes, ref int[] cubeIds, ref bool[] notChanged, ref bool[] hasDelta, ref ushort[] baselineSequence, ref CubeState[] cubeState, ref CubeDelta[] cubeDelta )
    {
        Profiler.BeginSample( "DeterminedNotChangedAndDeltas" );

#if !DISABLE_DELTA_COMPRESSION
        CubeState baselineCubeState = CubeState.defaults;
#endif // #if !DISABLE_DELTA_COMPRESSION

        for ( int i = 0; i < numCubes; ++i )
        {
            notChanged[i] = false;
            hasDelta[i] = false;

#if !DISABLE_DELTA_COMPRESSION

#if DEBUG_DELTA_COMPRESSION
            cubeDelta[i].absolute_position_x = cubeState[i].position_x;
            cubeDelta[i].absolute_position_y = cubeState[i].position_y;
            cubeDelta[i].absolute_position_z = cubeState[i].position_z;
#endif // #if DEBUG_DELTA_COMPRESSION

            if ( context.GetMostRecentAckedState( connectionData, cubeIds[i], ref baselineSequence[i], context.GetResetSequence(), ref baselineCubeState ) )
            {
                if ( Network.Util.BaselineDifference( currentSequence, baselineSequence[i] ) > Constants.MaxBaselineDifference )
                {
                    // baseline is too far behind => send the cube state absolute.
                    continue;
                }

                if ( baselineCubeState.Equals( cubeState[i] ) )
                {
                    notChanged[i] = true;
                }
                else
                {
                    hasDelta[i] = true;

                    cubeDelta[i].position_delta_x = cubeState[i].position_x - baselineCubeState.position_x;
                    cubeDelta[i].position_delta_y = cubeState[i].position_y - baselineCubeState.position_y;
                    cubeDelta[i].position_delta_z = cubeState[i].position_z - baselineCubeState.position_z;

                    cubeDelta[i].linear_velocity_delta_x = cubeState[i].linear_velocity_x - baselineCubeState.linear_velocity_x;
                    cubeDelta[i].linear_velocity_delta_y = cubeState[i].linear_velocity_y - baselineCubeState.linear_velocity_y;
                    cubeDelta[i].linear_velocity_delta_z = cubeState[i].linear_velocity_z - baselineCubeState.linear_velocity_z;

                    cubeDelta[i].angular_velocity_delta_x = cubeState[i].angular_velocity_x - baselineCubeState.angular_velocity_x;
                    cubeDelta[i].angular_velocity_delta_y = cubeState[i].angular_velocity_y - baselineCubeState.angular_velocity_y;
                    cubeDelta[i].angular_velocity_delta_z = cubeState[i].angular_velocity_z - baselineCubeState.angular_velocity_z;
                }
            }

#endif // #if !DISABLE_DELTA_COMPRESSION
        }

        Profiler.EndSample();
    }
コード例 #3
0
            public List <Tuple <CubeAction, CubeState> > SolveCube(CubeState puzzleState)
            {
                if (SolvingMap.Count <= 0)
                {
                    throw new ArgumentException();
                }

                var originalPuzzleState = new CubeState(puzzleState);
                var ret = new List <Tuple <CubeAction, CubeState> >();

                ret.Add(new Tuple <CubeAction, CubeState>(new CubeAction(), new CubeState(puzzleState)));

                for (int gIdx = 0; gIdx < SolvingMap.Count; gIdx++)
                {
                    var g = SolvingMap[gIdx];
                    if (null == g.OrbitToCoset)
                    {
                        for (int gIdx_inner = gIdx + 1; gIdx_inner < SolvingMap.Count; gIdx_inner++)
                        {
                            Utils.DebugAssert(null == SolvingMap[gIdx_inner].OrbitToCoset);
                        }

                        continue;
                    }

                    var observed = new BlockSet(puzzleState, g.ToStablize.Indexes);
                    if (!g.OrbitToCoset.ContainsKey(observed))
                    {
                        throw new ArgumentException();
                    }

                    var cosetRepresentative = g.OrbitToCoset[observed];
                    var rCosetRepr          = cosetRepresentative.Reverse();

                    rCosetRepr.Act(observed.State);
                    puzzleState = observed.State;

                    Utils.DebugAssert(g.OrbitToCoset.ContainsKey(observed));
                    Utils.DebugAssert(g.OrbitToCoset[observed].Equals(new CubeAction()));

                    ret.Add(new Tuple <CubeAction, CubeState>(rCosetRepr, new CubeState(observed.State)));
                }

                {
                    var trialState = new CubeState(originalPuzzleState);
                    foreach (var pair in ret)
                    {
                        var action = pair.Item1;
                        action.Act(trialState);
                    }
                    Utils.DebugAssert(trialState.Equals(new CubeState()));
                }
                return(ret);
            }
コード例 #4
0
            private bool EqualAct(CubeAction obj)
            {
                var stateThis = new CubeState();
                var stateObj  = new CubeState();

                this.Act(stateThis);
                obj.Act(stateObj);

                bool actEqual = stateThis.Equals(stateObj);

                return(actEqual);
            }
コード例 #5
0
            private bool EqualOps(CubeAction obj)
            {
                var stateThis = new CubeState();
                var stateObj  = new CubeState();

                this.ActOps(stateThis);
                obj.ActOps(stateObj);

                bool opsEqual = stateThis.Equals(stateObj);

                return(opsEqual);
            }
コード例 #6
0
            public static void VerifyCubeTurnAround()
            {
                for (int caseIdx = 0; caseIdx < 100; caseIdx++)
                {
                    CubeState cubeState = CubeAction.RandomCube(Utils.GlobalRandom.Next(1, 30));
                    foreach (CubeOp.Type op in Enum.GetValues(typeof(CubeOp.Type)))
                    {
                        var       action       = new CubeAction(new int[] { (int)op });
                        CubeState newCubeState = new CubeState(cubeState);

                        for (int i = 0; i < CubeState.TurnAround; i++)
                        {
                            action.Act(newCubeState);
                        }

                        Utils.DebugAssert(newCubeState.Equals(cubeState));
                    }
                }

                for (int caseIdx = 0; caseIdx < 100; caseIdx++)
                {
                    CubeState cubeState = CubeAction.RandomCube(Utils.GlobalRandom.Next(1, 30));
                    foreach (CubeOp.Type op in Enum.GetValues(typeof(CubeOp.Type)))
                    {
                        var       action       = new CubeAction(new int[] { (int)op, (int)op, (int)op });
                        CubeState newCubeState = new CubeState(cubeState);

                        for (int i = 0; i < CubeState.TurnAround; i++)
                        {
                            action.Act(newCubeState);
                        }

                        Utils.DebugAssert(newCubeState.Equals(cubeState));
                    }
                }

                for (int caseIdx = 0; caseIdx < 100; caseIdx++)
                {
                    CubeState cubeState = CubeAction.RandomCube(Utils.GlobalRandom.Next(1, 30));
                    foreach (CubeOp.Type op in Enum.GetValues(typeof(CubeOp.Type)))
                    {
                        var       action       = new CubeAction(new int[] { (int)op, (int)op });
                        CubeState newCubeState = new CubeState(cubeState);

                        for (int i = 0; i < CubeState.TurnAround / 2; i++)
                        {
                            action.Act(newCubeState);
                        }

                        Utils.DebugAssert(newCubeState.Equals(cubeState));
                    }
                }
            }
コード例 #7
0
            private void VerifyAccelerationMap()
            {
                if (!Utils.ShouldVerify())
                {
                    return;
                }

                var actionCubeState = new CubeState();

                ActOps(actionCubeState);

                var mapCubeState = new CubeState();

                AccelerationMap.Act(mapCubeState);

                Utils.DebugAssert(mapCubeState.Equals(actionCubeState));
            }
コード例 #8
0
            private static void LazyInitActionShrink()
            {
                if (ActionShrinkDict != null)
                {
                    return;
                }

                // Bigger than 5 may OOM on my desktop
                const int DICT_SCAN_ROUNDS = 5;
                // To aoivd OOM, exclude reflect and inverse ops after certain rounds
                const int DICT_SCAN_REDUCED_ROUND = 4;

                ActionShrinkDict = new Dictionary <CubeState, CubeAction>()
                {
                    { new CubeState(), new CubeAction() }
                };

                var fullyWalkedStates = new HashSet <CubeState>();

                for (int round = 0; round < DICT_SCAN_ROUNDS; round++)
                {
                    var needWalkStates = new HashSet <CubeState>(ActionShrinkDict.Keys);
                    needWalkStates.RemoveWhere(x => fullyWalkedStates.Contains(x));

                    int foundCount  = 0;
                    int walkedCount = 0;
                    foreach (var state in needWalkStates)
                    {
                        foreach (CubeOp.Type op in Enum.GetValues(typeof(CubeOp.Type)))
                        {
                            var action      = ActionShrinkDict[state];
                            int sameOpCount = 0;
                            for (int i = action.Ops.Count - 1; i >= 0; i--)
                            {
                                if (action.Ops[i] == op)
                                {
                                    sameOpCount++;
                                }
                                else
                                {
                                    break;
                                }
                            }

                            var newStateBase = new CubeState(state);
                            int turnCount    = CubeState.TurnAround - sameOpCount - 1;
                            if (round >= DICT_SCAN_REDUCED_ROUND)
                            {
                                turnCount = 1;
                            }

                            // Scanning while including the reflect and reverse operations
                            for (int turn = 1; turn <= turnCount; turn++)
                            {
                                CubeOp.Op(newStateBase, op);

                                if (!ActionShrinkDict.ContainsKey(newStateBase))
                                {
                                    var newState  = new CubeState(newStateBase);
                                    var newAction = new CubeAction(action);
                                    newAction.Ops.InsertRange(0, Enumerable.Repeat(op, turn));

                                    ActionShrinkDict.Add(newState, newAction);
                                    foundCount++;
                                }
                            }
                        }
                        walkedCount++;
                    }
                    fullyWalkedStates.UnionWith(needWalkStates);

                    Console.WriteLine(
                        $"LazyInitActionShrink: round={round} foundCount={foundCount} " +
                        $"total={ActionShrinkDict.Count}");
                }

                //
                // Verifying
                //

                foreach (var kv in ActionShrinkDict)
                {
                    var state = new CubeState();
                    kv.Value.ActOps(state);
                    Utils.DebugAssert(state.Equals(kv.Key));

                    Utils.DebugAssert(!kv.Value.RefMode);
                }
            }