// Modifying all quadratic terms x[i]*x[j] // in the objective function. internal static void ModifyQuadObjective(CplexModeler model) { IEnumerator matrixEnum = model.GetLPMatrixEnumerator(); matrixEnum.MoveNext(); ILPMatrix lp = (ILPMatrix)matrixEnum.Current; INumVar[] x = lp.NumVars; int ncols = x.Length; IObjective obj = model.GetObjective(); // Note that the quadratic expression in the objective // is normalized: i.e., for all i != j, terms // c(i,j)*x[i]*x[j] + c(j,i)*x[j]*x[i] are normalized as // (c(i,j) + c(j,i)) * x[i]*x[j], or // (c(i,j) + c(j,i)) * x[j]*x[i]. // Therefore you can only modify one of the terms // x[i]*x[j] or x[j]*x[i]. // If you modify both x[i]*x[j] and x[j]*x[i], then // the second modification will overwrite the first one. for (int i = 0; i < ncols; ++i) { model.SetQuadCoef(obj, x[i], x[i], i * i); for (int j = 0; j < i; ++j) { model.SetQuadCoef(obj, x[i], x[j], -2.0 * (i * j)); } } // Print out the objective function PrintObjective(obj); }
public static void Main(string[] args) { try { // setup files to transfer model to server string mfile = "Model.dat"; string sfile = "Solution.dat"; // build model INumVar[][] var = new INumVar[1][]; IRange[][] rng = new IRange[1][]; CplexModeler model = new CplexModeler(); PopulateByRow(model, var, rng); FileStream mstream = new FileStream(mfile, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(mstream, new ModelData(model, var[0])); mstream.Close(); // start server Server server = new Server(mfile, sfile); SolutionData sol = null; FileStream sstream = new FileStream(sfile, FileMode.Open); sol = (SolutionData)formatter.Deserialize(sstream); sstream.Close(); System.Console.WriteLine("Solution status = " + sol.status); if (sol.status.Equals(Cplex.CplexStatus.Optimal)) { System.Console.WriteLine("Solution value = " + sol.obj); int ncols = var[0].Length; for (int j = 0; j < ncols; ++j) { System.Console.WriteLine("Variable " + j + ": Value = " + sol.vals[j]); } } } catch (ILOG.Concert.Exception e) { System.Console.WriteLine("Concert exception '" + e + "' caught"); } catch (System.Exception t) { System.Console.WriteLine("terminating due to exception " + t); } }
public static void Main(string[] args) { try { // setup files to transfer model to server string mfile = "Model.dat"; string sfile = "Solution.dat"; // build model INumVar[][] var = new INumVar[1][]; IRange[][] rng = new IRange[1][]; CplexModeler model = new CplexModeler(); PopulateByRow(model, var, rng); FileStream mstream = new FileStream(mfile, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter (); formatter.Serialize(mstream, new ModelData(model, var[0])); mstream.Close(); // start server Server server = new Server(mfile, sfile); SolutionData sol = null; FileStream sstream = new FileStream(sfile, FileMode.Open); sol = (SolutionData) formatter.Deserialize(sstream); sstream.Close(); System.Console.WriteLine("Solution status = " + sol.status); if ( sol.status.Equals(Cplex.CplexStatus.Optimal) ) { System.Console.WriteLine("Solution value = " + sol.obj); int ncols = var[0].Length; for (int j = 0; j < ncols; ++j) System.Console.WriteLine("Variable " + j + ": Value = " + sol.vals[j]); } } catch (ILOG.Concert.Exception e) { System.Console.WriteLine("Concert exception '" + e + "' caught"); } catch (System.Exception t) { System.Console.WriteLine("terminating due to exception " + t); } }
// Modifying all quadratic terms x[i]*x[j] // in the objective function. internal static void ModifyQuadObjective(CplexModeler model) { IEnumerator matrixEnum = model.GetLPMatrixEnumerator(); matrixEnum.MoveNext(); ILPMatrix lp = (ILPMatrix)matrixEnum.Current; INumVar[] x = lp.NumVars; int ncols = x.Length; IObjective obj = model.GetObjective(); // Note that the quadratic expression in the objective // is normalized: i.e., for all i != j, terms // c(i,j)*x[i]*x[j] + c(j,i)*x[j]*x[i] are normalized as // (c(i,j) + c(j,i)) * x[i]*x[j], or // (c(i,j) + c(j,i)) * x[j]*x[i]. // Therefore you can only modify one of the terms // x[i]*x[j] or x[j]*x[i]. // If you modify both x[i]*x[j] and x[j]*x[i], then // the second modification will overwrite the first one. for (int i = 0; i < ncols; ++i) { model.SetQuadCoef(obj, x[i], x[i], i*i); for (int j = 0; j < i; ++j) model.SetQuadCoef(obj, x[i], x[j], -2.0*(i*j)); } // Print out the objective function PrintObjective(obj); }