Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 553 554 [555] 556 557 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 906592 times)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8310 on: October 27, 2015, 08:41:14 am »

If you're doing any sort of complex scenework in 3D however, it's extremely unlikely your homebrew OpenGL 3D engine is going to be as efficient as existing engines however.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8311 on: October 27, 2015, 11:26:06 am »

Hmmm... I got my own implementation of quadtrees working, and it successfully reduced the number of collision comparisons to 0.166 what it was before, using about 3.50% of my (quad-core) CPU while moving around and drawing 1000 AABBs at 60fps. But one of the code examples I looked at looks like this:

Code: [Select]
public List retrieve(List returnObjects, Rectangle pRect) {
   int index = getIndex(pRect);
   if (index != -1 && nodes[0] != null) {
     nodes[index].retrieve(returnObjects, pRect);
   }
 
   returnObjects.addAll(objects);
 
   return returnObjects;
 }

This function returns a list of objects that the given rect could potentially be intersecting. When getIndex returns -1, it means the given rect doesn't fit within one of the child nodes, so must be placed within the current one. The problem I found with this function is that it's not symmetrical. Say two collision rectangles are added to the quadtree, and one is placed into one of the child nodes as it fits entirely within one quadrant. But the other is halfway between two of the quadrants, so it gets placed in the parent node. Using the function above, the former will register as colliding with the latter, but not vice versa.

I can't even tell if that's intended behavior here.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8312 on: October 27, 2015, 12:26:29 pm »

Hmmm... I got my own implementation of quadtrees working, and it successfully reduced the number of collision comparisons to 0.166 what it was before, using about 3.50% of my (quad-core) CPU while moving around and drawing 1000 AABBs at 60fps. But one of the code examples I looked at looks like this:

Code: [Select]
public List retrieve(List returnObjects, Rectangle pRect) {
   int index = getIndex(pRect);
   if (index != -1 && nodes[0] != null) {
     nodes[index].retrieve(returnObjects, pRect);
   }
 
   returnObjects.addAll(objects);
 
   return returnObjects;
 }

This function returns a list of objects that the given rect could potentially be intersecting. When getIndex returns -1, it means the given rect doesn't fit within one of the child nodes, so must be placed within the current one. The problem I found with this function is that it's not symmetrical. Say two collision rectangles are added to the quadtree, and one is placed into one of the child nodes as it fits entirely within one quadrant. But the other is halfway between two of the quadrants, so it gets placed in the parent node. Using the function above, the former will register as colliding with the latter, but not vice versa.

I can't even tell if that's intended behavior here.

When searching a space partitioning tree for things that collide with an object, you need to look not only at the object node's parent nodes, but also at its child nodes.
Logged

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8313 on: October 27, 2015, 12:52:32 pm »

Yeah, I think the implementation I used as an example is just flawed. I'm going to look at a few more examples of quadtree classes and change that retrieval function up a bit.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8314 on: October 27, 2015, 02:37:54 pm »

Hey I need to write a program for solving a linear programming (optimization) problem using the graphical method. This boils down to solving a system of linear inequations of two variables by graphing -- that is the area of the points which coordinates satisfy each of the inequations is to be determined.

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
« Last Edit: October 27, 2015, 02:43:45 pm by RoguelikeRazuka »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8315 on: October 27, 2015, 03:03:03 pm »

Hey I need to write a program for solving a linear programming (optimization) problem using the graphical method. This boils down to solving a system of linear inequations of two variables by graphing -- that is the area of the points which coordinates satisfy each of the inequations is to be determined.

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
That's a Math Help Thread problem, methinks.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8316 on: October 27, 2015, 03:09:39 pm »

Hey I need to write a program for solving a linear programming (optimization) problem using the graphical method. This boils down to solving a system of linear inequations of two variables by graphing -- that is the area of the points which coordinates satisfy each of the inequations is to be determined.

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
That's a Math Help Thread problem, methinks.

No-no-no, I do know how to do it using a sheet of paper and a pencil. But how to program it?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8317 on: October 27, 2015, 03:14:42 pm »

Hey I need to write a program for solving a linear programming (optimization) problem using the graphical method. This boils down to solving a system of linear inequations of two variables by graphing -- that is the area of the points which coordinates satisfy each of the inequations is to be determined.

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
That's a Math Help Thread problem, methinks.

No-no-no, I do know how to do it using a sheet of paper and a pencil. But how to program it?
Plug in the values, see if they are defined on the function.
At least, I thought of doing it that way.
But where's your problem in transitioning from paper and pencil to computerized?
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8318 on: October 27, 2015, 03:34:13 pm »

Hey I need to write a program for solving a linear programming (optimization) problem using the graphical method. This boils down to solving a system of linear inequations of two variables by graphing -- that is the area of the points which coordinates satisfy each of the inequations is to be determined.

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
That's a Math Help Thread problem, methinks.

No-no-no, I do know how to do it using a sheet of paper and a pencil. But how to program it?
Plug in the values, see if they are defined on the function.
At least, I thought of doing it that way.
But where's your problem in transitioning from paper and pencil to computerized?
Suppose we have n constraints (n lines) and less-than-n points of their intersection with one another. That means the area is unbounded in the direction of the gradient of the objective function. How do I find it out not knowing how the polygon (which is shaped by the area) looks like?
« Last Edit: October 27, 2015, 03:37:14 pm by RoguelikeRazuka »
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8319 on: October 27, 2015, 03:36:51 pm »

To graph the region you need what's called a clipping algorithm to make the polygon. Possible steps would be :-

- calculate the set of all intersection points. (trivial)
- work out the smallest rectangle that holds all of the points, keep it aligned to the axes (trivial)
- feed this rectangle and your lines into a clipping algorithm of your choice. (not very hard).
- use your chosen  graphics library to render the polygon created in the last step with a fill color (trivial)

For calculating the optimal points, it's actually hard to answer that because we don't know what sorts of functions you're trying to solve. If you have differentiable functions however (partial derivatives for two or more variables), you can use Newton's Method or Gradient Descent to iteratively generate approximate solutions for your function.
https://en.wikipedia.org/wiki/Newton's_method
https://en.wikipedia.org/wiki/Gradient_descent
They're similar but a little different in the formula used to generate the next approximation.
« Last Edit: October 27, 2015, 03:43:32 pm by Reelya »
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8320 on: October 27, 2015, 03:41:29 pm »

To graph the region you need what's called a clipping algorithm to make the polygon. Possible steps would be :-

- calculate the set of all intersection points. (trivial)
- work out the smallest rectangle that holds all of the points, keep it aligned to the axes (trivial)
- feed this rectangle and your lines into a clipping algorithm of your choice. (not very hard).
- use your chosen  graphics library to render the polygon created in the last step with a fill color (trivial)

For calculating the optimal points, it's actually hard to answer that because we don't know what sorts of functions you're trying to solve. If you have differentiable functions however (partial derivatives for two or more variables), you can use Newton's Method or Gradient Descent to iteratively generate approximate solutions for your function.
https://en.wikipedia.org/wiki/Newton's_method
https://en.wikipedia.org/wiki/Gradient_descent
They're similar but a little different in the formula used to generate the next approximation.
The objective funtion is linear, each of the constraints is linear too.
« Last Edit: October 27, 2015, 03:43:48 pm by RoguelikeRazuka »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8321 on: October 27, 2015, 03:42:31 pm »

To graph the region you need what's called a clipping algorithm to make the polygon. Possible steps would be :-

- calculate the set of all intersection points. (trivial)
- work out the smallest rectangle that holds all of the points, keep it aligned to the axes (trivial)
- feed this rectangle and your lines into a clipping algorithm of your choice. (not very hard).
- use your chosen  graphics library to render the polygon created in the last step with a fill color (trivial)

For calculating the optimal points, it's actually hard to answer that because we don't know what sorts of functions you're trying to solve. If you have differentiable functions however (partial derivatives for two or more variables), you can use Newton's Method or Gradient Descent to iteratively generate approximate solutions for your function.
https://en.wikipedia.org/wiki/Newton's_method
https://en.wikipedia.org/wiki/Gradient_descent
They're similar but a little different in the formula used to generate the next approximation.
Interesting. And what if there's no 'smallest rectangle that holds all of the points'?
There always is?
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8322 on: October 27, 2015, 03:44:40 pm »

To graph the region you need what's called a clipping algorithm to make the polygon. Possible steps would be :-

- calculate the set of all intersection points. (trivial)
- work out the smallest rectangle that holds all of the points, keep it aligned to the axes (trivial)
- feed this rectangle and your lines into a clipping algorithm of your choice. (not very hard).
- use your chosen  graphics library to render the polygon created in the last step with a fill color (trivial)

For calculating the optimal points, it's actually hard to answer that because we don't know what sorts of functions you're trying to solve. If you have differentiable functions however (partial derivatives for two or more variables), you can use Newton's Method or Gradient Descent to iteratively generate approximate solutions for your function.
https://en.wikipedia.org/wiki/Newton's_method
https://en.wikipedia.org/wiki/Gradient_descent
They're similar but a little different in the formula used to generate the next approximation.
The objective funtion is linear, each of the constraints is linear too.
If the objective function is linear, then just compute that function at each intersection point. It'll be higher or lower at one of them, and you can forget the points in the middle. If there is actually an unbounded edge, well you need to make a bound for your drawing, because finite size of graph.
« Last Edit: October 27, 2015, 03:54:24 pm by Reelya »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8323 on: October 27, 2015, 03:46:40 pm »

Supose I have found the points of intersection of the lines that confine the area. How do I identify which one of those points is the point where the objective function reaches its maximum or minimum? It also can be that there is either no maximum or no minimum, or both no maximum and no minimum.
Suppose we have n constraints (n lines) and less-than-n points of their intersection with one another. That means the area is unbounded in the direction of the gradient of the objective function. How do I find it out not knowing how the polygon (which is shaped by the area) looks like?
To find a potential maximum, just evaluate the objective function f at all intersections of two lines and take the intersection P with the highest objective value f(P) such that P still fulfills all inequalities. To determine whether that point is actually maximal, take the line {Q : f(Q) = f(P) + 1} and intersect it with all the given boundary lines. If none of the resulting points is valid, then P is indeed maximal, otherwise P is not maximal.
« Last Edit: October 27, 2015, 03:49:02 pm by MagmaMcFry »
Logged

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8324 on: October 27, 2015, 03:47:46 pm »

To graph the region you need what's called a clipping algorithm to make the polygon. Possible steps would be :-

- calculate the set of all intersection points. (trivial)
- work out the smallest rectangle that holds all of the points, keep it aligned to the axes (trivial)
- feed this rectangle and your lines into a clipping algorithm of your choice. (not very hard).
- use your chosen  graphics library to render the polygon created in the last step with a fill color (trivial)

For calculating the optimal points, it's actually hard to answer that because we don't know what sorts of functions you're trying to solve. If you have differentiable functions however (partial derivatives for two or more variables), you can use Newton's Method or Gradient Descent to iteratively generate approximate solutions for your function.
https://en.wikipedia.org/wiki/Newton's_method
https://en.wikipedia.org/wiki/Gradient_descent
They're similar but a little different in the formula used to generate the next approximation.
Interesting. And what if there's no 'smallest rectangle that holds all of the points'?
There always is?

Nevermind.

Actually I don't necessarily need to graph the region and fill it with colour; I think just showing all the lines would be enough. But to get the answer to whether there is an optimum and if there is one which it is (an objective function may reach it at one point or at all of the points of a line) is necessary.
« Last Edit: October 27, 2015, 04:13:34 pm by RoguelikeRazuka »
Logged
Pages: 1 ... 553 554 [555] 556 557 ... 796