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