Operational Research applied to SCRUM using Solver Studio (GMPL) - Golfarelli method - solver

I'm trying to code a version of Scrum Optimization method (Golfarelli, 2013) using GLPK/GMPL on Solver Studio - its for Academic purposes - Paper: https://drive.google.com/file/d/0B7W9UWp9Yzx4M0RUUnFpbkRlbE0/view?usp=sharing
This is my code so far:
#Modelo de Otimizacao de Sprints
# Conjunto de Indices
set Sprint;
set UserStory;
#set A;
#Parametros
param capacidade{Sprint};
param complexidade{UserStory};
param riscoincerteza{UserStory};
param riscocriticidade{UserStory};
param utilidade {UserStory};
#param afinidade {A};
#Variaveis
var x{Sprint, UserStory} binary;
#var y{Sprint, UserStory, A} >=0;
var UtilidadeSprint{Sprint} >=0;
var UtilidadeAcumulada{Sprint} >=0;
#FuncaoObjetivo
maximize SomaUtilidadeAcumulada: sum{i in Sprint} UtilidadeAcumulada[i];
#Restricoes
s.t. rest_UtilidadeAcumuladaPrimeiroSprint: UtilidadeAcumulada[1] = UtilidadeSprint[1];
s.t. rest_UtilidadeAcumuladaDemais{i in Sprint}: UtilidadeAcumulada[i+1] = UtilidadeSprint[i+1] + UtilidadeAcumulada[i];
s.t. rest_UtilidadeSprint{i in Sprint}: UtilidadeSprint[i] = sum{j in UserStory} utilidade[j] * riscocriticidade[j] * x[i, j];
s.t. rest_capacidade{i in Sprint}: sum{j in UserStory} complexidade[j] * riscoincerteza[j] * x[i,j] <= capacidade[i];
s.t. rest_NaoRepetirUserStory{j in UserStory}: sum{i in Sprint} x[i,j] = 1;
#Resolver Problrema
solve;
#Escrever Utilidade Acumulada na planilha
printf " UtilidadeAcumulada :=\n" >> "Sheet";
printf {i in Sprint} "'%s' '%s'\n", i, UtilidadeAcumulada[i] >> "Sheet";
printf ";\n" >> "Sheet";
#Escrever Utilidade Sprint na planilha
printf " UtilidadeSprint :=\n" >> "Sheet";
printf {i in Sprint} "'%s' '%s'\n", i, UtilidadeSprint[i] >> "Sheet";
printf ";\n" >> "Sheet";
#Escrever Solucao Otima na planilha
printf "rest_capacidade :=\n" >> "Sheet";
printf {i in Sprint} "'%s' '%s'\n", i, rest_capacidade[i] >> "Sheet";
printf ";\n" >> "Sheet";
printf "x :=\n" >> "Sheet";
printf {i in Sprint, j in UserStory}: "'%s' '%s' '%s'\n", i, j, x[i,j] >> "Sheet";
printf ";\n" >> "Sheet";
end;
I got stuck in my code (must because I'm not familiar with this language). It is not accepting UtilidadeAcumulada because it's going till "7" and should be just till "6" (I couldn't lock this parameter) - and I also not entered in the most complicated part (use of groupings and precedences). =(
I would really appreciate if someone familiar with SolverStudio (and GMPL) could give me some help on this.

Related

Problem with Modified Newton Raphson method on Mathematica

I am trying to get the roots of the function with the modified newton raphson method, but on the second iteration the value for xi+1 blows up to -393, isnt't it supposed to get closer to the expected value of the root? (which is 0.34997). Also I am trying to get the root with an "error" below the "eS" criteria. Pls help
n = 50;
eS = 10^-4;
f[x_] := (x^2 - 10 x + 25) (x - Exp[-3 x])
Plot[f[x], {x, -1, 2}]
xi = 0.5;
Do[
f[xi]; f'[xi]; f''[xi];
xi1 = xi - (f[xi]*f'[xi]/(f'[xi])^2 - f[xi]*f''[xi]);
If[Abs[f[xi]] < 10^-7,
Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
Break[]];
eA = Abs[(xi1 - xi)/xi1];
If[eA < eS,
Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
Break[]];
xi = xi1;
If[i == n, Print["Did not converge in ", n, " iteration(s)"]];
, {i, 1, n}
]
There are many different "Modified Newton Raphson" methods. The one I am familiar with is
u[x_] := f[x]/f'[x]
Do[
xi1 = xi - u[xi]/u'[xi];
If[Abs[f[xi]] < 10^-7,
Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
Break[]];
eA = Abs[(xi1 - xi)/xi1];
If[eA < eS,
Print["The root is approx= ", xi1 // N, " iterations needed: ", i];
Break[]];
xi = xi1;
If[i == n, Print["Did not converge in ", n, " iteration(s)"]];, {i, 1, n}]
Here is a more functional way (no Do loop)
FixedPointList[# - u[#]/u'[#] &, .5]
(* {0.5, 0.372215, 0.350544, 0.34997, 0.34997, 0.34997, 0.34997} *)

Creating a table with scrambled and unordered data

I'm trying to create a nice little table of values and I'm doing it using bash, but not all the values are in order. Not only that the values also happen to be in their own file. My first few thoughts are to use cat and grep to grab the values, but from there I'm not sure what is appropriate. I feel like awk would do wonders in this situation, but I do not know awk very well.
file1 might look like this
V 0.001
A 98.6
N Measurement1
T 14:15:01
S 20.2
F 212.86
G 28.19
file2 might look like this
V 0.008
A 103.4
N Measurement2
T 16:20:31
S 21.2
F 215.86
G 28.19
The final file would look like this
N Measurement1 Measurement2
T 14:15:01 16:20:31
V 0.001 0.008
G 28.19 28.19
A 98.6 103.4
S 20.2 21.2
F 212.86 215.86
self commented, code is provided for understanding awk
awk '
# cretate new reference (per file)
FNR==1{Ref++}
# each line
{ # add label to memory
N[$1]
# add value in 2 dimension array
V[Ref ":" $1] = $2
# remember maximum length of this serie
if( length( $2 ) > M[Ref] ) M[Ref] = length( $2 )
}
# after last file
END{
# print header (name of the serie)
printf( "N ")
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":N" ] )
printf( "\n")
# print each data for this label (format suite the size to be aligned)
# don t print a second time the name of the serie
for ( n in N ){
if( n != "N" ){
printf( "%s ", n)
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":" n ] )
printf( "\n")
}
}
}
' file*

How to do date/time calculation and formatting in an awk script?

I have a file test.txt with multiple records as below.
100,200,300,08-May-2012 11:24:25
100,400,300,25-May-2012 09:24:25
Now I want to output the data using the following "format":
$1,$2,$3,$4,$4+30days,$4+60days,$4+90days
How can I do that using awk?
script.awk file
function map_month(m)
{
if (m ~ /[jJ][aA][nN]/) return 1;
if (m ~ /[fF][eE][bB]/) return 2;
if (m ~ /[mM][aA][rR]/) return 3;
if (m ~ /[aA][pP][rR]/) return 4;
if (m ~ /[mM][aA][yY]/) return 5;
if (m ~ /[jJ][uU][nN]/) return 6;
if (m ~ /[jJ][uU][lL]/) return 7;
if (m ~ /[aA][uU][gG]/) return 8;
if (m ~ /[sS][eE][pP]/) return 9;
if (m ~ /[oO][cC][tT]/) return 10;
if (m ~ /[nN][oO][vV]/) return 11;
if (m ~ /[dD][eE][cC]/) return 12;
return 0;
}
function cvt_timestamp(str, t, a)
{
split(str, a, /[- :]/)
a[2] = map_month(a[2])
#print a[3] " " a[2] " " a[1] " " a[4] " " a[5] " " a[6]
t = mktime(a[3] " " a[2] " " a[1] " " a[4] " " a[5] " " a[6])
#print t
return t
}
function fmt_timestamp(t)
{
return strftime("%d-%b-%Y %H:%M:%S", t)
}
BEGIN { FS = OFS = "," }
{
tm = cvt_timestamp($4);
t0 = fmt_timestamp(tm + 0 * 86400)
t1 = fmt_timestamp(tm + 30 * 86400)
t2 = fmt_timestamp(tm + 60 * 86400)
t3 = fmt_timestamp(tm + 90 * 86400)
print $1, $2, $3, t0, t1, t2, t3
}
There's probably a better way to do the month abbreviation to number mapping; have at it. Using functions in awk is valuable just the same as it is in any other language with functions.
Example data file
100,200,300,08-May-2012 11:24:25
100,400,300,25-May-2012 09:24:25
100,400,300,15-Sep-2012 09:24:25
100,400,300,29-Feb-2012 09:24:25
Example output
$ gawk -f script.awk data
100,200,300,08-May-2012 11:24:25,07-Jun-2012 11:24:25,07-Jul-2012 11:24:25,06-Aug-2012 11:24:25
100,400,300,25-May-2012 09:24:25,24-Jun-2012 09:24:25,24-Jul-2012 09:24:25,23-Aug-2012 09:24:25
100,400,300,15-Sep-2012 09:24:25,15-Oct-2012 09:24:25,14-Nov-2012 08:24:25,14-Dec-2012 08:24:25
100,400,300,29-Feb-2012 09:24:25,30-Mar-2012 10:24:25,29-Apr-2012 10:24:25,29-May-2012 10:24:25
$
If you decide you want to handle switches in time zone differently, that's your prerogative; you can fix the code.

OpenCL (JOCL) - 2D calculus over two arrays in Kernel

I'm asking this here because I thought I've understood how OpenCL works but... I think there are several things I don't get.
What I want to do is to get the difference between all the values of two arrays, then calculate the hypot and finally get the maximum hypot value, so If I have:
double[] arrA = new double[]{1,2,3}
double[] arrB = new double[]{6,7,8}
Calculate
dx1 = 1 - 1; dx2 = 2 - 1; dx3 = 3 - 1, dx4= 1 - 2;... dxLast = 3 - 3
dy1 = 6 - 6; dy2 = 7 - 6; dy3 = 8 - 6, dy4= 6 - 7;... dyLast = 8 - 8
(Extreme dx and dy will get 0, but i don't care about ignoring those cases by now)
Then calculate each hypot based on hypot(dx(i), dy(i))
And once all these values where obtained, get the maximum hypot value
So, I have the next defined Kernel:
String programSource =
"#ifdef cl_khr_fp64 \n"
+ " #pragma OPENCL EXTENSION cl_khr_fp64 : enable \n"
+ "#elif defined(cl_amd_fp64) \n"
+ " #pragma OPENCL EXTENSION cl_amd_fp64 : enable \n"
+ "#else "
+ " #error Double precision floating point not supported by OpenCL implementation.\n"
+ "#endif \n"
+ "__kernel void "
+ "sampleKernel(__global const double *bufferX,"
+ " __global const double *bufferY,"
+ " __local double* scratch,"
+ " __global double* result,"
+ " __const int lengthX,"
+ " __const int lengthY){"
+ " const int index_a = get_global_id(0);"//Get the global indexes for 2D reference
+ " const int index_b = get_global_id(1);"
+ " const int local_index = get_local_id(0);"//Current thread id -> Should be the same as index_a * index_b + index_b;
+ " if (local_index < (lengthX * lengthY)) {"// Load data into local memory
+ " if(index_a < lengthX && index_b < lengthY)"
+ " {"
+ " double dx = (bufferX[index_b] - bufferX[index_a]);"
+ " double dy = (bufferY[index_b] - bufferY[index_a]);"
+ " scratch[local_index] = hypot(dx, dy);"
+ " }"
+ " } "
+ " else {"
+ " scratch[local_index] = 0;"// Infinity is the identity element for the min operation
+ " }"
//Make a Barrier to make sure all values were set into the local array
+ " barrier(CLK_LOCAL_MEM_FENCE);"
//If someone can explain to me the offset thing I'll really apreciate that...
//I just know there is alway a division by 2
+ " for(int offset = get_local_size(0) / 2; offset > 0; offset >>= 1) {"
+ " if (local_index < offset) {"
+ " float other = scratch[local_index + offset];"
+ " float mine = scratch[local_index];"
+ " scratch[local_index] = (mine > other) ? mine : other;"
+ " }"
+ " barrier(CLK_LOCAL_MEM_FENCE);"
//A barrier to make sure that all values where checked
+ " }"
+ " if (local_index == 0) {"
+ " result[get_group_id(0)] = scratch[0];"
+ " }"
+ "}";
For this case, the defined GWG size is (100, 100, 0) and a LWI size of (10, 10, 0).
So, for this example, both arrays have size 10 and the GWG and LWI are obtained as follows:
//clGetKernelWorkGroupInfo(kernel, device, CL.CL_KERNEL_WORK_GROUP_SIZE, Sizeof.size_t, Pointer.to(buffer), null);
long kernel_work_group_size = OpenClUtil.getKernelWorkGroupSize(kernel, device.getCl_device_id(), 3);
//clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES, Sizeof.size_t * numValues, Pointer.to(buffer), null);
long[] maxSize = device.getMaximumSizes();
maxSize[0] = ( kernel_work_group_size > maxSize[0] ? maxSize[0] : kernel_work_group_size);
maxSize[1] = ( kernel_work_group_size > maxSize[1] ? maxSize[1] : kernel_work_group_size);
maxSize[2] = ( kernel_work_group_size > maxSize[2] ? maxSize[2] : kernel_work_group_size);
// maxSize[2] =
long xMaxSize = (x > maxSize[0] ? maxSize[0] : x);
long yMaxSize = (y > maxSize[1] ? maxSize[1] : y);
long zMaxSize = (z > maxSize[2] ? maxSize[2] : z);
long local_work_size[] = new long[] { xMaxSize, yMaxSize, zMaxSize };
int numWorkGroupsX = 0;
int numWorkGroupsY = 0;
int numWorkGroupsZ = 0;
if(local_work_size[0] != 0)
numWorkGroupsX = (int) ((total + local_work_size[0] - 1) / local_work_size[0]);
if(local_work_size[1] != 0)
numWorkGroupsY = (int) ((total + local_work_size[1] - 1) / local_work_size[1]);
if(local_work_size[2] != 0)
numWorkGroupsZ = (int) ((total + local_work_size[2] - 1) / local_work_size[2]);
long global_work_size[] = new long[] { numWorkGroupsX * local_work_size[0],
numWorkGroupsY * local_work_size[1], numWorkGroupsZ * local_work_size[2]};
The thing is I'm not getting the espected values so I decided to make some tests based on a smaller kernel and changing the [VARIABLE TO TEST VALUES] object returned in a result array:
/**
* The source code of the OpenCL program to execute
*/
private static String programSourceA =
"#ifdef cl_khr_fp64 \n"
+ " #pragma OPENCL EXTENSION cl_khr_fp64 : enable \n"
+ "#elif defined(cl_amd_fp64) \n"
+ " #pragma OPENCL EXTENSION cl_amd_fp64 : enable \n"
+ "#else "
+ " #error Double precision floating point not supported by OpenCL implementation.\n"
+ "#endif \n"
+ "__kernel void "
+ "sampleKernel(__global const double *bufferX,"
+ " __global const double *bufferY,"
+ " __local double* scratch,"
+ " __global double* result,"
+ " __const int lengthX,"
+ " __const int lengthY){"
//Get the global indexes for 2D reference
+ " const int index_a = get_global_id(0);"
+ " const int index_b = get_global_id(1);"
//Current thread id -> Should be the same as index_a * index_b + index_b;
+ " const int local_index = get_local_id(0);"
// Load data into local memory
//Only print values if index_a < ArrayA length
//Only print values if index_b < ArrayB length
//Only print values if local_index < (lengthX * lengthY)
//Only print values if this is the first work group.
+ " if (local_index < (lengthX * lengthY)) {"
+ " if(index_a < lengthX && index_b < lengthY)"
+ " {"
+ " double dx = (bufferX[index_b] - bufferX[index_a]);"
+ " double dy = (bufferY[index_b] - bufferY[index_a]);"
+ " result[local_index] = hypot(dx, dy);"
+ " }"
+ " } "
+ " else {"
// Infinity is the identity element for the min operation
+ " result[local_index] = 0;"
+ " }"
The returned values are far of being the espected but, if the [VARIABLE TO TEST VALUES] is (index_a * index_b) + index_a, almost each value of the returned array has the correct (index_a * index_b) + index_a value, i mean:
result[0] -> 0
result[1] -> 1
result[2] -> 2
....
result[97] -> 97
result[98] -> 98
result[99] -> 99
but some values are: -3.350700319577517E-308....
What I'm not doing correctly???
I hope this is well explained and not that big to make you angry with me....
Thank you so much!!!!!
TomRacer
You have many problems in your code, and some of them are concept related. I think you should read the standard or OpenCL guide completely before starting to code. Because some of the system calls you are using have a different behaviour that what you expect.
Work-groups and work-items are NOT like CUDA. If you want 100x100 work-items, separated into 10x10 work-groups you use as global-size (100x100) and local-size(10x10). Unlike CUDA, where the global work item is multiplied by the local size internally.
1.1. In your test code, if you are using 10x10 with 10x10. Then you are not filling the whole space, the non filled area will still have garbage like -X.xxxxxE-308.
You should not use lengthX and lengthY and put a lot of ifs in your code. OpenCL has a method to call the kernels with offsets and with specific number of items, so you can control this from the host side. BTW doing this is a performance loss and is never a good practice since the code is less readable.
get_local_size(0) gives you the local size of axis 0 (10 in your case). What is what you do not understand in this call? Why do you divide it by 2 always?
I hope this can help you in your debugging process.
Cheers
thank you for your answer, first of all this kernel code is based on the commutative reduction code explained here: http://developer.amd.com/resources/documentation-articles/articles-whitepapers/opencl-optimization-case-study-simple-reductions/.
So I'm using that code but I added some things like the 2D operations.
Regarding to the point you mentioned before:
1.1- Actually the global work group size is (100, 100, 0)... That 100 is a result of multiplying 10 x 10 where 10 is the current array size, so my global work group size is based on this rule... then the local work item size is (10, 10, 0).
Global work group size must be a multiple of local work item size, I have read this in many examples and I think this is ok.
1.2- In my test code I'm using the same arrays, in fact if I change the array size GWG size and LWI size will change dinamically.
2.1- There are not so many "if" there, there are just 3 "if", the first one checks when I must compute the hypot() based on the array objects or fill that object with zero.
The second and third "if"s are just part of the reduction algorithm that seems to be fine.
2.2- Regarding to the lengthX and lengthY yeah you are right but I haven't got that yet, how should I use that??
3.1- Yeah, I know that, but I realized that I'm not using the Y axis id so maybe there is another problem here.
3.2- The reduction algorithm iterates over each pair of elements stored in the scratch variable and checking for the maximum value between them, so for each "for" that it does it is reducing the elements to be computed to the half of the previous quantity.
Also I'm going to post a some changes on the main kernel code and in the test kernel code because there where some errors.
Greetings...!!!

Shell script is printing out the wrong for loop

I have to create a diamond but I keep getting the wrong output.
I have written 3 loops, the first is the height, and the second is the space, and the third is the asterisk
The second part is the second half of the diamond
Output
enter shape
diamond
width
5
* * * *
* * * * * *
* * * * * *
* * * *
* * * *
* * * * * *
* * * * * *
* * * *
Script
"diamond")
echo "width"
read width
for ((h = 1; h <= $width; h++))
do
for ((s = $width; s > h; s--))
do
printf ' '
for ((a = 1; a <= h; a++))
do
printf '* '
done
done
echo ""
done
for ((h = $width; h > 0; h--))
do
for ((s = $width; s > h; s--))
do
printf ' '
for ((a=1 ; a <= h; a++))
do
printf '* '
done
done
echo ""
done
;;
How can I make this work?
The done for your "space" for-loop is in the wrong place for both the top and bottom of the diamond:
for ((h = 1; h <= $width; h++))
do
for ((s = $width; s > h; s--))
do
printf ' '
done # <-- This should be here!
for ((a = 1; a <= h; a++))
do
printf '* '
done
echo ""
done
I recommend running your script with bash -x to see what's going on.

Resources