/// <summary>
 ///     上班
 /// </summary>
 public static void StartWork(SimulatorBase sender, CheckpointArgs args)
 {
     ((Staff)sender).IsWorking = true;
     StaffSQLController.CheckSql((Staff)sender, true);
     Debug.WriteLine(((Staff)sender).StaffName + "上班!");
     SimulatorTimer.SpeedSetNormal();
 }
Example #2
0
        public async Task <bool> QuantumSolve(int[,] puzzle, SimulatorBase sim)
        {
            int size = puzzle.GetLength(0);

            FindEdgesAndInitialNumberConstraints(
                puzzle,
                size,
                out var emptySquareEdges,
                out var startingNumberConstraints,
                out var emptySquares
                );
            var emptySquareEdgesQArray          = new QArray <(long, long)>(emptySquareEdges);
            var startingNumberConstraintsQArray = new QArray <(long, long)>(startingNumberConstraints);

            var(foundSolution, solution) = await SolvePuzzle.Run(sim, emptySquares.Count, size, emptySquareEdgesQArray, startingNumberConstraintsQArray);

            if (foundSolution)
            {
                foreach (var(emptySquare, completion) in Enumerable.Zip(emptySquares, solution))
                {
                    puzzle[emptySquare.Row, emptySquare.Column] = (int)completion + 1;
                }
                Console.WriteLine("Solved puzzle.");
            }
            return(foundSolution);
        }
        public Checkpoint(string checkpointNumber, DateTime checkpointTime,
                          string[] checkpointProcessIndicator,
                          string[] checkpointUpdateIndicator, string checkpointCheckMethodIndicator,
                          SimulatorBase checkpointTransferObject, CheckpointArgs checkpointTransferArgs,
                          string checkpointTypeIndicator, bool checkpointConstancy, bool checkpointCanBeSaved)
        {
            CheckpointNumber = checkpointNumber;
            CheckpointTime   = checkpointTime;

            CheckpointProcessIndicators = checkpointProcessIndicator;

            CheckpointUpdateIndicators     = checkpointUpdateIndicator;
            CheckpointCheckMethodIndicator = checkpointCheckMethodIndicator;
            CheckpointTransferObject       = checkpointTransferObject;

            CheckpointTransferArgs  = checkpointTransferArgs;
            CheckpointTypeIndicator = checkpointTypeIndicator;

            CheckpointIsConstant = checkpointConstancy;
            CheckpointCanBeSaved = checkpointCanBeSaved;

            _checkFinishMethod = (sender, args) => false;


            GenerateCheckpoint();
        }
Example #4
0
        public static void UpdateSales(SimulatorBase sender, CheckpointArgs args)
        {
            var game     = (Game)sender;
            int tmpSales = (int)((game.GameArt + game.GameMusic) * game.GameFun * 0.1 * Math.Abs(1 / (SimulatorTimer.GameTimeNow - game.GameFinishDevelopTime).TotalDays));

            game.GameSales += tmpSales;
            game.GameStudioObject.StudioProperty += game.GamePrice * tmpSales;
        }
        /// <summary>
        ///     查看是否上班
        /// </summary>
        public static bool CheckStartWork(SimulatorBase sender, CheckpointArgs args)
        {
            var staff = (Staff)sender;

            // Debug.WriteLine(staff.StaffCurStrength);
            return(!staff.IsWorking && staff.IsWeekday() && staff.StaffCurStrength > 10 &&
                   staff.IsInWorkTime());
        }
Example #6
0
        public static void SettleMaintenance(SimulatorBase sender, CheckpointArgs args)
        {
            Studio studio = (Studio)sender;
            var    cost   = (from staff in studio.StudioStaffs
                             select staff.StaffSalary).Aggregate(0, (current, s) => current + s);

            studio.ChangeProperty(-cost);
            Debug.WriteLine("已扣维护费 ¥" + cost);
        }
Example #7
0
        /// <summary>
        ///     开发更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void UpdateDevelop(SimulatorBase sender, CheckpointArgs args)
        {
            var game = (Game)sender;

            game._developmentProcess = args.UpdateParm;

            float artCapability = (from developer in game.Developers
                                   where developer.IsWorking && developer.StaffOccupation == Occupation.Artist
                                   select developer.StaffIntelligence).Aggregate(0, (current, s)
                                                                                 => current + s) / 100f;

            float musicCapability = (from developer in game.Developers
                                     where developer.IsWorking && developer.StaffOccupation == Occupation.Musician
                                     select developer.StaffIntelligence).Aggregate(0, (current, s)
                                                                                   => current + s) / 100f;

            float funCapability = (from developer in game.Developers
                                   where developer.IsWorking && developer.StaffOccupation == Occupation.Designer
                                   select developer.StaffIntelligence).Aggregate(0, (current, s)
                                                                                 => current + s) / 100f;

            float programCapability = (from developer in game.Developers
                                       where developer.IsWorking && developer.StaffOccupation == Occupation.Programmer
                                       select developer.StaffIntelligence).Aggregate(0, (current, s)
                                                                                     => current + s) / 100f;

            float controlCapability = (from developer in game.Developers
                                       where developer.IsWorking && developer.StaffOccupation == Occupation.Producer
                                       select developer.StaffIntelligence).Aggregate(0, (current, s)
                                                                                     => current + s) / 100f;


            game.GameArt   = (int)(((float)game.GameArt) + artCapability * (1 + controlCapability) * (1 + programCapability * 0.7));
            game.GameMusic = (int)(((float)game.GameMusic) + musicCapability * (1 + controlCapability) * (1 + programCapability * 0.7));
            game.GameFun   = (int)(((float)game.GameFun) + funCapability * (1 + controlCapability) * (1 + programCapability * 0.7));

            args.ArtParm   = game.GameArt;
            args.MusicParm = game.GameMusic;
            args.FunParm   = game.GameFun;


            Debug.WriteLine(game.GameName + " Processing:" + args.UpdateParm + "%. In " + SimulatorTimer.GameTimeNow);
            Debug.WriteLine(game.GameName + " ArtParm:" + args.ArtParm + ". In " + SimulatorTimer.GameTimeNow);
            Debug.WriteLine(game.GameName + " GameArt:" + game.GameArt + ". In " + SimulatorTimer.GameTimeNow);
            Debug.WriteLine(game.GameName + " GameArt Temp:" + (int)(((float)game.GameFun + funCapability) * (1 + controlCapability) * (0.5f + programCapability)) + ". In " + SimulatorTimer.GameTimeNow);
            Debug.WriteLine(game.GameName + " GameArt Capability:" + artCapability + ". In " + SimulatorTimer.GameTimeNow);
            args.UpdateParm += args.UpdateSpeed;


            //支付开发经费
            int cost = (from gameDeveloper in game.Developers
                        select gameDeveloper.StaffSalary).Aggregate(0, (current, s) => current + s);

            game.GameStudioObject.ChangeProperty(-cost / 10);

            UpdateDevelopEvent?.Invoke(sender, args);
        }
Example #8
0
 internal static SimulatorEventDisposer RegisterOperationHandlers(
     this SimulatorBase simulator,
     Action <ICallable, IApplyData> startOperation,
     Action <ICallable, IApplyData>?endOperation = null
     ) => new SimulatorEventDisposer(
     simulator,
     startOperation,
     endOperation ?? ((callable, data) => {})
     );
        private static void InitSimulator(SimulatorBase sim)
        {
            sim.InitBuiltinOperations(typeof(OperationsTestHelper));
            sim.Register(typeof(Tests.Circuits.Generics.Trace <>), typeof(TraceImpl <>), typeof(IUnitary));

            // For Toffoli, replace H with I.
            if (sim is ToffoliSimulator)
            {
                sim.Register(typeof(Intrinsic.H), typeof(Intrinsic.I), typeof(IUnitary));
            }
        }
Example #10
0
 private static void RunBellTest(SimulatorBase simulator)
 {
     // Try initial values
     Result[] initials = new Result[] { Result.Zero, Result.One };
     foreach (Result initial in initials)
     {
         var res = BellTest.Run(simulator, 1000, initial).Result;
         var(numZeros, numOnes, agree) = res;
         Console.WriteLine($"Init:{initial,-4} 0s={numZeros,-4} 1s={numOnes,-4} agree={agree,-4}");
     }
 }
        /// <summary>
        ///     查看是否下班
        /// </summary>
        public static bool CheckQuitWork(SimulatorBase sender, CheckpointArgs args)
        {
            var staff = (Staff)sender;

            return(staff.IsWorking
                   &&
                   (
                       staff.StaffCurStrength <= 0
                       ||
                       staff.IsWeekday() && !staff.IsInWorkTime()
                   ));
        }
        public static void WorkUpdate(SimulatorBase sender, CheckpointArgs args)
        {
            var staff = (Staff)sender;

            if (staff.IsWorking && staff.StaffCurStrength > 0)
            {
                staff.StaffCurStrength -= 1 * SimulatorTimer.TimeSpeed;
                if (staff.StaffCurStrength < 0)
                {
                    staff.StaffCurStrength = 0;
                }
            }
        }
        public static void QuitUpdate(SimulatorBase sender, CheckpointArgs args)
        {
            var staff = (Staff)sender;

            if (!staff.IsWorking && staff.StaffCurStrength < staff.StaffStrength)
            {
                staff.StaffCurStrength += 1 * SimulatorTimer.TimeSpeed;
                if (staff.StaffCurStrength > staff.StaffStrength)
                {
                    staff.StaffCurStrength = staff.StaffStrength;
                }
            }
        }
Example #14
0
        public SimulatorEventDisposer(
            SimulatorBase simulator,
            Action <ICallable, IApplyData> startOperation,
            Action <ICallable, IApplyData> endOperation
            )
        {
            Simulator      = simulator;
            StartOperation = startOperation;
            EndOperation   = endOperation;

            Simulator.OnOperationStart += startOperation;
            Simulator.OnOperationEnd   += endOperation;
        }
        /// <summary>
        ///     下班
        /// </summary>
        public static void QuitWork(SimulatorBase sender, CheckpointArgs args)
        {
            var staff = (Staff)sender;

            staff.IsWorking = false;
            StaffSQLController.CheckSql(staff, false);

            Debug.WriteLine(((Staff)sender).StaffName + "下班!");
            if (staff.StaffStudioObject.FindWorkingStaffs().Count == 0)
            {
                SimulatorTimer.SpeedSetQuick();
            }
        }
        /// <summary>
        /// A shell for simple Apply tests.
        /// </summary>
        internal static void applyTestShell <I, O>(SimulatorBase sim, ICallable <I, O> operation, Action <Qubit> test)
        {
            var allocate = sim.Get <Intrinsic.Allocate>();
            var release  = sim.Get <Intrinsic.Release>();

            var qbits = allocate.Apply(1);

            var q = qbits[0];

            operation.Apply(q);    // this is ok.

            test(q);

            operation.Apply(q);    // still ok.
            release.Apply(qbits);
        }
Example #17
0
        /// <summary>
        ///     开发结束
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public static void EndDevelop(SimulatorBase sender, CheckpointArgs args)
        {
            var game = (Game)sender;

            game.GameIsDeveloping = false;
            game.GameStudioObject.AddDevelopedGame(game);
            game.GameStudioObject.RemoveDevelopingGame(game);
            game.GameFinishDevelopTime = SimulatorTimer.GameTimeNow;
            game.UpdateSql();

            game.StartSales();

            // GameSQLController.UpdateGameInfoSql(GameSQLController.ReadGameInfoSql(game.GameNumber), game);
            Debug.WriteLine(game.GameName + " Game FINISHED!");
            EndDevelopEvent?.Invoke(sender, args);
        }
Example #18
0
    public void SetSimulator()
    {
        switch (UsedSimulator)
        {
        case SimulatorType.Micro:
            simulator = new MicroQiskitSimulator();
            break;

        case SimulatorType.PythonMicro:
            simulator = new PythonMicroQiskitSimulator();
            break;

        default:
            break;
        }
    }
        public static void RunWithMultipleSimulators(Action <SimulatorBase> test)
        {
            var simulators = new SimulatorBase[] { new QuantumSimulator() };

            foreach (var s in simulators)
            {
                try
                {
                    InitSimulator(s);

                    test(s);
                }
                finally
                {
                    if (s is IDisposable sim)
                    {
                        sim.Dispose();
                    }
                }
            }
        }
 public StartTracker(SimulatorBase s)
 {
     s.OnOperationStart += this.OnStart;
 }
 public static TraceImpl <T> GetTracer <T>(this SimulatorBase s)
 {
     return(s.Get <GenericCallable>(typeof(Tests.Circuits.Generics.Trace <>)).FindCallable(typeof(T), typeof(QVoid)) as TraceImpl <T>);
 }
Example #22
0
 public static void EndSales(SimulatorBase sender, CheckpointArgs args)
 {
 }
Example #23
0
 public static bool CheckMaintenance(SimulatorBase sender, CheckpointArgs args) => SimulatorTimer.GameTimeNow.Day == 1;
Example #24
0
 public static bool CheckSalesEnd(SimulatorBase sender, CheckpointArgs args) => false;
 private static void InitSimulator(SimulatorBase sim)
 {
     sim.InitBuiltinOperations(typeof(OperationsTestHelper));
 }
        //Helper functions, which need python code, thats why they are not exported to QuantumImageHelper
        #region Internal Helper Functions


        /// <summary>
        /// Getting a colored texture for given quantum circuits (each one representing 1 color channel of an image) directly without using python.
        /// Is faster than python versions but does not support logarithmic encoding yet and may still contain some errors.
        /// </summary>
        /// <param name="redCircuit">The quantum circuit which represents the red channel of the image.</param>
        /// <param name="greenCircuit">The quantum circuit which represents the green channel of the image.</param>
        /// <param name="blueCircuit">The quantum circuit which represents the blue channel of the image.</param>
        /// <param name="width">The width of the image</param>
        /// <param name="height">The height of the image</param>
        /// <param name="renormalize">If the image (colors) should be renormalized. (Giving it the highest possible saturation / becomes most light) </param>
        /// <param name="useLog">If logarithmic encoding is chosen DOES NOTHING (at the moment)</param>
        /// <returns>A texture showing the encoded image.</returns>
        public static Texture2D GetColoreTextureDirect(QuantumCircuit redCircuit, QuantumCircuit greenCircuit, QuantumCircuit blueCircuit, int width, int height,
                                                       bool renormalize = false, bool useLog = false, SimulatorBase simulator = null)
        {
            double[,] redData   = QuantumImageHelper.CircuitToHeight2D(redCircuit, width, height, renormalize, simulator);
            double[,] greenData = QuantumImageHelper.CircuitToHeight2D(greenCircuit, width, height, renormalize, simulator);
            double[,] blueData  = QuantumImageHelper.CircuitToHeight2D(blueCircuit, width, height, renormalize, simulator);

            return(QuantumImageHelper.CalculateColorTexture(redData, greenData, blueData));
        }
        /// <summary>
        /// Getting a grey scale texture for a given quantum circuit (which should represent an image) directly without using python.
        /// Is faster than python versions but does not support logarithmic encoding yet and may still contain some errors.
        /// </summary>
        /// <param name="quantumCircuit">The quantum circuit with the grey scale image representation</param>
        /// <param name="width">The width of the image</param>
        /// <param name="height">The height of the image</param>
        /// <param name="renormalize">If the image (colors) should be renormalized. (Giving it the highest possible saturation / becomes most light) </param>
        /// <param name="useLog">If logarithmic encoding is chosen DOES NOTHING (at the moment)</param>
        /// <returns>A texture showing the encoded image.</returns>
        public static Texture2D GetGreyTextureDirect(QuantumCircuit quantumCircuit, int width, int height, bool renormalize = false, bool useLog = false, SimulatorBase simulator = null)
        {
            //TODO Make version with only floats (being faster needing less memory)
            double[,] imageData = QuantumImageHelper.CircuitToHeight2D(quantumCircuit, width, height, renormalize, simulator);

            return(QuantumImageHelper.CalculateGreyTexture(imageData));
        }
        /// <summary>
        /// 从XElement中读取Checkpoint
        /// </summary>
        /// <param name="xe"></param>
        /// <param name="player"></param>
        /// <returns></returns>
        public static Checkpoint ReadCheckpointXml(XElement xe, Player.Player player)
        {
            var checkpointProcessIndicators =
                (from element in xe.Element("CheckpointProcessIndicators")
                 ?.Elements("CheckpointProcessIndicator")
                 select element.Attribute("target")?.Value).ToArray();

            var checkpointUpdateIndicators =
                (from element in xe.Element("CheckpointUpdateIndicators")
                 ?.Elements("CheckpointUpdateIndicator")
                 select element.Attribute("target")?.Value).ToArray();

            // var args =
            //     new
            //         CheckpointArgs(int.Parse(xe.Element("CheckpointTransferArgs")?.Element("CheckParm")?.FirstAttribute.Value
            //                               ?? throw new InvalidOperationException()),
            //                        int.Parse(xe.Element("CheckpointTransferArgs")?.Element("CheckParm")?.FirstAttribute.Value
            //                               ?? throw new InvalidOperationException()),
            //                        int.Parse(xe.Element("CheckpointTransferArgs")?.Element("UpdateSpeed")?.FirstAttribute.Value
            //                               ?? throw new InvalidOperationException())
            //                       );

            CheckpointArgs args = null;

            using (StringReader sr = new StringReader(xe.Element("CheckpointTransferArgs")?.Element("CheckpointArgs")?.ToString() ?? throw new InvalidOperationException()))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(CheckpointArgs));
                args = serializer.Deserialize(sr) as CheckpointArgs;
            }


            SimulatorBase obj = null;

            switch (xe.Attribute("CheckpointTypeIndicator")?.Value)
            {
            case "Game":
                //TODO 这里Game的加载也不太行
                obj =
                    player.PlayerStudio.FindGame(xe.Attribute("CheckpointTransferObject")?.Value);
                if (((Game.Game)obj).GameIsDeveloping)
                {
                    ((Game.Game)obj).GameArt   = args.ArtParm;
                    ((Game.Game)obj).GameMusic = args.MusicParm;
                    ((Game.Game)obj).GameFun   = args.FunParm;
                }
                break;

            case "Staff":
                obj =
                    PageBase.FindStaff(xe.Attribute("CheckpointTransferObject")?.Value);
                break;

            case "Studio":
                // TODO 这里的Studio读取方式不行,要优化
                obj =
                    PageBase.FindStudio(xe.Attribute("CheckpointTransferObject")?.Value);
                break;
            }

            return(new
                   Checkpoint(xe.Attribute("CheckpointNumber")?.Value ?? throw new InvalidOperationException(),
                              DateTime.Parse(xe.Attribute("CheckpointTime")?.Value),
                              checkpointProcessIndicators,
                              checkpointUpdateIndicators,
                              xe.Attribute("CheckpointCheckMethodIndicator")?.Value,
                              obj,
                              args,
                              xe.Attribute("CheckpointTypeIndicator")?.Value,
                              bool.Parse(xe.Attribute("CheckpointConstancy")?.Value ?? throw new InvalidOperationException()),
                              true
                              ));
        }
Example #29
0
 /// <summary>
 ///     This method is a wrapper to let the tests keep using a one Type parameter
 ///     method to fetch for Gates.
 /// </summary>
 public static T Get <T>(this SimulatorBase sim) where T : AbstractCallable
 {
     return(sim.Get <T, T>());
 }
Example #30
0
 private static void Run(SimulatorBase simulator)
 {
     RunTest(simulator);
     // RunBellTest(simulator);
 }