-
Notifications
You must be signed in to change notification settings - Fork 1
/
Add_obj.c
75 lines (61 loc) · 1.48 KB
/
Add_obj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "lp_var.h"
#include "glpk_var.h"
float GeneGauss(int mean,float stdev);
void Add_obj()
{
int i;
char num[5];
sig = (float*)malloc(n * sizeof(float));
for(i = 0; i < n; i++)
sig[i] = 1.0;
stdev = sqrt(1.0 / (2*CODERATE*pow(10,SNR/10)));
for(i = 0; i < n; i++)
sig[i] += GeneGauss(0,stdev);
/////////////////////////////////////////////////////////
// assign the sig to cplex as the r
// here just print them
/* glpk_data = fopen("//home//why/document//lp//lp.txt","a+");
fprintf(glpk_data,"minimize f : %f*f1",sig[0]);
for(int i =1; i < n; i ++)
fprintf(glpk_data,"+%f*f%d" , sig[i],i+1);
fprintf(glpk_data,";\n");
fclose(glpk_data);
*/
/////////////////////////////////////////////////////////
glp_add_cols(lp, n);
glp_set_obj_dir(lp, GLP_MIN);
for(i = 1; i <= n; i ++)
{
sprintf(num, "%d", i);
glp_set_col_name(lp, i, num);
glp_set_col_bnds(lp, i, GLP_DB, 0.0, 1.0);
glp_set_obj_coef(lp, i, sig[i-1]);
}
free(sig);
}
float GeneGauss(int mean,float stdev)
{
static float V1, V2, S;
static int phase = 0;
float X;
if(phase == 0)
{
do
{
float U1 = (float)rand() / RAND_MAX;
float U2 = (float)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
}while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
}
else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return (X * stdev + mean);
}