Puzzle Solver - TreeNode Help - iphone

I'm trying to code a puzzle solver app.
I need to find out how many moves it takes, and how many solutions there are.
I would rather not give too many details on the puzzle.
but the player moves around a grid ( say 5 x 7 )
as they move, obstacles could be captured so the state of the board needs to be tracked.
( this could be done as a string or an array )
I understand I need to create a TreeNode, starting with a root ( the players start position )
and give each node children of the possible moves until all the possible moves are calculated.
The puzzle stats could then be collected.
Number of Possible solutions, minimum number of moves to solve, average number of moves to solve, etc.
I have the puzzle logic created that will return if moves are possible and such.
I'm having problems creating the TreeNode structure and making sure moves are not duplicated.
The puzzle app itself is on the iPhone, but I'm writing this solver/editor on the Mac.
Any help would be VERY much appreciated.

Perhaps you could do a variant of a tree recursion? Traverse the tree recursively, having each end node return a value of how hard it was to get there (if there are costs associated with different moves) and a description of how it got there. This of course requires the player to only move in one direction, otherwise the tree-structure doesn't describe the problem. A bit more info on what your exact problem looks like would be helpful.
It might be a heavy algorithm, but it gets the job done.

For detecting repeated states, you would put the states in a set as you went along, and then check every time you found new states to see if they already existed. Though if space is an issue, you will have to resort to only checking if the children are not the same as the parent, or some kind of limited version of this approach.
A node class is very simple. It just contains a pointer back to a parent (if it has one) and the variable it holds (such as a state). You will also probably want other variables depending on your application.
When you get to a node, you use a successor function to get all the child nodes from there (the states that can be reached in one move) and add them to a list. You pluck from the list to traverse the tree.

Related

How to determine when to start a counter to ensure it never catches the previous counter

I have a problem where I have several events that are occurring in a project, the events happen semi-concurrently, where they do not start at the same time but multiple can still be occurring at once.
Each event is a team of people working on a linear task, starting at the beginning and then working their way to the end. Their progress is based on a physical distance.
I essentially need to figure out each events start time in order for no teams to be at the same location, nor passing eachother, at any point.
I am trying to program this in MATLAB so that the output would be the start and end time for each event. The idea would be to optimize the total time taken for the project.
I am not sure where to begin with something like this so any advice would be greatly appreciated.
If I understand correct, you just want to optimize the "calendar" of events with limited resources (aka space/teams).
This kind of problems are those called NP and there is no "easy" way to search for the best solution.
You here have two options:
Greedy like algorithm: You will have your solution in a resonable time but it won't be the best one.
Brute force like algorithm: You will find the best solution but maybe not in the time you need it.
Usually if the amount of events is low you can go for 2nd option but if don't you may need to go for the first one.
No mather which one you choose first thing you will need to do is to compute if a solution is valid. What does this mean? It means to check for every event wheter if it collisions whith others in time, space and teams.
So lets imagine the problem of making the calendar on a University. There you have to think about:
Students
Teacher
Classroom
So for each event I have to check if another event have same students, teacher or classroom at the same time. First of all I will check the events that match in time with the actual event. Then I will compare the actual event with all the others.
Once you have this done you could just write a greedy algorithm that starts placing events on time just checking if it collides with some other.

Moving agents with other agents using Pickup/Dropoff from PML in Anylogic without duplicate code

Info: The question was updated with more explanation
I want to transport a agent (e.g. bananas) with a moving agent (e.g. truck) from place A to place B, where, for example, place A is where the bananas where plucked and place B is some storage for the bananas. So the bananas are simply being transported by the truck. Especially, the agent to be moved (the bananas) are not a resource (in the sense of Anylogic PLM) and have no upper amount limit.
There are various ways to solve this problem, but most of them either require some element in the model that I don't need or want (for example, a rack/pallet system in the case of the block 'Rack Store') or require the agents to be Anylogic resources.
As described in this answer, it kinda makes sense to use pickup and dropoff for this task. The problem is that the agent to be moved is not being transported, so that answer does not solve my question. To explain further, when the agent to be moved (the bananas) are being dropped off at the target location (place B), they simply re-appear at their original location (place A), even though the truck which picked them up via the pickup block has moved to place B.
I made a minimal example of this here.
As I described, the 'transportation' only works if I add the separate 'moveTo1' block for the dropped off agents.
Is there any simple and obvious way to handle this rather simple task of transportation in Anylogic without having multiple move blocks or other workarounds? I know there is 'ResourceAttach', but that requires the agent to be moved to be resources, and there is 'RackStore', which requires a rack/pallet system, which I don't need or want in my model.
What I want to know is what the 'standard' Anylogic way would be to do this.
Thanks a lot in advance!
Now I understand what your problem is...
When you use dropoff, the block that comes after it needs to define the new location of the agents, otherwise they stay in the same place.. You can use the moveTo block with a jump so the agents are teleported to the location you want them to be:
In almost all the blocks of the PML you can define the agent location in the properties, and this is a case where using that property is necessary.
You can set the position of the bananas to the position of the truck.
e.g. using agent.setXY(container.getX(), container.getY()) in the "On dropoff" field.
It seems to work for a simple test model.

How can I capture metrics from an emulated (SNES) game?

My goal is to emulate a game (e.g., Super Bombliss) using an Android emulator (e.g., Snes9x EX+) and to capture game metrics (e.g., score and level) as the game is played.
I assume I would need to modify the open-source emulator and/or to modify the ROM to do this, but I need some guidance on the best approach. Thanks!
To answer "How can I capture metrics from an emulated (SNES) game?" you need to first answer "Where can I find metrics in a SNES game?". The answer to the latter is "It depends on the game."
Think about GameShark codes, they write a value to a specific position of the memory.
It can write a value constantly to a memory position. For keeping you life bar always full on Mortal Kombat, for example.
It can write a value to a memory position just once. If you want to jump to last level of Sunset Riders.
But how do they know which memory position they should write to? That is the hard question. Usually it comes down to reverse engineering the ROM. You create a map of which memory position corresponds to each metric you are looking for.
Let's assume you want to find your current score and your current level on Super Mario World. A possible solution would be to constantly scan the RAM memory, looking for a known value and create a map of matches.
Knowing your score is 321, you can dump the RAM and look for where memory positions that contain 321. If only one matches, there is a very likely chance that it is the memory position that indicates your score. If you have more matches you should keep a list of all matches and keep playing until your score changes. If it changes to 567 now you check from your previous list if any of the memory positions now holds the value 567.
It is not always straight forward, for example, for the stage level 6-3. Should you look in the memory for 6 and for a 3 separately? What if the game stores the levels in just one variable represented as BCD 63. Then you would have to look for something completely different.
The only way to be sure 100% is by reverse engineering the ROM, in that way you know where the game goes fetch whatever is displayed on the screen.
Now to answer your original question "How can I capture metrics from an emulated (SNES) game?" you have to first figure out where those metrics are located in every game ROM and once you know you just need to build an emulator that listens to modifications in those memory positions so you can capture those metrics.
You can find many websites that are dedicated to reverse engineering ROMs. I suggest you take a look at the work done on http://www.romhacking.net/.

removing all geometry from SceneKit, or modifying it on the fly

I have a simple SceneKit view that displays antenna designs, like a TV antenna, or this less common example, a biquad.
These designs consist of a number of SKCylinders that are rotated and positioned.
Connected with that view is a NSTableView that lists the endpoints of the cylinders and lets the user edit them. When they exit an editor, the 3D view updates.
The problem is that my code current always adds new SKCylinders to the view with every redraw. So as they make edits, multiple copies of the SKCylinders end up in the view. I'm looking at the docs trying to figure out the best way to fix this.
1) should I simply remove all the geometry nodes before every draw and then make it fresh? Is there an easy way to find all the nodes that are geometry, rather than cameras or lights (or whatever)?
2) is there some way I can identify nodes within the collection so I could say that since line 5 of the geometry changed, I need to adjust node-with-something=5? I though about using name but I don't see a way to find a node by name
3) (2) is not a complete solution because I allow inserts and deletes in the list, so it might be "everything after this changes". Does that bring me to (1) or is there a better solution here?
Thanks for any advice!
I haven't used SceneKit yet but, from the documentation, it would seem that you can find nodes by name by calling:
SCNScene.rootNode.childNodeWithName( name, recursively: true)
or just iterate through childNodes recursively yourself.
Depending on the complexity of the nodes hierarchy, it may be tricky to implement insertions and deletions but, once you found the nodes you're looking for, that's just plumbing (prune and graft tree manipulations and such).

Are "swap move factories" worth the effort?

I noticed that for problems such as Cloudbalancing, move factories exist to generate moves and swaps. A "move move" transfers a cloud process from one computer to another. A "swap move" swaps any two processes from their respective computers.
I am developing a timetabling application.
A subjectTeacherHour (a combination of subject and teacher) have
only a subset of Periods to which they may be assigned. If Jane teaches 6 hours at a class, there are 6 subjectTeacherHours each which have to be allocated a Period, from a possible 30 Periods of that class ;unlike the cloudbalance example, where a process can move to any computer.
Only one subjectTeacherHour may be allocated a Period (naturally).
It tries to place subjectTeacherHour to eligible Periods , till an optimal combination is found.
Pros
The manual seems to recommend it.
...However, as the traveling tournament example proves, if you can remove
a hard constraint by using a certain set of big moves, you can win
performance and scalability...
...The `[version with big moves] evaluates a lot less unfeasible
solutions, which enables it to outperform and outscale the simple
version....
...It's generally a good idea to use several selectors, mixing fine
grained moves and course grained moves:...
While only one subjectTeacher may be allocated to Period, the solver must temporarily break such a constraint to discover that swapping two certain Period allocations lead to a better solution. A swap move "removes this brick wall" between those two states.
So a swap move can help lead to better solutions much faster.
Cons
A subjectTeacher have only a subset of Periods to which they may be assigned. So finding intersecting (common) hours between any two subjectTeachers is a bit tough (but doable in an elegant way: Good algorithm/technique to find overlapping values from objects' properties? ) .
Will it only give me only small gains in time and optimality?
I am also worried about crazy interactions having two kinds of moves may cause, leading to getting stuck at a bad solution.
Swap moves are crucial.
Consider 2 courses assigned to a room which is fully booked. Without swapping, it would have to break a hard constraint to move 1 course to a conflicted room and chose that move as the step (which is unlikely).
You can use the build-in generic swap MoveFactory. If you write your own, you can say the swap move isDoable() false when your moving either sides to an ineligible period.