예제 #1
0
 public MapGenerator2(DungeonParameter param, MonoBehaviour obj,RndGenerator rnd)
 {
     DgParam = param;
     this.obj = obj;
     Rnd = rnd;
     reset();
 }
        public ComputeBuffer Generate()
        {
            ComputeBuffer rndGenerators = new ComputeBuffer(2, 20);

            RndGenerator[] gen = new RndGenerator[2];
            gen[0] = new RndGenerator();
            gen[1] = new RndGenerator();

            int s = seed;

            s         = xorshift(s) % 99;
            gen[0].wx = (s + 0.25356234f) / 22.03460124817f;

            s         = xorshift(s * s - 9173571) % 99;
            gen[0].wy = (s - 0.6913404f) / 28.81307123f;

            s        = xorshift(s / 814 + 5134071) % 99;
            gen[0].m = (s - 0.6913404f) / 40.9341705134f;

            s = xorshift(s * 134 - 515690871) % 99;
            gen[0].offsetx = (s + 5.5462f) / 10.012386134f;

            s = xorshift(s * s / 17 + 90151346) % 99;
            gen[0].offsety = (s - 8.51342f) / 51.07349237283f;


            s         = xorshift(s * 71924 - 127823) % 99;
            gen[1].wx = (s + 0.25356234f) / 41.03450124817f;

            s         = xorshift(s * s - 9173571) % 99;
            gen[1].wy = (s - 0.9913404f) / 13.808237123f;

            s        = xorshift(s / 814 + 5134071) % 99;
            gen[1].m = (s - 9.2913404f) / 30.9031705134f;

            s = xorshift(s * 134 - 515690871) % 99;
            gen[1].offsetx = (s + 1.63462f) / 52.92086134f;

            s = xorshift(s * s / 17 + 90151346) % 99;
            gen[1].offsety = (s - 2.512642f) / 31.07349237283f;


            rndGenerators.SetData(gen);

            ComputeBuffer gradients = new ComputeBuffer(size * size, 8);

            gradientGeneratorCompute.SetBuffer(0, "gradient", gradients);
            gradientGeneratorCompute.SetBuffer(0, "rndGenerator", rndGenerators);

            gradientGeneratorCompute.SetInt("size", size);
            gradientGeneratorCompute.SetFloat("scale", scale);

            int threadGroup = Mathf.CeilToInt(size / 8f);

            gradientGeneratorCompute.Dispatch(0, threadGroup, threadGroup, 1);

            rndGenerators.Release();

            return(gradients);
        }
예제 #3
0
 //private static bool startup = false;
 public GameController()
 {
     Debug.Log("GameController.");
     CurrentGameMode = GameMode.NORMAL;
     GameControlProxy.add(this);
     WindowConfigure.initAll();
     if (Rnd == null) Rnd = new RndGenerator((int)System.DateTime.Now.Ticks);
     if (GameActionEvent == null) GameActionEvent = new GameActionEvent();
     if (PlayingLogger == null) PlayingLogger = new PlayLogger();
     if (DungeonInformation == null) DungeonInformation = new DungeonInformation();
     if (ObjectList == null) ObjectList = new ObjectList();
     if (DgParameterTable == null) DgParameterTable = new DungeonParameterTable(5);
     if (SoundController == null) SoundController = SoundController.Instance;
     if (TempGameState == null) TempGameState = new Stack<GameState>();
     if (ActCommandController == null) ActCommandController = new ActionCommandController();
     if (GlobalGameInformation == null) GlobalGameInformation = new GlobalGameInformation();
     if (SystemConfigure == null) SystemConfigure = new SystemConfigure();
     if (SystemConfigureMenu == null) SystemConfigureMenu = new GameSystemConfigureMenu();
     DataSaveSystem.addGameActionEvent();
     //var t = new TagList();
     SaveDataConverter.add(this);
     //PlayerUIController.setVisible(false);
     gameStateChange(GameState.PRELOAD);
     filedModeChange(FieldMode.TOWN);
 }
 /** Method: Constructor  */
 internal MontecarloConv(int maxIt)
 {
     rg         = new RndGenerator();
     convData   = new List <double>();
     hist       = new Histogram(100);
     de         = new KernelDensity(1, 100, 100);
     this.maxIt = 2000; //TODO: eliminate parameter
 }
예제 #5
0
        public List <ValueTuple <int, int, int> > BuildHiddenSet()
        {
            var rows = new List <ValueTuple <int, int, int> >();

            for (int i = 0; i < 200; i++)
            {
                var x = RndGenerator.RndInt32(0, 40);
                var y = RndGenerator.RndInt32(0, 40);
                rows.Add((x, y, HiddenFunction(x, y)));
            }
            return(rows);
        }
예제 #6
0
        internal MontecarloGenericConv(int maxIt, int normMin)
        {
            distributions = new List <Distribution>();
            de            = new KernelDensity(1, 100, 100);
            nd            = new NormalDistrib();
            rg            = new RndGenerator();
            this.maxIt    = maxIt;
            this.normMin  = normMin;

            this.mean  = -1;
            this.stDev = -1;
        }
 private static void CreateVehiclesList()
 {
     vehicles = new List <VehicleInfo>();
     for (int i = 0; i < 10000; i++)
     {
         var brand   = brands.ElementAt(RndGenerator.Next(brands.Count));
         var model   = brand.Item2.ElementAt(RndGenerator.Next(brand.Item2.Count));
         var vehicle = new VehicleInfo()
         {
             Brand     = brand.Item1,
             Model     = model.Item1,
             DailyCost = model.Item2,
             Plate     = GenerateNewPlate(vehicles.Where(v => v.Plate != null).Select(v => v.Plate)),
             State     = VehicleState.Free
         };
         vehicles.Add(vehicle);
     }
 }
예제 #8
0
        public Expression CrossOver(Expression t1, Expression t2, double probswap = 0.7, bool top = true)
        {
            if (RndGenerator.RndDouble() < probswap && !top)
            {
                return(t2);
            }
            var result         = t1;
            var childrenExpsT1 = GetChildren(t1);
            var childrenExpsT2 = GetChildren(t2);

            if (childrenExpsT1 == null || childrenExpsT2 == null)
            {
                return(result);
            }
            var newChildren = new List <Expression>();

            foreach (var expression in childrenExpsT1)
            {
                newChildren.Add(CrossOver(expression, childrenExpsT2[RndGenerator.RndInt32(0, childrenExpsT2.Count)], probswap, false));
            }
            return(UpdateChildren(result, newChildren));
        }
예제 #9
0
        public static Expression MakeRandomTree(ParameterExpression[] paramExps, int maxTreeDepth = 4,
                                                double fpr = 0.5, double ppr = 0.6)
        {
            if (RndGenerator.RndDouble() < fpr && maxTreeDepth > 0)
            {
                (var funcExp, var pc) = ExpFactory.Choice();
                var children = new Expression[pc];
                for (int i = 0; i < pc; i++)
                {
                    children[i] = MakeRandomTree(paramExps, maxTreeDepth - 1, fpr, ppr);
                }
                return(funcExp(children));
            }

            else if (RndGenerator.RndDouble() < ppr)
            {
                return(paramExps[RndGenerator.RndInt32(0, paramExps.Length)]);
            }
            else
            {
                return(Expression.Constant(RndGenerator.RndInt32(0, 10)));
            }
        }
예제 #10
0
        public override void GenerateNewBag()
        {
            int[] trainDocs    = new int[CompleteTrainingSet.NumDocs];
            int[] outOfBagDocs = new int[CompleteTrainingSet.NumDocs];
            int   trainSize    = 0;
            int   outOfBagSize = 0;

            int[]  tmpTrainQueryIndices = new int[CompleteTrainingSet.NumQueries];
            bool[] selectedTrainQueries = new bool[CompleteTrainingSet.NumQueries];

            int qIdx = 0;

            for (int i = 0; i < CompleteTrainingSet.NumQueries; i++)
            {
                int begin        = CompleteTrainingSet.Boundaries[i];
                int numDocuments = CompleteTrainingSet.Boundaries[i + 1] - begin;

                if (RndGenerator.NextDouble() < TrainFraction)
                {
                    for (int d = 0; d < numDocuments; d++)
                    {
                        trainDocs[trainSize] = begin + d;
                        trainSize++;
                    }
                    tmpTrainQueryIndices[qIdx] = i;
                    qIdx++;
                    selectedTrainQueries[i] = true;
                }
            }

            int outOfBagQueriesCount = CompleteTrainingSet.NumQueries - qIdx;

            var currentTrainQueryIndices = new int[CompleteTrainingSet.NumQueries - outOfBagQueriesCount];

            Array.Copy(tmpTrainQueryIndices, currentTrainQueryIndices, currentTrainQueryIndices.Length);

            var currentOutOfBagQueryIndices = new int[outOfBagQueriesCount];
            int outOfBagQIdx = 0;

            for (int q = 0; q < CompleteTrainingSet.NumQueries; q++)
            {
                if (!selectedTrainQueries[q])
                {
                    int begin        = CompleteTrainingSet.Boundaries[q];
                    int numDocuments = CompleteTrainingSet.Boundaries[q + 1] - begin;

                    for (int d = 0; d < numDocuments; d++)
                    {
                        outOfBagDocs[outOfBagSize] = begin + d;
                        outOfBagSize++;
                    }
                    currentOutOfBagQueryIndices[outOfBagQIdx] = q;
                    outOfBagQIdx++;
                }
            }

            CurrentTrainPartition    = new DocumentPartitioning(trainDocs, trainSize, MaxLeaves);
            CurrentOutOfBagPartition = new DocumentPartitioning(outOfBagDocs, outOfBagSize, MaxLeaves);
            CurrentTrainPartition.Initialize();
            CurrentOutOfBagPartition.Initialize();
        }
예제 #11
0
    public Vector2 getRoomAreaSideRandomPoint(RoomArea.Side side, RndGenerator rnd)
    {
        float x = 0, y = 0;
        switch (side)
        {
            case Side.TOP:
                y = RoomRect.yMax;
                x = rnd.Next((int)RoomRect.xMin,(int)RoomRect.xMax);
                break;
            case Side.BOTTOM:
                y = RoomRect.yMin;
                x = rnd.Next((int)RoomRect.xMin, (int)RoomRect.xMax);
                break;
            case Side.LEFT:
                x = RoomRect.xMin;
                y = rnd.Next((int)RoomRect.yMin, (int)RoomRect.yMax);
                break;
            case Side.RIGHT:
                x = RoomRect.xMax;
                y = rnd.Next((int)RoomRect.yMin, (int)RoomRect.yMax);
                break;
        }

        return new Vector2(x, y);
    }
예제 #12
0
    List<Pathway> createPathWays(RoomArea start, RoomArea end, RoomArea.Side startside, RoomArea.Side endside, RndGenerator rnd)
    {
        List<Pathway> ret = new List<Pathway>();
        Vector2 sv = start.getRoomAreaSideRandomPoint(startside, rnd);
        Vector2 ev = end.getRoomAreaSideRandomPoint(endside, rnd);
        float sx = 0, sy = 0, ex = 0, ey = 0;
        float xmin = start.RoomRect.xMin, xmax = start.RoomRect.xMax, ymin = start.RoomRect.yMin, ymax = start.RoomRect.yMax;
        int max = 0, min = 0;
        xmin++; xmax--;
        ymin++; ymax--;
        int sideline = (int)start.getSideLine(startside);
        switch (startside)
        {
            case RoomArea.Side.TOP:
                sx = rnd.Next((int)xmin, (int)xmax);
                sy = sideline-1;
                ex = sx;
                max = (int)Mathf.Max(end.Center.y, end.RoomRect.yMax, sideline + 6);
                min = sideline + 1;
                if (min >= max) max += min - max;
                ey = rnd.Next(min, max);
                break;
            case RoomArea.Side.BOTTOM:
                sx = rnd.Next((int)xmin, (int)xmax);
                sy = sideline+1;
                ex = sx;
                min = (int)Mathf.Min(end.Center.y, end.RoomRect.yMin, sideline - 6);
                min = Mathf.Max(0, min);
                max = sideline - 1;
                if (min >= max) max += min - max;
                ey = rnd.Next(min, max);
                break;
            case RoomArea.Side.LEFT:
                sy = rnd.Next((int)ymin, (int)ymax);
                ey = sy;
                sx = sideline+1;
                min = (int)Mathf.Min(end.Center.x, end.RoomRect.xMin, sideline - 6);
                min = Mathf.Max(0, min);
                max = sideline - 1;
                if (min >= max) max += min - max;
                ex = rnd.Next(min, max);
                break;
            case RoomArea.Side.RIGHT:
                sy = rnd.Next((int)ymin, (int)ymax);
                ey = sy;
                sx = sideline-1;
                max = (int)Mathf.Max(end.Center.x, end.RoomRect.xMin, sideline - 6);
                min = sideline + 1;
                if (min >= max) max += min - max;
                ex = rnd.Next(min, max);
                break;
        }
        sx = Mathf.Clamp(sx, 0, Size.x);
        ex = Mathf.Clamp(ex, 0, Size.x);
        sy = Mathf.Clamp(sy, 0, Size.y);
        ey = Mathf.Clamp(ey, 0, Size.y);
        Pathway pw = new Pathway(new Vector2(sx, sy), new Vector2(ex, ey));
        ret.Add(pw);
        int retry = 0;
        while (!end.RoomRect.Contains(pw.EndPosition)) {
            Vector2 sp = pw.EndPosition;
            Vector2 ep = Vector2.zero;
            if (pw.CurrentAxis == Pathway.Axis.HORIZONTAL)
            {
                ep = new Vector2(sp.x, end.CenterToInt.y);
            }
            else
            {
                ep = new Vector2(end.CenterToInt.x, sp.y);
            }
            pw = new Pathway(sp, ep);
            if (pw.CurrentAxis == Pathway.Axis.UNKWON) {
                if (sp.x == end.CenterToInt.x)
                {
                    ep.y = end.CenterToInt.y;
                }
                else if(sp.y == end.CenterToInt.y){
                    ep.x = end.CenterToInt.x;
                }
                pw = new Pathway(sp, ep);
            }

            //Debug.LogFormat("{0} {1} {2}", pw.ToString(), end.RoomRect.ToString(), end.RoomRect.Contains(pw.EndPosition));
            retry++;
            if (retry > 100) {
                Debug.LogFormat("Break Loop: {0} {1}", pw.ToString(),end.CenterToInt);
                break;
            }
            ret.Add(pw);
        }
        return ret;
    }
예제 #13
0
 public void generatePathways2(MapGraph graph,RndGenerator rnd)
 {
     Pathways = new List<Pathway>();
     foreach (Edge e in graph.EdgeList)
     {
         RoomArea r1 = Rooms.Find(room => room.Area.CenterToInt.Equals(e.Start.Position)).Area;
         RoomArea r2 = Rooms.Find(room => room.Area.CenterToInt.Equals(e.End.Position)).Area;
         //Rect a1 = Array.Find<Rect>(this.SplitFloorRect, rect => rect.Contains(r1.Center));
         //Rect a2 = Array.Find<Rect>(this.SplitFloorRect, rect => rect.Contains(r2.Center));
         float deg = MathUtil.SignedAngle(r1.Center, r2.Center);
         MathUtil.Quadrant q = MathUtil.getQuadrant(deg);
         var sides = RoomArea.getRoomAreaSide(q, deg);
         var sside = sides[rnd.Next(sides.Length)];
         sides = RoomArea.getRoomAreaSide(MathUtil.getReveseQuadrant(q), -deg);
         var eside = sides[rnd.Next(sides.Length)];
         var pws = createPathWays(r1, r2, sside, eside,rnd);
         Pathways.AddRange(pws);
     }
 }
예제 #14
0
        public Func <int, int, int> Evolve(ParameterExpression[] pc, int popsize,
                                           Func <List <ValueTuple <Func <int, int, int>, Expression> >, List <ValueTuple <long, Func <int, int, int>, Expression> > > rankfunction, int maxgen = 500, double mutationrate = 0.1,
                                           double breedingreate = 0.4, double pexp = 0.7, double pnew = 0.05)
        {
            //返回一个随机数,通常是一个较小的数
            //pexp的取值越小,我们得到的随机数就越小
            Func <int> selectIndex = () => (int)(Math.Log(RndGenerator.RndDouble()) / Math.Log(pexp));

            // 创建一个随机的初始种群
            var population = new List <ValueTuple <Func <int, int, int>, Expression> >(popsize);

            for (int i = 0; i < popsize; i++)
            {
                var exp  = MakeRandomTree(pc);
                var func = exp.Compile <Func <int, int, int> >(pc);
                population.Add((func, exp));
            }
            List <ValueTuple <long, Func <int, int, int>, Expression> > scores = null;

            for (int i = 0; i < maxgen; i++)
            {
                scores = rankfunction(population);
                _outputWriter?.Invoke(scores[0].Item1);
                if (scores[0].Item1 == 0)
                {
                    break;
                }

                // 取两个最优的程序
                var newpop = new List <ValueTuple <Func <int, int, int>, Expression> >()
                {
                    (scores[0].Item2, scores[0].Item3),
                    (scores[1].Item2, scores[1].Item3)
                };

                //构造下一代
                while (newpop.Count < popsize)
                {
                    if (RndGenerator.RndDouble() > pnew)
                    {
                        var exp = Mutate(
                            CrossOver(scores[selectIndex()].Item3,
                                      scores[selectIndex()].Item3, breedingreate), pc, mutationrate);
                        var func = exp.Compile <Func <int, int, int> >(pc);
                        newpop.Add((func, exp));
                    }
                    else
                    {
                        //加入一个随机节点,增加种群的多样性
                        var exp  = MakeRandomTree(pc);
                        var func = exp.Compile <Func <int, int, int> >(pc);
                        newpop.Add((func, exp));
                    }
                }

                population = newpop;
            }
            var printer = new GpPrinter();

            _outputWriter?.Invoke(printer.Display(scores[0].Item3));
            return(scores[0].Item2);
        }
예제 #15
0
 public Point()
 {
     h = (Int16)RndGenerator.NextSecure(0, 512);
     v = (Int16)RndGenerator.NextSecure(0, 384);
 }
예제 #16
0
        public void Receive(ThePalaceEntities dbContext, object message)
        {
            var sessionState = ((Message)message).sessionState;
            var protocol     = ((Message)message).protocol;

            if (!sessionState.Authorized)
            {
                if ((sessionState.userFlags & (int)UserFlags.U_Pin) != 0)
                {
                    return;
                }
            }

            var maxRoomOccupancy = ConfigManager.GetValue <int>("MaxRoomOccupancy", 45);
            var inboundPacket    = (Protocols.MSG_ROOMGOTO)protocol;
            var destRoom         = dbContext.GetRoom(inboundPacket.dest);

            if (destRoom.NotFound)
            {
                new MSG_NAVERROR
                {
                    reason = NavErrors.SE_RoomUnknown,
                }.Send(dbContext, message);

                return;
            }
            else if (!sessionState.Authorized)
            {
                var destRoomUserCount = SessionManager.GetRoomUserCount(inboundPacket.dest);

                if ((destRoom.Flags & (int)RoomFlags.RF_WizardsOnly) != 0 || (destRoom.Flags & (int)RoomFlags.RF_Closed) != 0)
                {
                    new MSG_NAVERROR
                    {
                        reason = NavErrors.SE_RoomClosed,
                    }.Send(dbContext, message);

                    return;
                }
                else if ((destRoom.MaxOccupancy > 0 && destRoomUserCount >= destRoom.MaxOccupancy) || (destRoom.MaxOccupancy == 0 && destRoomUserCount >= maxRoomOccupancy))
                {
                    new MSG_NAVERROR
                    {
                        reason = NavErrors.SE_RoomFull,
                    }.Send(dbContext, message);

                    return;
                }
            }

            var nbrUsers    = SessionManager.GetRoomUserCount(sessionState.RoomID, sessionState.UserID);
            var currentRoom = dbContext.GetRoom(sessionState.RoomID);

            if (nbrUsers > 0)
            {
                new MSG_USEREXIT().SendToRoomID(dbContext, message);
            }
            else if (!currentRoom.NotFound)
            {
                currentRoom.Flags &= ~(int)RoomFlags.RF_Closed;
            }

            sessionState.RoomID = inboundPacket.dest;

            if (!sessionState.Authorized)
            {
                var spots = destRoom.Hotspots
                            .Where(s => (s.flags & (int)(HotspotFlags.HS_LandingPad)) != 0)
                            .ToList();
                if (spots.Any())
                {
                    var offset   = (Int32)RndGenerator.NextSecure((UInt32)spots.Count);
                    var vortexes = new List <Point>();
                    var spot     = spots
                                   .Skip(offset)
                                   .Take(1)
                                   .FirstOrDefault();
                    var minH = (Int16)(spot.loc.h + spot.Vortexes[0].h);
                    var maxH = (Int16)(spot.loc.h + spot.Vortexes[0].h);
                    var minV = (Int16)(spot.loc.v + spot.Vortexes[0].v);
                    var maxV = (Int16)(spot.loc.v + spot.Vortexes[0].v);

                    foreach (var vortex in spot.Vortexes)
                    {
                        var p = new Point
                        {
                            h = (Int16)(spot.loc.h + vortex.h),
                            v = (Int16)(spot.loc.v + vortex.v),
                        };

                        vortexes.Add(p);

                        if (p.h < minH)
                        {
                            minH = p.h;
                        }
                        if (p.h > maxH)
                        {
                            maxH = p.h;
                        }
                        if (p.v < minV)
                        {
                            minV = p.v;
                        }
                        if (p.v > maxV)
                        {
                            maxV = p.v;
                        }
                    }

                    do
                    {
                        sessionState.details.roomPos.h = (Int16)RndGenerator.NextSecure((UInt32)minH, (UInt32)maxH);
                        sessionState.details.roomPos.v = (Int16)RndGenerator.NextSecure((UInt32)minV, (UInt32)maxV);

                        if (vortexes.PointInPolygon(sessionState.details.roomPos))
                        {
                            break;
                        }
                    } while (true);
                }
                else
                {
                    sessionState.details.roomPos.h = (Int16)RndGenerator.NextSecure((UInt32)destRoom.Width);
                    sessionState.details.roomPos.v = (Int16)RndGenerator.NextSecure((UInt32)destRoom.Height);
                }
            }
            else
            {
                sessionState.details.roomPos.h = (Int16)RndGenerator.NextSecure((UInt32)destRoom.Width);
                sessionState.details.roomPos.v = (Int16)RndGenerator.NextSecure((UInt32)destRoom.Height);
            }

            var sendBusinesses = new List <ISendBusiness>
            {
                new MSG_ROOMDESC(),
                new MSG_USERLIST(),
                new MSG_ROOMDESCEND(),
            };

            foreach (var sendBusiness in sendBusinesses)
            {
                sendBusiness.Send(dbContext, message);
            }

            new MSG_USERNEW().SendToRoomID(dbContext, message);
        }