- fprintf(syncState->graphsStream,
- "\t\"analysis_eval_hrtt-%2$03d_and_%1$03d.data\" "
- "title \"RTT/2\" with boxes linetype 1 linewidth 3 "
- "linecolor rgb \"black\" fill transparent solid 0.75, \\\n"
- /*"\t\"analysis_eval_tt-%1$03d_to_%2$03d.data\" "
- "title \"%1$u to %2$u\" with boxes linetype 1 linewidth 3 "
- "linecolor rgb \"black\" fill transparent solid 0.5, \\\n"
- "\t\"analysis_eval_tt-%2$03d_to_%1$03d.data\" "
- "title \"%2$u to %1$u\" with boxes linetype 1 linewidth 3 "
- "linecolor rgb \"black\" fill transparent solid 0.25, \\\n"*/
- , i, j);
- /*
- fprintf(syncState->graphsStream,
- "\t\"analysis_eval_hrtt-%2$03d_and_%1$03d.data\" "
- "title \"RTT/2\" with linespoints linetype 1 linewidth 3 "
- "linecolor rgb \"black\", \\\n"
- "\t\"analysis_eval_tt-%1$03d_to_%2$03d.data\" "
- "title \"%1$u to %2$u\" with linespoints linetype 1 linewidth 3 "
- "linecolor rgb \"gray70\", \\\n"
- "\t\"analysis_eval_tt-%2$03d_to_%1$03d.data\" "
- "title \"%2$u to %1$u\" with linespoints linetype 1 linewidth 3 "
- "linecolor rgb \"gray40\", \\\n", i, j);
-*/
+ unsigned int it;
+ const int zero= 0;
+ const double zeroD= 0.;
+ glp_prob* lp= glp_create_prob();
+ unsigned int hullPointNb= g_queue_get_length(lowerHull) +
+ g_queue_get_length(upperHull);
+ GArray* iArray= g_array_sized_new(FALSE, FALSE, sizeof(int), hullPointNb +
+ 1);
+ GArray* jArray= g_array_sized_new(FALSE, FALSE, sizeof(int), hullPointNb +
+ 1);
+ GArray* aArray= g_array_sized_new(FALSE, FALSE, sizeof(double),
+ hullPointNb + 1);
+ struct {
+ GQueue* hull;
+ struct LPAddRowInfo rowInfo;
+ } loopValues[2]= {
+ {lowerHull, {lp, GLP_UP, iArray, jArray, aArray}},
+ {upperHull, {lp, GLP_LO, iArray, jArray, aArray}},
+ };
+
+ // Create the LP problem
+ glp_term_out(GLP_OFF);
+ glp_add_rows(lp, hullPointNb);
+ glp_add_cols(lp, 2);
+
+ glp_set_col_name(lp, 1, "a0");
+ glp_set_col_bnds(lp, 1, GLP_FR, 0., 0.);
+ glp_set_col_name(lp, 2, "a1");
+ glp_set_col_bnds(lp, 2, GLP_LO, 0., 0.);
+
+ // Add row constraints
+ g_array_append_val(iArray, zero);
+ g_array_append_val(jArray, zero);
+ g_array_append_val(aArray, zeroD);
+
+ for (it= 0; it < sizeof(loopValues) / sizeof(*loopValues); it++)
+ {
+ g_queue_foreach(loopValues[it].hull, &gfLPAddRow,
+ &loopValues[it].rowInfo);
+ }
+
+ g_assert_cmpuint(iArray->len, ==, jArray->len);
+ g_assert_cmpuint(jArray->len, ==, aArray->len);
+ g_assert_cmpuint(aArray->len - 1, ==, hullPointNb * 2);
+
+ glp_load_matrix(lp, aArray->len - 1, &g_array_index(iArray, int, 0),
+ &g_array_index(jArray, int, 0), &g_array_index(aArray, double, 0));
+
+ glp_scale_prob(lp, GLP_SF_AUTO);
+
+ g_array_free(iArray, TRUE);
+ g_array_free(jArray, TRUE);
+ g_array_free(aArray, TRUE);
+
+ return lp;
+}
+
+
+/*
+ * A GFunc for g_queue_foreach(). Add constraints and bounds for one row.
+ *
+ * Args:
+ * data Point*, synchronization point for which to add an LP row
+ * (a constraint)
+ * user_data LPAddRowInfo*
+ */
+static void gfLPAddRow(gpointer data, gpointer user_data)
+{
+ Point* p= data;
+ struct LPAddRowInfo* rowInfo= user_data;
+ int indexes[2];
+ double constraints[2];
+
+ indexes[0]= g_array_index(rowInfo->iArray, int, rowInfo->iArray->len - 1) + 1;
+ indexes[1]= indexes[0];
+
+ if (rowInfo->boundType == GLP_UP)
+ {
+ glp_set_row_bnds(rowInfo->lp, indexes[0], GLP_UP, 0., p->y);
+ }
+ else if (rowInfo->boundType == GLP_LO)
+ {
+ glp_set_row_bnds(rowInfo->lp, indexes[0], GLP_LO, p->y, 0.);
+ }
+ else
+ {
+ g_assert_not_reached();
+ }
+
+ g_array_append_vals(rowInfo->iArray, indexes, 2);
+ indexes[0]= 1;
+ indexes[1]= 2;
+ g_array_append_vals(rowInfo->jArray, indexes, 2);
+ constraints[0]= 1.;
+ constraints[1]= p->x;
+ g_array_append_vals(rowInfo->aArray, constraints, 2);
+}
+
+
+/*
+ * Calculate min or max correction factors (as possible) using an LP problem.
+ *
+ * Args:
+ * lp: A linear programming problem with constraints and bounds
+ * initialized.
+ * direction: The type of factors desired. Use GLP_MAX for max
+ * approximation factors (a1, the drift or slope is the
+ * largest) and GLP_MIN in the other case.
+ *
+ * Returns:
+ * If the calculation was successful, a new Factors struct. Otherwise, NULL.
+ * The calculation will fail if the hull assumptions are not respected.
+ */
+static Factors* calculateFactors(glp_prob* const lp, const int direction)
+{
+ int retval, status;
+ Factors* factors;
+
+ glp_set_obj_coef(lp, 1, 0.);
+ glp_set_obj_coef(lp, 2, 1.);
+
+ glp_set_obj_dir(lp, direction);
+ retval= glp_simplex(lp, NULL);
+ status= glp_get_status(lp);
+
+ if (retval == 0 && status == GLP_OPT)
+ {
+ factors= malloc(sizeof(Factors));
+ factors->offset= glp_get_col_prim(lp, 1);
+ factors->drift= glp_get_col_prim(lp, 2);
+ }
+ else
+ {
+ factors= NULL;
+ }
+
+ return factors;
+}
+
+
+/*
+ * Calculate min, max and approx correction factors (as possible) using an LP
+ * problem.
+ *
+ * Args:
+ * lp: A linear programming problem with constraints and bounds
+ * initialized.
+ *
+ * Returns:
+ * Please note that the approximation type may be MIDDLE, INCOMPLETE or
+ * ABSENT. Unlike in analysis_chull, ABSENT is also used when the hulls do
+ * not respect assumptions.
+ */
+static void calculateCompleteFactors(glp_prob* const lp, FactorsCHull* factors)
+{
+ factors->min= calculateFactors(lp, GLP_MIN);
+ factors->max= calculateFactors(lp, GLP_MAX);
+
+ if (factors->min && factors->max)
+ {
+ factors->type= MIDDLE;
+ calculateFactorsMiddle(factors);
+ }
+ else if (factors->min || factors->max)
+ {
+ factors->type= INCOMPLETE;
+ factors->approx= NULL;
+ }
+ else
+ {
+ factors->type= ABSENT;
+ factors->approx= NULL;
+ }