1 #include "Constraint.h" 2 #include "LinearSpec.h" 3 #include "Variable.h" 4 5 #include "lp_lib.h" 6 7 8 /** 9 * Gets the index of the constraint. 10 * 11 * @return the index of the constraint 12 */ 13 int32 14 Constraint::Index() 15 { 16 int32 i = fLS->Constraints()->IndexOf(this); 17 if (i == -1) 18 printf("Constraint not part of fLS->Constraints()."); 19 return i + 1; 20 } 21 22 23 /** 24 * Gets the coefficients of the constraint. 25 * 26 * @return the coefficients of the constraint 27 */ 28 BList* 29 Constraint::Coeffs() 30 { 31 return fCoeffs; 32 } 33 34 35 /** 36 * Gets the variables of the constraint. 37 * 38 * @return the variables of the constraint 39 */ 40 BList* 41 Constraint::Vars() 42 { 43 return fVars; 44 } 45 46 47 /** 48 * Changes the left side of the constraint, i.e. its coefficients and variables. 49 * There must be exactly one coefficient for each variable. 50 * 51 * @param coeffs the new coefficients 52 * @param vars the new variables 53 */ 54 void 55 Constraint::ChangeLeftSide(BList* coeffs, BList* vars) 56 { 57 int32 sizeCoeffs = coeffs->CountItems(); 58 int32 sizeVars = vars->CountItems(); 59 if (sizeCoeffs != sizeVars) 60 printf("Number of coefficients and number of variables in a constraint must be equal."); 61 62 fCoeffs = coeffs; 63 fVars = vars; 64 65 double coeffsArray[sizeCoeffs]; 66 for (int32 i = 0; i < sizeCoeffs; i++) 67 coeffsArray[i] = *(double*)coeffs->ItemAt(i); 68 69 int vIndexes[sizeCoeffs]; 70 for (int32 i = 0; i < sizeVars; i++) 71 vIndexes[i] = ((Variable*)vars->ItemAt(i))->Index(); 72 73 if (!set_rowex(fLS->LP(), this->Index(), sizeCoeffs, &coeffsArray[0], &vIndexes[0])) 74 printf("Error in set_rowex."); 75 76 fLS->RemovePresolved(); 77 } 78 79 80 /** 81 * Gets the operator used for this constraint. 82 * 83 * @return the operator used for this constraint 84 */ 85 OperatorType 86 Constraint::Op() 87 { 88 return fOp; 89 } 90 91 92 /** 93 * Sets the operator used for this constraint. 94 * 95 * @param value operator 96 */ 97 void 98 Constraint::SetOp(OperatorType value) 99 { 100 fOp = value; 101 if (!set_constr_type(fLS->LP(), this->Index(), 102 ((fOp == OperatorType(EQ)) ? EQ 103 : (fOp == OperatorType(GE)) ? GE 104 : LE))) 105 printf("Error in set_constr_type."); 106 107 fLS->RemovePresolved(); 108 } 109 110 111 /** 112 * Gets the constant value that is on the right side of the operator. 113 * 114 * @return the constant value that is on the right side of the operator 115 */ 116 double 117 Constraint::RightSide() 118 { 119 return fRightSide; 120 } 121 122 123 /** 124 * Sets the constant value that is on the right side of the operator. 125 * 126 * @param value constant value that is on the right side of the operator 127 */ 128 void 129 Constraint::SetRightSide(double value) 130 { 131 fRightSide = value; 132 if (!set_rh(fLS->LP(), Index(), fRightSide)) 133 printf("Error in set_rh."); 134 135 fLS->RemovePresolved(); 136 } 137 138 139 // Needs to be fixed. Use BString. Substring? 140 //~ string Constraint::ToString() { 141 //~ string s = ""; 142 //~ for (int32 i = 0; i < fVars->CountItems(); i++) 143 //~ s += (double)fCoeffs->ItemAt(i) + "*" + (Variable*)fVars->ItemAt(i).ToString() + " + "; 144 //~ s = s.Substring(0, s.Length - 2); 145 //~ if (Op == OperatorType.EQ) s += "= "; 146 //~ else if (Op == OperatorType.GE) s += ">= "; 147 //~ else s += "<= "; 148 //~ s += rightSide.ToString(); 149 //~ return s; 150 //~ } 151 152 153 /** 154 * Destructor. 155 * Removes the constraint from its specification. 156 */ 157 Constraint::~Constraint() 158 { 159 del_constraint(fLS->LP(), Index()); 160 delete fCoeffs; 161 delete fVars; 162 fLS->Constraints()->RemoveItem(this); 163 } 164 165 166 /** 167 * Default constructor. 168 */ 169 Constraint::Constraint() {} 170 171 172 /** 173 * Constructor. 174 */ 175 Constraint::Constraint(LinearSpec* ls, BList* coeffs, BList* vars, OperatorType op, 176 double rightSide) 177 { 178 179 int32 sizeCoeffs = coeffs->CountItems(); 180 int32 sizeVars = vars->CountItems(); 181 if (sizeCoeffs != sizeVars) 182 printf("Number of coefficients and number of variables in a constraint must be equal."); 183 184 fLS = ls; 185 fCoeffs = coeffs; 186 fVars = vars; 187 fOp = op; 188 fRightSide = rightSide; 189 190 double coeffsArray[sizeCoeffs]; 191 for (int32 i = 0; i < sizeCoeffs; i++) 192 coeffsArray[i] = *(double*)coeffs->ItemAt(i); 193 194 int vIndexes[sizeCoeffs]; 195 for (int32 i = 0; i < sizeVars; i++) 196 vIndexes[i] = ((Variable*)vars->ItemAt(i))->Index(); 197 198 if (!add_constraintex(ls->LP(), sizeCoeffs, &coeffsArray[0], &vIndexes[0], 199 ((op == OperatorType(EQ)) ? EQ 200 : (op == OperatorType(GE)) ? GE 201 : LE), rightSide)) 202 printf("Error in add_constraintex."); 203 204 ls->Constraints()->AddItem(this); 205 } 206 207