xref: /haiku/src/tests/libs/linprog/Program.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3  * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include <List.h>
8 #include <SupportDefs.h>
9 
10 #include "LinearSpec.h"
11 
12 #include <stdio.h>
13 
14 
15 using namespace LinearProgramming;
16 
17 
18 void
19 PrintResults(const VariableList& list)
20 {
21 	for (int32 i = 0; i < list.CountItems(); i++)
22 		printf("Variable %i = %f\n", (int)i, list.ItemAt(i)->Value());
23 }
24 
25 
26 void
27 Test1()
28 {
29 	LinearSpec ls;
30 	Variable* x1 = ls.AddVariable();
31 	Variable* x2 = ls.AddVariable();
32 
33 	Constraint* c1 = ls.AddConstraint(1.0, x1, OperatorType(kLE), 108);
34 	Constraint* c2 = ls.AddConstraint(1.0, x2, OperatorType(kGE), 113);
35 
36 	ls.Solve();
37 	printf("ls: %s\n", ls.ToString().String());
38 
39 	ls.RemoveConstraint(c2);
40 	ls.Solve();
41 	printf("ls: %s\n", ls.ToString().String());
42 
43 	c2 = ls.AddConstraint(1.0, x2, OperatorType(kGE), 113);
44 	ls.Solve();
45 	printf("ls: %s\n", ls.ToString().String());
46 }
47 
48 
49 void
50 SoftConstraints()
51 {
52 	printf("SoftConstraints\n\n");
53 
54 	LinearSpec ls;
55 	Variable* x1 = ls.AddVariable();
56 	x1->SetLabel("label_x1");
57 	Variable* x2 = ls.AddVariable();
58 	x2->SetLabel("label_x2");
59 	Variable* x3 = ls.AddVariable();
60 	x3->SetLabel("label_x3");
61 
62 	Constraint* c1 = ls.AddConstraint(1, x1, kEQ, 0);
63 	Constraint* c2 = ls.AddConstraint(1, x1, -1, x2, kLE, 0);
64 	Constraint* c3 = ls.AddConstraint(1, x2, -1, x3, kLE, 0);
65 	Constraint* c4 = ls.AddConstraint(1, x3, -1, x1, kEQ, 20);
66 
67 	Constraint* c5 = ls.AddConstraint(1, x2, -1, x1, kEQ, 10, 5, 5);
68 	Constraint* c6 = ls.AddConstraint(1, x3, -1, x2, kEQ, 5, 5, 5);
69 
70 	ls.Solve();
71 	printf("ls: %s\n", ls.ToString().String());
72 	PrintResults(ls.AllVariables());
73 
74 	ls.RemoveConstraint(c6);
75 	ls.Solve();
76 	printf("ls: %s\n", ls.ToString().String());
77 	PrintResults(ls.UsedVariables());
78 }
79 
80 
81 void
82 SoftInequalityConstraints()
83 {
84 	printf("SoftInequalityConstraints\n\n");
85 
86 	LinearSpec ls;
87 	Variable* x1 = ls.AddVariable();
88 	x1->SetLabel("label_x1");
89 	Variable* x2 = ls.AddVariable();
90 	x2->SetLabel("label_x2");
91 	Variable* x3 = ls.AddVariable();
92 	x3->SetLabel("label_x3");
93 
94 	Constraint* c1 = ls.AddConstraint(1, x1, kEQ, 0);
95 	Constraint* c2 = ls.AddConstraint(1, x1, -1, x2, kLE, 0);
96 	Constraint* c3 = ls.AddConstraint(1, x2, -1, x3, kLE, 0);
97 	Constraint* c4 = ls.AddConstraint(1, x3, -1, x1, kEQ, 20);
98 
99 	Constraint* c5 = ls.AddConstraint(1, x2, -1, x1, kEQ, 15, 5, 5);
100 	Constraint* c6 = ls.AddConstraint(1, x3, -1, x2, kGE, 10, 5000, 5);
101 
102 	ls.Solve();
103 	printf("ls: %s\n", ls.ToString().String());
104 	PrintResults(ls.AllVariables());
105 
106 	ls.RemoveConstraint(c6);
107 	ls.Solve();
108 	printf("ls: %s\n", ls.ToString().String());
109 	PrintResults(ls.UsedVariables());
110 }
111 
112 
113 int
114 main()
115 {
116 	Test1();
117 	SoftConstraints();
118 	SoftInequalityConstraints();
119 }
120 
121