xref: /haiku/src/tests/libs/linprog/Program.cpp (revision 0562493379cd52eb7103531f895f10bb8e77c085)
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 void
16 PrintVars(LinearSpec* ls)
17 {
18 	int32 size = ls->Variables()->CountItems();
19 	Variable* variable;
20 	for (int i = 0; i < size; i++) {
21 		variable = (Variable*)ls->Variables()->ItemAt(i);
22 		printf("%f ", variable->Value());
23 	}
24 	printf("%d\n", ls->Result());
25 }
26 
27 
28 void
29 Test1()
30 {
31 	LinearSpec* ls = new LinearSpec();
32 	Variable* x1 = ls->AddVariable();
33 	Variable* x2 = ls->AddVariable();
34 
35 	Constraint* c1 = ls->AddConstraint(1.0, x1, OperatorType(LE), 108);
36 	Constraint* c2 = ls->AddConstraint(1.0, x2, OperatorType(GE), 113);
37 
38 	BList* summands = new BList(2);
39 	summands->AddItem(new Summand(1.0, x1));
40 	summands->AddItem(new Summand(1.0, x2));
41 	ls->SetObjFunction(summands);
42 
43 	ls->Solve();
44 	PrintVars(ls);
45 
46 	delete c2;
47 	ls->Solve();
48 	PrintVars(ls);
49 
50 	c2 = ls->AddConstraint(1.0, x2, OperatorType(GE), 113);
51 	ls->Solve();
52 	PrintVars(ls);
53 }
54 
55 
56 int
57 main()
58 {
59 	Test1();
60 }
61 
62