LP File, can not save in proper format. do I do it in word? - rstudio

How do you create and save a lp file?
/* Objective function */
Min Z = 5X1-2X2+4X3-3X4
/* Constraints */
Subject to
c1: 12x1+9x2+15x3>=125
c2: 5x1+3x2+4x3=40
3: 5x1+7x2+8x3<=15
c4: x1,x2,x3>=0
Cant save as an lp file

Related

random number generation and two DO....END loops in sas

I need to Use SAS random number generation functions RAND() and a DO....END loop to create 100 obs in variable named X then I want to use another DO loop of 500 rounds to generate a total of 500 samples, each with 100 obs. a sample is basically sampling from a standard normal distribution.
I tried the following code but it does not give me what I need:
data A;
call streaminit(123); /* set random number seed */
do i = 1 to 100;
X = rand("Normal"); /* random number generator */
output;
end;
do r = 1 to 500 ;
if i then X = rand("Normal");
output;
end;
run;
Any input will be greatly appreciated.
Perfect time to use PROC IML:
proc iml;
call streaminit(123); /* set seed */
x = j(500, 100); /* allocate 500 by 100 matrix */
call randgen(x, "Normal"); /* fill matrix with N(0,1) random draws */
create mydata from x; /* move matrix to a dataset in the work directory */
append from x;
close mydata;
quit;
Here is a data step solution
data want;
do I=1 to 500;
do _iorc_=1 to 100;
X=rand ("normal");
output;
end;
end;
run;

how to combine two same scene images for image registration

I try to do image registration on two grayscale images where the images were taken twice with different views. The images were taking by myself using a Lifecam camera.
To register these images, I used template matching method and normalized cross correlation as similarity measure and found the right location. But the result after combination of these two images was not good as I wish. I don't know how to fix it. Do I need to do some rotation or translation first before combine it? If so, I have no idea how to get the real angle for rotation. Or do you have any idea how to fix the image result without applying any rotation?
Input image 1:
Input Image 2:
Result:
This my code:
A = imread('image1.jpg');
B = imread('image2.jpg');
[M1, N1] = size(A); % size imej A n B
[M2, N2] = size(B);
%% finding coordinated of (r2,c2)
r1 = size(A,1)/2; % midpoint of image A as coordinate
c1 = size(A,2
template = imcrop(A,[(c1-20) (r1-20) 40 40]);
[r2, c2] = normcorr(temp,B); % Normalized cross correlation
%% count distance of coordinate (r1,c1) in image A and (r2,c2)in image B
UA = r1; % distance of coordinate (r1,c1) from top in image A
BA = M1 - r1; % distance of coordinate (r1,c1) from bottom
LA = c1; % left distance from (r1,c1)
RA = N1 - c1; % right distance from (r1,c1)
UB = r2; % finding distance of coordinate (r2,c2) from top,
BB = M2 - r2; % bottom, left and right in image B
LB = c2;
RB = N2 - c2;
%% zero padding for both image
if LA > LB
L_diff = LA - LB; % value of columns need to pad with zero on left side
B = [zeros(M2,L_diff),B];
else
L_diff = LB - LA;
A = [zeros(M1,L_diff),A];
end
if RA > RB
R_diff = RA - RB; % value of columns need to pad with zero on right side
B = [B, zeros(M2,R_diff)];
else
R_diff = RB - RA;
A = [A, zeros(M1,R_diff)];
end
N1 = size(A, 2); % renew value column image A and B
N2 = size(B, 2);
if UA > UB
U_diff = UA - UB; % value of rows need to pad with zero on top
B = [zeros(U_diff,N2);B];
else
U_diff = UB - UA;
A = [zeros(U_diff,N1);A];
end
if BA > BB
B_diff = BA - BB; % value of rows need to pad with zero on bottom
B = [B; zeros(B_diff,N2)];
else
B_diff = BB - BA;
A = [A; zeros(B_diff,N1)];
end
%% find coordinate that have double value
if LA > LB
r = r1;
c = c1;
else
r = r2;
c = c2;
end
if UA >= UB
i_Start = r - UB + 1;
else
i_Start = r - UA + 1;
end
if BA >= BB
i_Stop = r + BB ;
else
i_Stop = r + BA;
end
if LA >= LB
j_Start = c - c2 + 1;
else
j_Start = c - c1 + 1;
end
if RA >= RB
j_Stop = c + RB;
else
j_Stop = c + RA;
end
%% add image A and B
A = im2double(A);
B = im2double(B);
final_im = A + B;
for i = i_Start:i_Stop
for j = j_Start:j_Stop
final_im(i,j) = final_im(i,j)/2;
end
end
final_im = im2uint8(final_im);
The answer from rayryeng in Ryan L's first link is quite applicable here. Cross-correlation likely won't provide a close enough match between the two images since the transformation between the two images is more accurately described as a homography than a 2D rigid transform.
Accurate image registration requires that you find this projective transformation. To do so you can find a set of corresponding points in the two images (using SURF, as mentioned above, usually works well) and then use RANSAC to obtain the homography's parameters from the corresponding points. RANSAC does a nice job even when some of the "corresponding" features in your two images are actually not correct matches. Once found, you can use the transformation to move one of your images to the other's point of view and fuse.
Here's a nice explanation of feature matching, RANSAC, and fusing two images with some Matlab code samples. The lecture uses SIFT features, but the idea still works for SURF.
Best published way to perform such a registration is based on fiducial points. You can choose the most clear edges or crossing points as a fiducial and then adjust the smoothness and regularization parameter to register them together.
look at the SlicerRT package. and let me know if you face any problem.

How to create block image?

I have 2000 images, the size of each is Xi=320*512 double (i=1:1:2000). I want to regard each image as one block, so there are 2000 blocks, and then put them in one big image. For each block there is a label corresponding to it, the label ranges from 1 to 10. My question is how to put the 2000 images into a big block images with a label for each block as I described above?
I have 2000 images like this. Can anyone tell me how to put this kind of images into blocks?
My comment was incorrect, reshape will not solve your problem. However, I did use reshape to create an example array of images.
% Replace these with 320, 512, and 2000.
nx = 2;
ny = 3;
nz = 4;
% nz images, each of size nx by ny
images = reshape(1: nx * ny * nz, nx, ny, nz)
% Put each image into a larger image composed of n1 * n2 blocks
n1 = 2;
n2 = 2;
image = zeros(n1 * nx, n2 * ny);
% Note, nz == n1 * n2 must be true
iz = 0;
for i1 = 1: n1
for i2 = 1: n2
iz = iz + 1;
image((i1 - 1) * nx + 1: i1 * nx, (i2 - 1) * ny + 1: i2 * ny) ...
= images(:, :, iz);
end
end
image
This creates the big block image correctly. You may want to change the inner/outer order of the loops to do column-major ordering instead of row-major ordering.
Like paisanco, I am unsure what you want to do with labels.

What are "source" and "destination" parameters in MPI_Cart_shift?

Here it is written that the output parameters of MPI_Cart_shift are ranks of the source and destination processes. However, in this tutorial (code below) what is returned as the source process is later used in MPI_Isend to send messages. Anyone can clear it up - what actually "source" and "destination" mean?
#include "mpi.h"
#include <stdio.h>
#define SIZE 16
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int main(argc,argv)
int argc;
char *argv[]; {
int numtasks, rank, source, dest, outbuf, i, tag=1,
inbuf[4]={MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL,MPI_PROC_NULL,},
nbrs[4], dims[2]={4,4},
periods[2]={0,0}, reorder=0, coords[2];
MPI_Request reqs[8];
MPI_Status stats[8];
MPI_Comm cartcomm;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
if (numtasks == SIZE) {
MPI_Cart_create(MPI_COMM_WORLD, 2, dims, periods, reorder, &cartcomm);
MPI_Comm_rank(cartcomm, &rank);
MPI_Cart_coords(cartcomm, rank, 2, coords);
MPI_Cart_shift(cartcomm, 0, 1, &nbrs[UP], &nbrs[DOWN]);
MPI_Cart_shift(cartcomm, 1, 1, &nbrs[LEFT], &nbrs[RIGHT]);
printf("rank= %d coords= %d %d neighbors(u,d,l,r)= %d %d %d %d\n",
rank,coords[0],coords[1],nbrs[UP],nbrs[DOWN],nbrs[LEFT],
nbrs[RIGHT]);
outbuf = rank;
for (i=0; i<4; i++) {
dest = nbrs[i];
source = nbrs[i];
MPI_Isend(&outbuf, 1, MPI_INT, dest, tag,
MPI_COMM_WORLD, &reqs[i]);
MPI_Irecv(&inbuf[i], 1, MPI_INT, source, tag,
MPI_COMM_WORLD, &reqs[i+4]);
}
MPI_Waitall(8, reqs, stats);
printf("rank= %d inbuf(u,d,l,r)= %d %d %d %d\n",
rank,inbuf[UP],inbuf[DOWN],inbuf[LEFT],inbuf[RIGHT]); }
else
printf("Must specify %d processors. Terminating.\n",SIZE);
MPI_Finalize();
}
MPI_Cart_shift: Returns the shifted source and destination ranks, given a shift direction and amount
int MPI_Cart_shift(MPI_Comm comm, int direction, int displ, int *source, int *dest)
What you hand in to the function is comm, direction and displ. Where direction specifies the dimension in which the displacement is taken. The displacement is the distance.
Example
Imagine a 2D cart topology like this (names are not ranks but process-names, only for explanation):
A1 A2 A3 A4 A5
B1 B2 B3 B4 B5
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5
E1 E2 E3 E4 E5
As you might already have understood you are writing SPMD-Code in MPI, therefore we can now pick, w.l.o.g., one process to show what is happening. Let's pick C3
The general idea of MPI_Cart_shift is that we get the rank of a specified process in our topology.
First, we have to decide in which direction we want to go, let's pick 0, which is the column dimension.
Then we have to specify a distance to the other process, let's say this is 2.
So the call would be like:
MPI_Cart_shift(cartcomm, 0, 2, &source, &dest);
Now, the ranks which are placed into the source and dest variables are those respectively of the processes A3 and E3.
How to interpret the results
I (C3) want to send data to the process in the same column with a distance of 2. So this is the dest rank.
If you do the same from the viewpoint of A3: process A3 gets as its dest field the rank of C3.
And this is what source says: what is the rank of the process which is sending me those data if it calls the same MPI_Cart_shift.
If there is no process at the specified place the variable contains MPI_PROC_NULL.
So the results of the call at each process would look like this (with source|dest for each process, using - for MPI_PROC_NULL):
MPI_Cart_shift(cartcomm, 0, 2, &source, &dest);
A1 A2 A3 A4 A5
-|C1 -|C2 -|C3 -|C4 -|C5
B1 B2 B3 B4 B5
-|D1 -|D2 -|D3 -|D4 -|D5
C1 C2 C3 C4 C5
A1|E1 A2|E2 A3|E3 A4|E4 A5|E5
D1 D2 D3 D4 D5
B1|- B2|- B3|- B4|- B5|-
E1 E2 E3 E4 E5
C1|- C2|- C3|- C4|- C5|-
Additional bit of information
If you create the cart with any dimension set periods = 1 then there is a virtual edge between the first and the last node of the cart. In this example, periods[0] = 1 would make a connection between A1 and E1, between A2 and E2, and so on. If you then call the MPI_Cart_shift, the counting has to be wrapped around the corners so your output would be:
A1 A2 A3 A4 A5
D1|C1 D2|C2 D3|C3 D4|C4 D5|C5
B1 B2 B3 B4 B5
E1|D1 E2|D2 E3|D3 E4|D4 E5|D5
C1 C2 C3 C4 C5
A1|E1 A2|E2 A3|E3 A4|E4 A5|E5
D1 D2 D3 D4 D5
B1|A1 B2|A2 B3|A3 B4|A4 B5|A5
E1 E2 E3 E4 E5
C1|B1 C2|B2 C3|B3 C4|B4 C5|B5
MPI_Cart_shift is a convenience function. It's primary usage is for data shifts, i.e. operations in which each rank sends data in a certain direction (i.e. to destination) and receives data from the opposite direction (i.e. from source) (forward operation). When source is used as destination and destination as source, data flows in the opposite direction (backward operation). An example of such operation is the halo swapping and it usually requires two shifts along each dimension - one forward and one backward.
MPI_Cart_shift is a convenience function since its action is equivalent to the following set of MPI calls:
// 1. Determine the rank of the current process
int rank;
MPI_Comm_rank(cartcomm, &rank);
// 2. Transform the rank into topology coordinates
int coords[ndims];
MPI_Cart_coords(cartcomm, rank, ndims, coords);
// 3. Save the current coordinate along the given direction
int saved_coord = coords[direction];
// 4. Compute the "+"-shifted position and convert to rank
coords[direction] = saved_coord + displ;
// Adjust for periodic boundary if necessary
if (periods[direction])
coords[direction] %= dims[direction];
// 5. Convert to rank
MPI_Cart_rank(cartcomm, coords, &destination);
// 6. Compute the "-"-shifted position and convert to rank
coords[direction] = saved_coord - displ;
// Adjust for periodic boundary
if (periods[direction])
coords[direction] %= dims[direction];
// 7. Convert to rank
MPI_Cart_rank(cartcomm, coords, &source);
One could also compute the rank<->coordinate transforms using arithmetic without calls to MPI_Cart_rank or MPI_Cart_coords but it would be very inflexible as the formulas change when the dimensionality of the topology changes.
Something very important. The ranks as computed by MPI_Cart_shift (or by the equivalent code above) are related to the cartcomm communicator. Those match the ranks in the original communicator (the one used in MPI_Cart_create) only if reorder = 0. When reordering is allowed, the ranks could differ and therefore one should not use those ranks within the context of the original communicator. The following code of yours is valid but strongly dependent on the fact that reorder = 0 in the call to MPI_Cart_create:
dest = nbrs[i];
source = nbrs[i];
MPI_Isend(&outbuf, 1, MPI_INT, dest, tag,
MPI_COMM_WORLD, &reqs[i]);
MPI_Irecv(&inbuf[i], 1, MPI_INT, source, tag,
MPI_COMM_WORLD, &reqs[i+4]);
Here nbrs are computed within cartcomm and then used within MPI_COMM_WORLD. The correct code should use cartcomm in both communication calls:
MPI_Isend(&outbuf, 1, MPI_INT, dest, tag,
cartcomm, &reqs[i]);
MPI_Irecv(&inbuf[i], 1, MPI_INT, source, tag,
cartcomm, &reqs[i+4]);
Some algorithms require that data travels the other way round, i.e. forward and backward are swapped. For such algorithms the displacement displ specified could be negative. In general, a call to MPI_Cart_shift with negative displacement is equivalent to a call with positive displacement but source and destination swapped.

UITextField int data type xcode

I am trying to get a mathmetical equation to recognise a + /- sign of an integer (either -1 or +1) entered in a UItextfield (s1, s2). So if the user enters different signs the equations will be subtracted from each other.
It seems that the sign is not being recognised for some reason and the program just adds d1 and d2.
-(IBAction)calculateD:(id)sender{
float n1, r1, n2, r1, d, d1, d2;
int s1, s2;
s1= [textfieldS1.text intvalue]; //etc for all variables
d1 = s1 * ((n1-1)/r1);
d2 = s2 * ((n2-1)/r2);
if (s1 != s2) { d = d1 - d2;}
else { d = d1 + d2;
}}
Any problems apparent in this code please?
I have no idea what you are trying to do here. Variables are not initialized and there is no specific reference to actual UITextField inside of the -calculateD: method. With this said, here are some hints, hope it will come in hand.
The signs s1, s2 are actually taken twice into a consideration. Once to produce d1, d2, and later to decide the (s1 != s2). Because of this, the latter will make sure you add two numbers of the same sign, possibly negating what you really want to obtain here. Example:
say that s1=+1, s1=+1, then you got d = ((n1-1)/r1) + ((n2-1)/r2);
say that s1=+1, s1=-1, then you got d = ((n1-1)/r1) + ((n2-1)/r2); the same as before;
Just drop the if, and leave a single: d = d1 + d2.

Resources