aboutsummaryrefslogtreecommitdiffstats
path: root/native/src/linprog/io.cpp
blob: 801e833ca03fa0bf36b02fe7b785aeb3d1f68aa6 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>

#include "linprog/solver.h"
#include "linprog/io.h"

std::ostream& operator<<(std::ostream &os, const LinearExpression &exp)
{
	bool first = true;
	foreach (exp.get_terms(), term)
	{
		if (term->first < 0)
			os << "- " << -term->first;
		else if (!first)
			os << "+ " << term->first;
		else
			os << term->first;

		os <<  " X" << term->second << " ";
		first = false;
	}

	return os;
}

std::ostream& operator<<(std::ostream &os, const LinearProgram &lp)
{
	os << "maximize " << *lp.get_objective() << " subject to:" << std::endl;
	foreach (lp.get_equalities(), it)
	{
		os << *(it->first) << " = " << it->second << std::endl;
	}
	foreach (lp.get_inequalities(), it)
	{
		os << *(it->first) << " <= " << it->second << std::endl;
	}

	return os;
}

void dump_lp_solution(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const TaskInfo& ti,
	const Solution& solution,
	std::ostream& out,
	bool show_zeros)
{
	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();

		out << "T" << t << " part=" << tx->get_cluster() << std::endl;

		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();

			out << "  res=" << q
			    << "  L=" << request->get_request_length()
			    << std::endl;

			foreach_request_instance(*request, ti, v)
			{
				unsigned int var_id;
				bool newline = false;

				var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
				if (solution.get_value(var_id) || show_zeros)
				{
					out << "    XD_" << t  << "_" << q << "_" << v
					    << "=" << solution.get_value(var_id);
					newline = true;
				}

				var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
				if (solution.get_value(var_id) || show_zeros)
				{
					out << "    XI_" << t  << "_" << q << "_" << v
					    << "=" << solution.get_value(var_id);
					newline = true;
				}


				var_id = vars.lookup(t, q, v, BLOCKING_PREEMPT);
				if (solution.get_value(var_id) || show_zeros)
				{
					out << "    XP_" << t  << "_" << q << "_" << v
					    << "=" << solution.get_value(var_id);
					newline = true;
				}

				if (newline)
					out << std::endl;
			}
		}
	}
}