Jump to content

  • Log In with Google      Sign In   
  • Create Account

Production graph search


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 comfy chair   Members   -  Reputation: 548

Like
1Likes
Like

Posted 29 October 2012 - 05:35 PM

In my game, I have modelled the production of items as a graph, where each node is an item (type) and the connections between them are processes to turn one or more material items into one or more products.

Now I want the player to be able to set a production goal, that is a desired number of items he wants produced of a give type.

The task is now for the computer to find the most efficient way of fulfilling the goal. There can be a number of material items already in store, and there can be a cost in time and energy for the processes. The cost in time and energy should be minimized.

Which search algorithm should I use? Neither A* nor BFS seem appropriate...

Edited by comfy chair, 29 October 2012 - 05:44 PM.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 29 October 2012 - 05:44 PM

This could be quite a bit more complex than a simple path search (how complex is your production tree by the way?) so you might want to look into automated planning techniques. Although I never did anything major with them so I might not be able to give you much more advice than that.

#3 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 29 October 2012 - 05:56 PM

I think I want to keep it simple to start with and not worry about scheduling.

#4 Telcontar   Members   -  Reputation: 893

Like
1Likes
Like

Posted 29 October 2012 - 10:11 PM

Barring any other knowledge about your data structure, you are going to need to fully explore all possible paths of production in order to know which is the most efficient. Thus, Dijkstra's algorithm seems like a good bet to start with. Imagine your edge weights as material costs instead of road lengths and it should be easy enough to apply.

I Create Games to Help Tell Stories | Writing Blog


#5 Emergent   Members   -  Reputation: 971

Like
3Likes
Like

Posted 29 October 2012 - 10:22 PM

This is an interesting question. I need to get some sleep so I'll keep this short (apologies if it's a bit telegraphic), but I hope the following is helpful:

This sounds very similar to max-flow (but not quite identical).

If you have n conversion processes and T time steps, picture a graph with n T nodes. Each node represents doing a particular process at a particular time. Then, you link all the nodes from one time to all the nodes at the next time. I'm picturing a vertical column of nodes for each time, and those columns arranged in order from early times to late times from left to right.

What you're going to do is optimize over flows on the edges.

What are our constraints? Let's look at an individual node. Say you have a process with two inputs and two outputs. The conversion it does is,
INPUT: 2 units of input resource 1 and 3 units of input resource 2
OUTPUT: 4 units of output resource 1 and 7 units of output resource 2.

Say the amounts of input resources you put in are a and b, and the amounts of output resources are x and y. Then, introducing a new variable z to represent the total number of "trades" done, the above is summarized by the constraints,

z <= a/2
z <= b/3
x <= 4 z
y <= 7 z

so you have inequality constraints relating the flows on your edges. Additionally, all the flows need to be nonnegative, meaning that they go the right direction in time:

x >= 0
y >= 0
a >= 0
b >= 0 .

You also have some equality constraints: The flows into the nodes at the far left of the graph (the "earliest" ones) are fixed; these are the quantities you start out with. And the flows at the far right of the graph have "greater-than" constraints, to say that you want produced at least this much of each resource.

Finally, you have a cost, which is just a weighted sum of the flows.

So what does this boil down to?
- You have as many variables as you have edges [EDIT: plus nodes; you also have the "z" variables]
- You have a linear cost (a weighted sum)
- You have linear inequality constraints

You can solve this exactly as an integer linear program, or you can relax the answer to be real (instead of just integer), in which case you just have a standard LP.

Does this formulation leave anything out?

Edited by Emergent, 30 October 2012 - 05:06 AM.


#6 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 30 October 2012 - 05:39 AM

The linear programming approach looks suitable for this problem. I don't have much experience with LP though. I hope you will post again if you think of more.

With this solution, will it be possible to present the user with a production plan for each of his orders, that he can inspect (and possibly cancel)?
The production plan would be: "Produce x items a, y items b" and so on.

Edited by comfy chair, 30 October 2012 - 05:53 AM.


#7 IADaveMark   Moderators   -  Reputation: 2472

Like
0Likes
Like

Posted 30 October 2012 - 08:06 AM

Combine solving the traveling salesman problem with the knapsack problem and you have two NP-hard problems -- at the same time. Don't be fixated on getting "the right answer". You won't. However, you can get something that is reasonable.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

#8 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 30 October 2012 - 08:49 AM

Dave,

I was afraid of that. But, keep the suggestions coming.

#9 IADaveMark   Moderators   -  Reputation: 2472

Like
1Likes
Like

Posted 30 October 2012 - 02:52 PM

Investigate using swarm/particle theory. It solves Traveling Salesman in freakish time. It can be reworked to incorporate the bin-packing as well.

Also, this is probably a good place for either MCTS or a straight up GA. Anything that converges on a solution rather than trying to build one from scratch is a good bet.
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

#10 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 30 October 2012 - 04:13 PM

I haven't used these techniques before. But they look great, and not too complicated. I guess it's time to learn something new.

#11 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 30 October 2012 - 05:00 PM

Anything that converges on a solution rather than trying to build one from scratch is a good bet.

Also, when the user changes an order to 3 instead of 2 products, it would be really nice if the computer could continue where it left off with the previous solution instead of starting over, and thereby save tons of computation. This would give real-time responsiveness and make the game a lot more fun.
MCTS seems to have this ability.

#12 Emergent   Members   -  Reputation: 971

Like
0Likes
Like

Posted 30 October 2012 - 06:54 PM

Combine solving the traveling salesman problem with the knapsack problem and you have two NP-hard problems -- at the same time. Don't be fixated on getting "the right answer". You won't. However, you can get something that is reasonable.


Do we know that traveling salesman and/or knapsack can be reduced to the poster's problem? I don't think we've established that the problem is NP-hard. In fact, I don't think we've committed to a particular problem statement at all.

Also: For a very long time, I have been inclined to dismiss NP-hard problems as unsolvable and move directly to heuristics or stochastic search (my current favorite is the cross-entropy method). But there is a middle ground: Many np-hard problems can be solved to within "epsilon" in polynomial time -- and some algorithms that can have exponential complexity practically never do (e.g., simplex algorithm). I'm developing a newfound respect for the Operations Research guys.

The linear programming approach looks suitable for this problem. I don't have much experience with LP though. I hope you will post again if you think of more.
With this solution, will it be possible to present the user with a production plan for each of his orders, that he can inspect (and possibly cancel)?
The production plan would be: "Produce x items a, y items b" and so on.


Sure, that's what's the flows represent. If you were to cancel production partway through, you'd be left with whatever quantities flow into the nodes corresponding to that timestep.

Let me play with this a little and see what I can do.

Also, when the user changes an order to 3 instead of 2 products, it would be really nice if the computer could continue where it left off with the previous solution instead of starting over, and thereby save tons of computation. This would give real-time responsiveness and make the game a lot more fun.


You can "warm start" most solvers with an old solution to a similar problem.

Edited by Emergent, 30 October 2012 - 07:03 PM.


#13 comfy chair   Members   -  Reputation: 548

Like
0Likes
Like

Posted 31 October 2012 - 02:59 AM

In fact, I don't think we've committed to a particular problem statement at all.

Now that you mention it, there are some additional features that I would really like to have in this game system.

The first is choice between inputs. Instead of requiring "2 of a", I would like to see "2 of a" OR "2 of b". This opens the door to a lot of realistic simulation of assembly processes.
Perhaps this requirement could be fulfilled by just adding more processes, I don't know.

On second thought, I think I will implement this as separate processes, but give the designer some shorthand tags to make it easier for him to make alternative processes.

The second is a quality level computed on each item. Each process would contain a formula that calculated the value of properties on its output(s), from the properties of its inputs.
The optimizer would then try to optimize for speed, quality, or a balance between the two.

Edited by comfy chair, 31 October 2012 - 09:37 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS