I have been tasked with writing a linear program that will tell the user where to add weight onto a cylindrical drum in order to balance the center of gravity. The weights are 2 lbs and 5 lbs, and a Maximum of 10 lbs can be added into one location. The 2 lb weights are 2" tall and the 5 lb weights are 6" tall. I think the best way to go about this is to use polar coordinates and assume a perfect cyinder for now, as it will be within 1% of perfect. I also think I should start only changing the X and Y axis and keep the Z axis at 0 for now. Any tips to head me in the right direction would be appreciated.
!Drum weight problem;
!sets;
Sets:
Weight: Pounds, Height;
Location: X, Y, Angle;
Set(Weight, Location): PX, PY, PAngle;
Endsets
!data;
Data:
Weight = W1 W2 W3 W4;
Location = L1 L2 L3 L4;
!attribute values;
Pounds = 2 4 5 10;
Height = 2 4 6 12;
X = 0 1 2 3;
Y = 0 1 2 3;
Angle = 0 90 180 270;
Enddata
!objective;
Min = #MIN(Set(I, J): Weight (I, J), Location (K, L, M);
!constraints;
#FOR( Weight(I): [Weight_row]
Pounds >= 2;
Height >= 2;
#FOR( Location(J): [Location_row]
X >=0;
Y >=0;
Angle >=0;
End
Related
Consider the fixed transformation pipeline of OpenGL, with the following parameters:
GL_MODELVIEW_MATRIX
0,175,303.109,0,688.503,-2741.84,1583,0,29.3148,5.52094,-3.18752,0,-87.4871,731.309,-1576.92,1
GL_PROJECTION_MATRIX
2.09928,0,0,0,0,3.73205,0,0,0,0,-1.00658,-1,0,0,-43.9314,0
GL_VIEWPORT
0,0,1920,1080
When I draw the faces of the unit cube I get the following:
By looking at the picture, I would expect half of the vertices to have pixel y-coordinate above 1080, and the other half to have a negative y-coordinate.
Instead, with gluProject, all vertices have y > 1080:
model coordinate 0 0 0 -> screen coordinate 848.191 1474.61 0.989359
model coordinate 1 0 0 -> screen coordinate 821.586 1973.88 0.986045
model coordinate 0 1 0 -> screen coordinate -198317 667165 4.61719
model coordinate 1 1 0 -> screen coordinate -2957.48 12504.1 1.07433
model coordinate 0 0 1 -> screen coordinate 885.806 1479.77 0.989388
model coordinate 1 0 1 -> screen coordinate 868.195 1979.01 0.986088
model coordinate 0 1 1 -> screen coordinate -438501 1.39841e+06 8.60228
model coordinate 1 1 1 -> screen coordinate -3191.35 12592.4 1.07507
I could successfully reproduce the gluProject() results with my "custom" calculations.
Why the y-coordinate of all vertices is above 1080?
P.S. To draw the cube I rely on:
glBegin(GL_QUADS);
for(int d = 0; d < 3; ++d)
for(int s = 0; s < 2; ++s)
for(int v = 0; v < 4; ++v)
{
const int a = (v & 1) ^ (v >> 1 & 1);
const int b = v >> 1 & 1;
const int d0 = d;
const int d1 = (d + 1) % 3;
const int d2 = (d + 2) % 3;
double p[3];
p[d] = s;
p[d1] = a;
p[d2] = b;
glColor3dv(p);
glVertex3dv(p);
}
glEnd();
I found the answer, in part thanks to this post.
The explanation is that the 4 vertices that have y < 0 in screen space, are also behind the camera, and so have w_clip < 0.
Perspective division (y_clip/w_clip) produces in turn a positive value in device independent coordinates and screen space.
I encountered this problem practicing for an upcoming national competition. The problem goes as follows: You need to create a mixture of two ingredients being in relation to 1:1. You are given N different mixtures, each having its own weight Wi, and its relation in the mixture between the ingredients Mi, Ti (Each value, N, Wi, Mi, and Ti, will be less than 100). We need to find the biggest possible weight of the final mixture, keeping the relation to 1:1. We can take from each given mixture how much we want, we don't necessarily need to take the whole mixture, we can take some portion of it.
So with the given relation 1:1 in the final mixture, we know that we need to have an equal amount of weight from both ingredients possible. After that I need to know if I take K grams of some mixture, how much weight that is for ingredients A and B. So I came up with the following formula:
Let W be the weight in grams, and M and T be the relation between the ingredients respectively. If we want to take K (K <= W) grams we have the following:
Weight of ingredient A = M * (K / (M+T))
Weight of ingredient B = T * (K / (M+T))
#include <bits/stdc++.h>
using namespace std;
class state{
public:
int weight;
int A;
int B;
};
int n;
vector<state> arr;
double ans= 0;
void f(double weight_A, double weight_B, int idx){
if(weight_A == weight_B)
ans = max(ans, weight_A + weight_B);
if(idx >= n)
return;
int weight = arr[idx].weight, relA = arr[idx].A, relB = arr[idx].B;
for(int K = 0; K <= weight; K++){
f(weight_A + relA * (K * 1.0/(relA + relB)), weight_B + relB * (K * 1.0/(relA + relB)), idx+1);
}
}
int main(){
cin>>n;
for(int i = 0; i < n; i++){
state in;
cin>>in.weight>>in.A>>in.B;
arr.push_back(in);
}
f(0.0, 0.0, 0);
cout<<fixed<<setprecision(8);
cout<<ans<<endl;
}
The problem I encountered was that we don't necessarily need to take integer weights, some times to achieve the maximum possible weight of the final product we need to take decimal weights. Let's take a look at this example:
5
14 3 2
4 1 3
4 2 2
6 6 1
10 4 3
We have N = 5, and in each row are given 3 integers, Wi, Mi, and Ti. The weight of the ith mixture and its relation. My solution for this example gives 20.0000, and the correct solution for the above example is 20.85714286. Looking back my initial idea won't work because of the decimal numbers. I suppose there is some formula but I can't figure it out, can anyone help?
This is a Linear Programming problem, so you can solve it by constructing the problem in standard form, and then solve it with an optimization algorithm, like the simplex algorithm.
The objective is to maximize the quantity of medicine (from the original problem), that is the sum of quantities taken from each jar (I'll call the quantities x1, x2, ...).
The quantities are bounded to be lower than the weight Wi available in each jar.
The constraint is that the total amount of honey (first ingredient) is equal to the total amount of tahini (second ingredient). This would mean that:
sum(Mi/(Mi+Ti)*xi) = sum(Ti/(Mi+Ti)*xi)
You can take the second summation to the LHS and get:
sum((Mi-Ti)/(Mi+Ti)*xi) = 0
In order to get integer multipliers just multiply everything by the least common multiple of the denominators lcm(Mi+ti) and then divide by the gcd of the coefficients.
Using your example, the constraint would be:
(3-2)/(3+2) x1 + (1-3)/(1+3) x2 + (2-2)/(2+2) x3 + (6-1)/(6+1) x4 + (4-3)/(4+3) x5 = 0
that is
1/5 x1 -2/4 x2 + 0/4 x3 + 5/7 x4 + 1/7 x5 = 0
Multiply by the lcm(5,4,4,7,7)=140:
28 x1 -70 x2 + 0 x3 + 100 x4 + 20 x5 = 0
divide by 2:
14 x1 -35 x2 +0 x3 + 50 x4 + 10 x5 = 0
We are ready to solve the problem. Let's write it in CPLEX format:
maximize
quantity: x1 + x2 + x3 + x4 + x5
subject to
mix: 14 x1 -35 x2 +0 x3 + 50 x4 + 10 x5 = 0
bounds
x1 <= 14
x2 <= 4
x3 <= 4
x4 <= 6
x5 <= 10
end
Feed it to GLPK:
#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>
int main(void)
{
glp_prob *P;
P = glp_create_prob();
glp_read_lp(P, NULL, "problem.cplex");
glp_adv_basis(P, 0);
glp_simplex(P, NULL);
glp_print_sol(P, "output.txt");
glp_delete_prob(P);
return 0;
}
And the output is:
Problem:
Rows: 1
Columns: 5
Non-zeros: 4
Status: OPTIMAL
Objective: quantity = 20.85714286 (MAXimum)
No. Row name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 mix NS 0 0 = 0.0714286
No. Column name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 x1 B 2.85714 0 14
2 x2 NU 4 0 4 3.5
3 x3 NU 4 0 4 1
4 x4 NL 0 0 6 -2.57143
5 x5 NU 10 0 10 0.285714
Karush-Kuhn-Tucker optimality conditions:
KKT.PE: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
KKT.PB: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
KKT.DE: max.abs.err = 0.00e+00 on column 0
max.rel.err = 0.00e+00 on column 0
High quality
KKT.DB: max.abs.err = 0.00e+00 on row 0
max.rel.err = 0.00e+00 on row 0
High quality
End of output
Of course given your input you should construct the problem in memory and feed it to the simplex algorithm without going through a file. Additionally, there's no need to get integer coefficients, it was just to allow a nicer problem formulation.
I have a circle centred at 0 with radius 80. How using python do I calculate the coordinates for 8 equidistant points around the circumference of the circle?
r = 80
numPoints = 8.0
points = []
for index in range(numPoints):
points.append([r*math.cos((index*2*math.pi)/numPoints),r*math.sin((index*2*math.pi)/numPoints)])
return points
you can simplify this some if you know you are always going to have only 8 points with something like:
r = 80
numPoints = 8
points = []
x = (r*math.sqrt(2))/2
points = [[0,r],[x,x],[r,0],[-x,x],[-r,0],[-x,-x],[0,-r],[x,-x]]
print points
with x being the x/y of the point 45 degrees and 80 units away from the origin
click this pic for more clarity
in the above picture.
coordinates 1,2,3,4,5,6,7,8 are equidistant points on a circumference of circle Radius R and its centre is at X (0,0)
take the triangle XLZ , its aright angled at L ,
Let LZ = H ,
LY = A
XL + LY = R => XL + A = R => XL = R-A
since XLZ is right angled , XZ square = XL square + LZ square
R square = (R-A) square + h square ————1
since these 8 points makes an octagon theta = 360 deg / 8 = 45 deg
tan 45 deg = h / XL = h / R-A => 1 = h/ R-A => h = R-A —————2
Z coordinates are (R-A, h) = > (h,h)
from the equations 1 and 2
R square = h square + h square => 2 h square = R square => h = R/ sqrt 2
so the coordinates at point 2 (Z) = (R/sqrt2, R/sqrt2)
remaining can be derived easily as they are just oppside
So all coordinates are
1 (0,R)
2 (R/sqrt2,R/sqrt2)
3 (R,0)
4 (-R/sqrt2, R/sqrt2)
5 (-R,0)
6 (-R/sqrt2,-R/sqrt2)
7 (0,-R)
8 (R/sqrt2, -R/sqrt2)
What is the fastes way in C++ to convert an index with such a formation to X, Y and Z coordinates and back ?
EDIT:
I want for example get for the index 15 the numbers X=0, Y=1, Z=2, for the index 17 the numbers X=2, Y=1, Z=2, and for the index 22 the numbers X=1, Y=2, Z=1.
I need this to emulate a multidimensional array.
To:
x = index % 3;
y = index / 3 % 3;
z = index / 9;
Back:
index = ((z) * 3 + y) * 3 + x;
I have recently been trying to render a 3D sphere in OpenGL using triangles. I have been testing and modifying code from various websites and have finally found a winning combination. The only problem is that there are visible gaps in the sphere. Any thoughts on what would be causing this?
Code to render sphere
float Slices = 30;
float Stacks = 60;
float Radius = 20.0;
for (int i = 0; i <= Stacks; ++i){
float V = i / (float) Stacks;
float phi = V * glm::pi <float> ();
for (int j = 0; j <= Slices; ++j){
float U = j / (float) Slices;
float theta = U * (glm::pi <float> () * 4);
float x = cosf (theta) * sinf (phi);
float y = cosf (phi);
float z = sinf (theta) * sinf (phi);
x *= Radius;
y *= Radius;
z *= Radius;
Vertex *v = new Vertex {{x,y,z}, //Position
{255,0,0}}; //Color
screenToBuffer(v, 1);
delete []v;
}
}
Problem
Try and set it to GL_TRIANGLE_STRIP
What might be the problem is that it considers each group of three vertices to be only one triangle.
Like so
Indices: 0 1 2 3 4 5 ...
Triangles: {0 1 2} {3 4 5}
The GL_TRIAGLE_STRIP will do this.
Indices: 0 1 2 3 4 5 ...
Triangles: {0 1 2}
{1 2 3} drawing order is (2 1 3) to maintain proper winding
{2 3 4}
{3 4 5}
See this answer for a proper way to do it.
https://stackoverflow.com/a/7958376/1943599