# Constrained/penalized distance function

Assume a character is located on a n by n grid and has to reach a certain entry on that grid. Its current position is `(x1,y1)`. Also on the same grid is an enemy with coordinates `(x2,y2)`. Each step algorithm randomly generates new candidate locations for the hero (if there are k candidates then there is a kx2 matrix of new potential locations.

What I need is some distance objective function to compare the candidates. I'm currently using `d1 - c * d2`, where `d1` is distance to the objective (measure in terms of number of pixels for each axis), `d2` is distance to the enemy and `c` is some coefficient (this is very much like a set-up for Lagrangian). It's not working very well though. I'd be quite keen to learn how what constrained distance function are used for similar cases.

Any suggestions are very much appreciated.

-
I'm not sure I understand what your problem is. Is that an AI problem, i.e. is your "character" or "hero" located in `(x1,y1)` computer-driven? If so, are you trying to make this AI both go to its goal, let's say in `(xg,yg)`, and avoid its enemy in `(x2,y2)`? Please try to clarify this a bit. – Laurent Couvidou Apr 6 '12 at 11:03
Maybe you should just skip all the maths and tell us in plain words what behaviour you are trying to achieve. – aaaaaaaaaaaa Apr 6 '12 at 15:02
You need to say, at minimum, what you consider "not working very well." – John Calsbeek Apr 6 '12 at 15:58
OK, here's the challenge: the character needs to et to some entry on the grid without being killed by an enemy which performs a random walk. For this purpose the step that the cahracter takes has to account for 1)distance to the target 2)distance to the enemy. What's the best way of doing it? – sigma.z.1980 Apr 8 '12 at 1:09