ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX https://doi.org/10.1109/ELECTRONICS.2018.8443636 A Study on Fog Computing Environment Mobility and Migration Pedro R. Juan1,2, Salvador Alcaraz1, Katja Gilly1, Carlos Juiz2 1Department of Physics and Computer Architecture, Miguel Hernández University, Avda. Universidad, s/n 03202 Elche (Alicante), Spain 2Department of Computer Science, Balearic Islands University, Ctra. Valldemossa, km 7.5 07122 Palma de Mallorca, Spain Abstract-Cloud Computing paradigm has reached a high degree of popularity among all kinds of computer users, but it may not be suitable for mobile devices as they need computing power to be as close as possible to data sources in order to reduce delays. This paper focuses on achieving mathematical models for users moving around and proposes an overlay mobility model for Fog Data Centres based on traditional wireless mobility models aimed at better allocating edge computing resources to client demands. Additionally, the focus is also on providing a model as to how the computing assets related to each user follow it through the fog infrastructure so as to minimise the distance to the user according to a Fat-Tree based Data Centre topology. Index Terms- fog computing; mobility models; migration; networking. I. INTRODUCTION Fog Computing paradigm brings Cloud Computing to the edge of the network [1], allowing the user a better interaction with the services provided. That delivers a lot of advantages, such as lower latencies, but it also carries some drawbacks, such as more complexity in network designs. Therefore, the main characteristic of Fog Computing is the presence of computing assets, such as processing power, storage and application services near the end users. There are many scenarios where Fog Computing may be implemented [2,3], ranging from smart grids, smart cities or wireless sensor and actuator networks, all of them included into the key player concept of Internet of Things (IoT), that permits the processing, storage and network facilities to be closer to data sources in order to reduce network delays and hence, to improve the service quality experienced by end users. In this paper, the focus is put on moving IoT devices, which may be in motion through an area, following diverse trajectories. Therefore, it is necessary to build models in order to simulate two kinds of movements. First, mobility models must be studied in order to cover physical mobility of users moving around in different ways. After that, migration must be then studied for their Manuscript received April XX, 20XX; accepted April XX, 20XX. This research was funded by a grant (No. XXX-00/0000) from the Research Council of Lithuania. This research was performed in cooperation with the Institution. associated virtual machines (VMs), which act as the computing assets and tries to get as near as possible to them in order to optimise the computing resources available. There is much literature about mobility models [4] and migrations [5], but Fog Computing is a relatively new topic and there is not much literature around [6]. Nevertheless, some ideas have been taken from there for the models, such as the use of Data Centre topologies [7]. Therefore, as per mobility models, the most common types of physical movements are studied, taking into account a mathematical approach, and also algorithmic when feasible. With regards to migration, after its related users have already moved, an algorithmic approach is taken into consideration in order to get a VM as close as possible to its user, so the target is to minimise the distance between them. This way, both mobility models and migration meet what happens in the Fog Computing paradigm, hence fulfilling the requirements defined and thus validating the models designed. The organisation of this paper will be as follows: first, Section 2 introduces some tessellation concepts, later, Section 3 focuses on the election of a tessellation model, then, Section 4 presents some mobility models for users, after that, Section 5 will show basic concepts regarding FatTree topology, afterwards, Section 6 will render the migration model for VM movements associated to users, and finally, Section 7 will draw the final conclusions. II. TESSELLATION CONCEPTS In order to adapt to Fog Computing a mobility model, we are going to assume that signal coverage has the greatest influence [8]. So, first, we need to divide the total coverage area into smaller pieces so as to get nearer to the users. This is, filling the coverage plane with a set of predefined tiles without leaving any gaps or overlaps in between. The simplest way to do so is by means of regular tessellation, where tiles are regular polygons of the same kind, such as triangles, squares and hexagons. As an example of regular tessellation in networking, it is worth to be mentioned that hexagonal tessellation is widely used in basic resource allocation in mobile telephony and other sorts of wireless networks. ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX Special attention might be given to Voronoi tessellations, where each tile is a convex polygon containing just one generating point. This fact conveys some considerations, such as that the rest of points within a single tile are closest to that predefined point, the edges of such a polygon are formed by points being exactly halfway between two generating points and a vertex is the intersection of three or more such edges. Voronoi tessellations may have many practical applications nowadays, such as computational geometry, robot navigation, machine learning, clustering analysis, resource management or digital image compression, just to name a few. But regarding networking, one of the killer applications for Voronoi tessellations is advanced resource allocation in wireless networks. There is a significant type of Voronoi diagrams called Centroidal Voronoi Tessellation (CVT), where each generating point must be the centre of mass for its corresponding region. Therefore, CVT might be seen as a Voronoi tessellation with an extra constraint regarding the optimal distribution of generators given a probability density function (PDF). The use of Voronoi tessellations gives a more realistic approach as opposed to regular tessellations. However, the study of Voronoi tessellations, either regular and CVT, might become cumbersome as it involves the implementation of complex algorithms. In addition to that, Gersho's conjecture states that for any PDF, as the number of points increases, the distribution of CVT points become locally uniform. Focusing on the twodimensional case, the basic cells for the optimal CVT while forming a tessellation are locally congruent regular hexagons. Therefore, in order to keep things simple, in this paper we are going to be using hexagonal tessellations, where regular hexagons are used to create the geometric pattern dividing the coverage area for the Fog Computing domain. III. TESSELLATION MODEL Once that we have chosen the type of tessellation to be used, it is then necessary to decide how many hexagons are necessary to cover the first row of the coverage area, and that late value will be hold in variable f. That will depend on the length of that area and the size of each hexagon. Let us assume different values for f: A. f=2 In this case, the first row contains just 2 hexagons, so Figure 1 exhibits the tiling pattern. Fig. 1. Tiling pattern for 2 hexagons in the first row (f=2) In order to infer the moving pattern, we take a central hexagon and try to move in all six possible directions. With that in mind, Figure 2 shows the operation to be applied when moving to a neighbouring cell. Fig. 2. Moving pattern for 2 hexagons in the first row (f=2) B. f=3 In this case, the first row contains 3 hexagons, so Figure 3 depicts the tiling pattern. Fig. 3. Tiling pattern for 3 hexagons in the first row (f=3) As per the inference of the moving pattern, we proceed as in the previous case, and Figure 4 shows the operation to be applied when moving to a neighbouring cell. Fig. 4. Moving pattern for 3 hexagons in the first row (f=3) C. f=1,2,...,x In a general case, where the first row contains x hexagons, Figure 5 displays the tiling pattern by applying mathematical induction. Fig. 5. Tiling pattern for x hexagons in the first row (f=x) Regarding the inference of the moving pattern, we must proceed as in the previous cases, so Figure 6 explains the operation to be applied when moving to a neighbouring cell. Fig. 6. Moving pattern for x hexagons in the first row (f=x) Therefore, the general case (f=x) would fit any hexagonal tessellation pattern, no matter how many hexagons fit into the first row. As an example, Table I shows a translation board. TABLE I. GENERALISING TESSELLATION F=3 TO F=X. Direction Vector f=3 f=x North-West -3 -x North -5 -2x+1 North-East -2 -x+1 South-East +3 +x South +5 +2x-1 South-West +2 +x-1 ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX IV. MOBILITY MODELS After having defined the tessellation model, it is necessary to simulate the mobility model, this is, the physical movement of a user in a Fog Computing environment. Let us assume that a single user may be moving around no matter whether the destination cell is occupied by other users or otherwise, this is, there is no restriction in how a user may change to the next cell into the Fog Computing domain. In principle, all kind of movements might be possible, but we have selected just four paths as being the most significant ones, because they might encompass the most intuitive trajectories. Those four paths are the following: pure random (Laplace), same direction (Vector), same sense (Sweep) and skewed random (Pareto). Names may be selfexplanatory although they are all going to be explained in due course. For the study of each case, it is necessary to define some position variables representing the previous cell where the device came from, the present cell where the device is standing now and all possible six neighbouring cells where the device might move to. These variables are defined in Table II. Supposing f=3 and f=x, the generic moving pattern is exhibited in Figure 7, provided that the present cell is n (standing for now). The probability for each destination cell is 1/6, as stated above for equiprobable events. Fig. 7. Movement pattern for f=3 (left) and f=x (right) in Laplace path case B. Case 2: Same Direction path (Vector) This case does take into account which the previous cell of the trajectory is and the directed path just aims at the following cell pointed by the vector going from the previous cell (p) to the present cell (n). This destination cell is going to be the successor towards the direction vector (c). This path is similar as the one related in literature as Random Direction. Therefore, the successor cell will be given by adding the present cell to the subtraction of the present cell number from the previous cell number. TABLE II. VARIABLES FOR MOVING USERS. c = n + (n p) (1) This is, the possible successor will always be the forward centre cell following the incoming trajectory, obviously with a probability of 100%, so prob(c)=1. For instance, taking n=10, if p=5, then c=15; or otherwise, if p=12, then c=8. Figure 8 exhibits the case where c=n+5, but it might be whichever else. Once the conditions for each scenario are presented, the probability for each possible successor is going to be calculated, so that those probability values will show the likelihood for each possible trajectory a device might undertake according to the constraints set for each path. A. Case 1: Pure Random path (Laplace) This case does not take into account which the previous cell of the trajectory is. It might be said that cells do not have memory, similarly as Markov chains do. This path resembles the one related in literature as Random Walk. This is ideal for devices appearing for the first time in the Fog Computing domain and also for memoryless environments, as in both cases there are no records regarding which the previous cell on the trajectory is. Therefore, the direction movement does not apply in this case. This is a typical case of equiprobability, where all six possible neighbouring cells are equally likely to be the possible successor. Therefore, and according to Laplace's principle of indifference, that probability for each cell is 1/6, so prob(any)=1/6. In conclusion, the user might go anywhere. Fig. 8. Movement pattern for f=3 (left) and f=x (right) in Vector path case C. Case 3: Same Sense path (Sweep) This case also takes into account which the previous cell (p) is but not only focusing on the forward centre cell (c), but also on the forward left (l) and the forward right (r) cells. This path is a slight variation of the previous one. It may be seen as if the trajectory will follow the movement line, starting from the present cell (n) and behaving like a wave sweeping towards the direction vector, thus including not only the centre cell but also the one on its left and the one on its right, as the user may not necessarily take an axial way through the cell. As an example, Figure 9 shows the case where c=n+5, but c might point to any other neighbour. Fig. 9. Movement pattern for f=3 (left) and f=x (right) in Sweep path case Variable Meaning Name p Previous cell Predecessor n Present cell Now c Future cell – forward centre Successor just towards the direction vector l Future cell – forward left Successor on the left towards the direction vector r Future cell – forward right Successor on the right towards the direction vector bc Future cell – backward centre Successor just opposite to the direction vector bl Future cell – backward left Successor left opposite to the direction vector br Future cell – backward right Successor right opposite to the direction vector ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX n p It is to be noted that the cells considered are regular hexagons, so each one is composed by 6 equilateral triangles. Such triangles contain three angles measuring just 60 degrees. Therefore, if the side of the regular hexagon is denoted by s, Figure 10 exhibits the horizontal orthogonal then be calculated in two different ways, namely, by using algebraic expressions and by using an algorithm. On the one hand, the former is obtained by using the following expressions which eventually lead to the calculation of c, l and r, given n and p, for f=3. projections for all three possible successors, being the cell pointed by the direction vector and both lateral neighbours. abs _ l ae abs _ c = n p ae abs _ c öö (2) (3) = ç abs _ c + roundç ÷÷ mod 6 2 è è øø ae ae abs _ l öö (4) abs _ r = ç abs _ l + roundç ÷÷ mod 6 2 Fig. 10. Orthogonal projections in a regular hexagon onto the x-axis Considering that sin 30o=1/2, Figure 11 yields the ratio among left, centre and right cells, and according to that, the è è øø sig _ c = n p +1 sig _ l = (-1) ae abs _ c -abs _ r ö (5) (6) probabilities for each event may well be calculated. Finally, the ratio among the three cells involved is also shown, being sig _ r = (-1) round ç è ÷+1 3 ø (7) 1:2:1 in the format left:centre:right. That means the probability of getting to the forward centre cell is twice as getting to the left or to the right. So, prob(c)=2/4, prob(l)=1/4 and prob(r)=1/4. c= n + sig _ c × abs _ c l = n + sig _ l × sig _ c × abs _ l r = n + sig _ r × sig _ c × abs _ r (8) (9) (10) Fig. 11. Probabilities and ratio for Sweep path case In the sweep path case, we may use (1) in order to get c, given the previous cell (p) and the present cell (n), being the possible successor towards the direction vector. But now it is necessary to get expressions for both l and r. In order to do that, Table III presents all possible combinations for a given n, as shown in Figure 8, where l and r are assigned watching c towards n, this is, from outside c inwards the centre of n. TABLE III. SUCCESSORS FOR THE PRESENT CELL N WHEN F=3. Predecessor Centre Left Right n–3 c = n+(n-(n-3)) = n+3 n+5 n–2 n–5 c = n+(n-(n-5)) = n+5 n+2 n+3 n–2 c = n+(n-(n-2)) = n +2 n–3 n+5 n+3 c = n+(n-(n+3)) = n–3 n–5 n+2 n+5 c = n+(n-(n+5)) = n–5 n–2 n–3 n+2 c = n+(n-(n+2)) = n–2 n+3 n–5 If we just focus on the terms being added or subtracted to n, it is worth noting that, taking the absolute values and following the sequence centre-left-right, we always get the next element of the set {2, 3, 5}, such that c=x1, l=x2=(x1+1)mod3, r=x3=(x1+2)mod3. That works in a similar manner as Z3 in group theory, but this is not really the case as the aforesaid set does not have any neutral element. Therefore, this set is not isomorphic to Z3, hence operations get harder to deal with. Additionally, the proper signs have to be considered, so it is worth realising that the sign of c is always maintained by l and r, except in the cases where the difference in absolute value between c and any of the other variables is just one. Taking those premises into consideration, for a given n and p, it is possible to calculate c, and in turn, l and r might On the other hand, the latter may be accomplished by Algorithm1, which gives the proper values for c, l and r, given n and p, for f=x. If f=3, results obtained are the same as above. Algorithm 1. Calculation of c, l and r, given n and p, for any f D. Case 4: Skewed Random path (Pareto) This last case is a mix of the first one and the previous one, in a way that herein we consider that the movement line might not always have to follow the trajectory but directions might change anytime with different odds. This path is corresponding to the one related in literature as probabilistic version of the Random Walk. In order to reflect this, we are going to apply the Pareto principle, also known as the 80/20 rule. It is an empirical principle having many definitions, such as 80% of the revenue is coming from 20% of the consumers within a company. But in this context we are going to suppose that 80% of the time the user will follow the sweep trajectory and 20% will not. Algorithm1(n,p,f){ int vector = |n p|; int c = 0, l = 0, r = 0; int abs_c = vector, abs_l = 0, abs_r = 0; int sig_c = 1, sig_l = 1, sig_r = 1; int values = [f-1, f, 2*f-1]; //[2,3,5] if f=3 int i = 0; for (i=0; i<3; i++){ if (values[i] == vector){ abs_l = values[(i + 1) % 3]; abs_r = values[(i + 2) % 3]; } } if |abs_c – abs_l| == 1 sig_l = -1; if |abs_c – abs_r| == 1 sig_r == -1; if ((n – p) < 0) sig_c = -1; c = n + sig_c * abs_c; l = n + sig_l * sig_c * abs_l; r = n + sig_r * sig_c * abs_r; } ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX As 80/20 may be simplified as 4/1, we may consider a ratio 4:1 being the relationship between forward and backward. Additionally, we are going to apply the ratio 1:2:1 obtained in the previous case for the left:centre:right cells in both the forward and the backward trajectory, thus, making possible to move to all 6 neighbouring cells. Obviously, all 6 events are not equiprobable, thus, each cell will have its own. Regarding forward trajectory, it accounts for 80% with a ratio 1:2:1, so probabilities will be prob(c)=2/5, prob(l)=1/5 and prob(r)=1/5. Considering backward trajectory, it makes for 20% with a ratio 1:2:1, thus probabilities will be prob(bc)=2/20, prob(bl)=1/20 and prob(br)=1/20. These facts are exposed in Figure 12. Fig. 12. Probabilities and ratio for Pareto path case Putting it all together, Figure 13 shows the case where c=n+5, where l=n+2, r=n+3, and its backward counterparts are bc=n-5, bl=n-2 and br=n-3, although c might point anywhere. Fig. 13. Movement pattern for f=3 (left) and f=x (right) in Pareto path case Looking at the previous figure, the relationships between forward trajectory cells and its backward counterparts are easily appreciated, and it is depicted in Table IV. TABLE IV. FORWARD –VS– BACKWARD CELLS WHEN C=N+5. Forward Value Backward Name Forward centre (c) n+5 Backward centre (bc) n-5 Forward left (l) n+2 Backward left (bl) n-2 Forward right (r) n+3 Backward right (br) n-3 Therefore, calculations of the backward cells are as simple as changing the sign of the term modifying n in its forward counterparts, which may be made by using both methods described in case 3, namely, algorithmically or algebraically. Finally, Table V shows a summary for all 4 cases studied. TABLE V. PROBABILITIES FOR EACH PATH. Path c l r bc bl br Laplace path 1/6 1/6 1/6 1/6 1/6 1/6 Vector path 1 0 0 0 0 0 Sweep path 2/4 1/4 1/4 0 0 0 Pareto path 2/5 1/5 1/5 2/20 1/20 1/20 V. FAT-TREE TOPOLOGY Fat-Tree based architecture is a Data Centre (DC) topology widely used in recent years [9] in order to deliver scalable bandwidth for large cluster environments. That topology is a special type of Clos network, meaning a multistage switching network. In this paper we are going to be focusing on one of the most common designs, namely, the k-ary tree with k=4 and 1:1 oversubscription ratio, resulting in a three-layer topology, such as edge, aggregation and core. This topology provides full path redundancy between whichever pair of switches, hence between any pair of servers. In order to achieve that, each couple of servers are connected to the same edge switch, then, each two edge switches belonging to the same pod are connected to both its own aggregation switches, which in turn are connected to two different core switches, thus making that each pod has a direct connection to each core switch. This is all shown in Figure 14. Fig. 14. Fat-Tree Topology for k=4 and oversubscription 1:1 VI. MODELS FOR VIRTUAL MACHINE MIGRATIONS Right after the user has eventually moved onto a different cell, whichever the simulation path was taken, we are going to try to move the VM associated to that user as close to the user itself as possible, as defined by the Fog Computing paradigm. This action is known as migration. The ideal situation would be to try to move both user and VM to the same cell, so as to minimise the distance between them. But often times this is not possible, so the VM should be moved to the nearest possible cell to its related user. This non-ideal situation happens because each cell might have a limited number of VMs, hence if a user is in a cell where no more resources are available, then the associated VM may have to be allocated in a nearby cell, the closer the better. In order to simulate that, some new variables may be defined as shown in Table VI. It is to be said that a two dimensional array is used to map cells to VMs, where the first index takes the cell number and the second index carries the VMs standing in that cell, having an upper limit capacity. The values held in this matrix are the user_IDs owning each VM. TABLE VI. VARIABLES FOR MIGRATION. Variable Meaning Name u New cell where the user has just moved New User cell m Present cell where the VM is standing Present VM cell v New cell where the VM will move to be the closest to the user New VM cell to be determined VM[ ][ ] Each VM within a cell belongs to a user, whose user_ID is held Matrix mapping cells to VMs top Upper bound of VMs per cell Capacity of each cell hops Hops away from user to VM Number of hops The upper bound of VMs per cell is a complex issue that depends on many factors, but herein it is considered as ELEKTRONIKA IR ELEKTROTECHNIKA, ISSN 1392-1215, VOL. XX, NO. X, 20XX simple as a cell reaching its predefined capacity. But anyway, if a VM is finally allocated in a different cell from the one holding its related user, then a performance penalty may have to be applied, depending on how far apart both cells are. This is because every time a user needs to use its computing power, if they both are located in different cells, the further away both cells are, the longer processing time might take. Regarding the penalties, they may be based as an example on the Fat-Tree topology model explained in Section V, such that each hop means getting higher on the hierarchy, thus taking longer for the user to reach its own VM. Table VII explains penalties depending on the User-VM hop distance. TABLE VII. PERFORMANCE PENALTIES IMPOSED. Value Relative distance Fat-Tree model analogy 0 User is located 0 hop away from its related VM (in a the same cell) As if a User and its VM were located in the same server 1 User is located 1 hop away from its related VM (in a neighbouring cell) As if a User and its VM were located in servers connected to the same edge switch 2 User is located 2 hops away from its related VM As if a User and its VM were connected through a same aggregation switch 3 User is located 3 or more hops away from its related VM As if a User and its VM were connected through a core switch The User-VM hop distance may also be seen in Figure 15, showing how far away a VM is located from its related user. The inner circle stands for the case where both share the same cell (n), and the greater the circle between the user (n) and its associated VM, the higher the penalty is carried. Fig. 15. Hops between a user and its VM for f=3, penalty going from 0 to 3 According to the figure above for f=3, it may be appreciated that cells 1-hop away are ±2, ±3 and ±5. Besides, cells 2-hops away may be classified in two groups, both being values related to the previous ones. This is, either doubling one hop away values, such as ±4, ±6 and ±10, or making a composition of those, such as ±1 (3-2), ±7 (5+2) and ±8 (5+3). Putting it all together, Algorithm2 has been designed for f=x, where the reference is a user standing on a cell (u) and the target is to move its related VM from its previous cell (m) to another cell being as close as possible to the user (v). That algorithm also calculates the performance penalty (hops), in a way that if it gets greater than 2, VM does not move, but if it gets less than 3, VM does move trying to diminish the hop distance between the user and itself. It is to be said that the algorithm just takes into account the minimum distance from user to VM as the only target, although in real deployments other criteria might apply. Algorithm 2. Calculation of new cell for VM and hop count to its user VII. FINAL CONCLUSIONS In this paper we have been working on achieving both a mobility model and migration for a Fog Computing environment. The focus has been twofold: first, simulating users moving around in some of the most common ways, and then, simulating the migration of VMs associated to those users trying to be as close to them as possible, getting in both cases the fulfilment of the requirements. REFERENCES [1] R. Kamath, Fog Computing – a practical overview, in IJIRCCE'2017, Vol. 5, Issue 4, pp. 7819-7822. [2] I. Stojmenovic and S. Wen, The Fog Computing paradigm: scenarios and security issues, in CSIS'2014, Vol. 2, pp. 1-8. [3] T.H. Luan, L. Gao, Z. Li, Y. Xiang, L. Sun, Fog Computing: Focusing on Mobile Users at the Edge, in arXiv CoRR'2015. [4] T. Camp, J. Boleng and V. Davies, A survey of mobility models for ad hoc network research, in WCMC'2002, Vol. 2, Issue 5, pp. 483-502. [5] P. Kaur and A. Rani, Virtual machine migration in cloud computing. [6] Tadapaneni, N. R. (2018). Cloud Computing: Opportunities And Challenges. International Journal of Technical Research and Applications. [7] O. Osanaiye, S. Chen, Z. Yan, R. Lu, K.R. Choo and M. Dlodlo, From Cloud to Fog Computing: A Review and a Conceptual Live VM Migration Framework, in RACRAN'2017, Vol. 5, pp. 8284-8300. [8] K.C. Okafor, Leveraging Fog Computing for scalable IoT datacenter using Spine-Leaf network topology, in JECE'2017, pp. 1-11. [9] M.A. Khajehnejad, A. Heidarzadeh and S.N. Esfahani, A sufficient condition for infinite connectivity in two dimensional ad-hoc wireless sensor networks, in ISSPA'2007, pp. 1-4. [10] M. Al-Fares, A. Loukissas and A. Vahdat, A scalable, commodity data center network architecture, in SIGCOMM'2008, pp. 63-74. View publication stats int size(cell,*VM,top){ int i, s = 0; for (i = 0; i < top; i++) while (VM[cell][i] != NULL) s++; return s; } void move(m,cell,*VM,top,user_id){ int i; for (i = 0; i < top; i++) if (VM[m][i] == user_id) VM[m][i] == NULL; for (i = 0; i < top; i++) if (VM[cell][i] == NULL) VM[cell][i] = user_id; return; } Algorithm2(u,m,f,*VM,top,user_id){ int v = m, hops = 3, i = 0, j = 0; int pos[3] = {0, -1, +1}; int offset[3][6] = { {-f, -2*f+1, -f+1, f, 2*f-1, f-1}, {-2*f, -4*f+2, -2*f+2, 2*f, 4*f-2, 2*f-2}, {-1, -3*f+1, -3*f+2, +1, 3*f-1, 3*f-2} }; // int offset[0]=[-3,-5,-2,+3,+5,+2]; //if f=3 // int offset[1]=[-6,-10,-4,+6,+10,+4]; //if f=3 // int offset[2]=[-1,-8,-7,+1,+8,+7]; //if f=3 if (u == m) {hops = 0;} //just moved onto VM else{ for (i = 0; i < 6; i++) { //check offset[0] if (u == m + offset[0][i]) { for (j = 0; j < 3; j++) { if (size(m+offset[0][(i+pos[j])%6],*VM,top) < top) { move(m,m+offset[0][(i+pos[j])%6],*VM,top,user_id); v = m + offset[0][(i+pos[j])%6]; hops = |pos[j]|; } }else {hops = |pos|;} //VM does not move }if (u == m + offset[1][i]) { //check offset[1] if (size(m+offset[0][i],*VM,top) < top) { move(m,m+offset[0][i],*VM,top,user_id); v = m + offset[0][i]; hops = 1; }else {hops = 2;} //VM does not move }if (u == m + offset[2][i]) { //check offset[2] for (j = 0; j < 2; j++) { if (size(m+offset[0][(i+pos[j])%6],*VM,top) < top) { move(m,m+offset[0][(i+pos[j])%6],*VM,top,user_id); v = m + offset[0][(i+pos[j])%6]; hops = 1; } }else {hops = 2;} //VM does not move }}}}