internal static void PopulateByRow(IMPModeler model, INumVar[][] var, IRange[][] rng) { // First define the variables, three continuous and one integer double[] xlb = { 0.0, 0.0, 0.0, 2.0 }; double[] xub = { 40.0, System.Double.MaxValue, System.Double.MaxValue, 3.0 }; NumVarType[] xt = { NumVarType.Float, NumVarType.Float, NumVarType.Float, NumVarType.Int }; INumVar[] x = model.NumVarArray(4, xlb, xub, xt); var[0] = x; // Objective Function: maximize x0 + 2*x1 + 3*x2 + x3 double[] objvals = { 1.0, 2.0, 3.0, 1.0 }; model.AddMaximize(model.ScalProd(x, objvals)); // Three constraints rng[0] = new IRange[3]; // - x0 + x1 + x2 + 10*x3 <= 20 rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]), model.Prod(1.0, x[1]), model.Prod(1.0, x[2]), model.Prod(10.0, x[3])), 20.0); // x0 - 3*x1 + x2 <= 30 rng[0][1] = model.AddLe(model.Sum(model.Prod(1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod(1.0, x[2])), 30.0); // x1 - 3.5*x3 = 0 rng[0][2] = model.AddEq(model.Sum(model.Prod(1.0, x[1]), model.Prod(-3.5, x[3])), 0.0); }
internal static void PopulateByRow (IMPModeler model, INumVar[][] var, IRange[][] rng) { // First define the variables, three continuous and one integer double[] xlb = {0.0, 0.0, 0.0, 2.0}; double[] xub = {40.0, System.Double.MaxValue, System.Double.MaxValue, 3.0}; NumVarType[] xt = {NumVarType.Float, NumVarType.Float, NumVarType.Float, NumVarType.Int}; INumVar[] x = model.NumVarArray(4, xlb, xub, xt); var[0] = x; // Objective Function: maximize x0 + 2*x1 + 3*x2 + x3 double[] objvals = {1.0, 2.0, 3.0, 1.0}; model.AddMaximize(model.ScalProd(x, objvals)); // Three constraints rng[0] = new IRange[3]; // - x0 + x1 + x2 + 10*x3 <= 20 rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]), model.Prod( 1.0, x[1]), model.Prod( 1.0, x[2]), model.Prod(10.0, x[3])), 20.0); // x0 - 3*x1 + x2 <= 30 rng[0][1] = model.AddLe(model.Sum(model.Prod( 1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod( 1.0, x[2])), 30.0); // x1 - 3.5*x3 = 0 rng[0][2] = model.AddEq(model.Sum(model.Prod( 1.0, x[1]), model.Prod(-3.5, x[3])), 0.0); }
internal static void PopulateByRow(IMPModeler model, INumVar[][] var, IRange[][] rng) { // Define the variables one-by-one INumVar[] x = new INumVar[4]; x[0] = model.NumVar(0.0, 40.0, "x0"); x[1] = model.IntVar(0, System.Int32.MaxValue, "x1"); x[2] = model.IntVar(0, System.Int32.MaxValue, "x2"); x[3] = model.IntVar(2, 3, "x3"); var[0] = x; // Objective Function model.AddMaximize(model.Sum(model.Prod(1.0, x[0]), model.Prod(2.0, x[1]), model.Prod(3.0, x[2]), model.Prod(1.0, x[3]))); // Define three constraints one-by-one rng[0] = new IRange[3]; rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]), model.Prod(1.0, x[1]), model.Prod(1.0, x[2]), model.Prod(10.0, x[3])), 20.0, "rng0"); rng[0][1] = model.AddLe(model.Sum(model.Prod(1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod(1.0, x[2])), 30.0, "rng1"); rng[0][2] = model.AddEq(model.Sum(model.Prod(1.0, x[1]), model.Prod(-3.5, x[3])), 0, "rng2"); // add special ordered set of type 1 INumVar[] sosvars = { x[2], x[3] }; double[] sosweights = { 25.0, 18.0 }; model.AddSOS1(sosvars, sosweights); }
internal static void PopulateByRow (IMPModeler model, INumVar[][] var, IRange[][] rng) { // Define the variables one-by-one INumVar[] x = new INumVar[4]; x[0] = model.NumVar(0.0, 40.0, "x0"); x[1] = model.IntVar(0, System.Int32.MaxValue, "x1"); x[2] = model.IntVar(0, System.Int32.MaxValue, "x2"); x[3] = model.IntVar(2, 3, "x3"); var[0] = x; // Objective Function model.AddMaximize(model.Sum(model.Prod( 1.0, x[0]), model.Prod( 2.0, x[1]), model.Prod( 3.0, x[2]), model.Prod( 1.0, x[3]))); // Define three constraints one-by-one rng[0] = new IRange[3]; rng[0][0] = model.AddLe(model.Sum(model.Prod(-1.0, x[0]), model.Prod( 1.0, x[1]), model.Prod( 1.0, x[2]), model.Prod(10.0, x[3])), 20.0, "rng0"); rng[0][1] = model.AddLe(model.Sum(model.Prod( 1.0, x[0]), model.Prod(-3.0, x[1]), model.Prod( 1.0, x[2])), 30.0, "rng1"); rng[0][2] = model.AddEq(model.Sum(model.Prod( 1.0, x[1]), model.Prod(-3.5, x[3])), 0, "rng2"); // add special ordered set of type 1 INumVar[] sosvars = {x[2], x[3]}; double[] sosweights = {25.0, 18.0}; model.AddSOS1(sosvars, sosweights); }
// Step 4 ***************************************************************************************************** // Step 4 ***************************************************************************************************** internal static void PopulateByRow(IMPModeler model, out IIntVar[][][] var2, out IIntVar[][][][] var3, out IIntVar[][][][][] var4, out IRange[][] rng, CRegion lscrg, CRegion sscrg, double[,] adblTD, string strAreaAggregation) { var aCph = lscrg.GetCphCol().ToArray(); int intCpgCount = lscrg.GetCphCount(); //double dblILPSmallValue = 0.000000001; //double dblILPSmallValue = 0; var x = new IIntVar[intCpgCount][][]; for (int i = 0; i < intCpgCount; i++) { x[i] = new IIntVar[intCpgCount][]; for (int j = 0; j < intCpgCount; j++) { x[i][j] = model.BoolVarArray(intCpgCount); } } //cost in terms of type change var y = Generate4DNumVar(model, intCpgCount - 1, intCpgCount, intCpgCount, intCpgCount); //cost in terms of compactness (length of interior boundaries) var z = Generate4DNumVar(model, intCpgCount - 2, intCpgCount, intCpgCount, intCpgCount); var c = Generate4DNumVar(model, intCpgCount - 2, intCpgCount, intCpgCount, intCpgCount); var3 = new IIntVar[1][][][]; var4 = new IIntVar[3][][][][]; var3[0] = x; var4[0] = y; var4[1] = z; var4[2] = c; //model.AddEq(x[2][0][3], 1.0, "X1"); //model.AddEq(x[2][1][3], 1.0, "X2"); //model.AddEq(x[2][2][2], 1.0, "X3"); //model.AddEq(x[2][3][3], 1.0, "X4"); //add minimizations ILinearNumExpr pTypeCostExpr = model.LinearNumExpr(); //ILinearNumExpr pTypeCostAssitantExpr = model.LinearNumExpr(); for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { for (int k = 0; k < intCpgCount; k++) { for (int l = 0; l < intCpgCount; l++) { double dblCoe = aCph[j].dblArea * adblTD[aCph[k].intTypeIndex, aCph[l].intTypeIndex]; pTypeCostExpr.AddTerm(y[i][j][k][l], dblCoe); //pTypeCostAssitantExpr.AddTerm(y[i][j][k][l], dblILPSmallValueMinimization); } } } } //this is actually for t=1, whose compactness is known double dblCompCostFirstPart = 0; ILinearNumExpr pCompCostSecondPartExpr = model.LinearNumExpr(); var pAdjCorrCphsSD = lscrg.AdjCorrCphsSD; double dblConst = Convert.ToDouble(intCpgCount - 1) / Convert.ToDouble(intCpgCount - 2); for (int i = 0; i < intCpgCount - 2; i++) //i represents indices { double dblNminusT = intCpgCount - i - 2; //double dblTemp = (intCpgCount - i) * dblConst; dblCompCostFirstPart += 1 / dblNminusT; double dblSecondPartDenominator = lscrg.dblInteriorSegLength * dblNminusT * 2; //we don't need to divide the value by 2 because every boundary is only counted once foreach (var pCorrCphs in pAdjCorrCphsSD.Keys) { for (int l = 0; l < intCpgCount; l++) { pCompCostSecondPartExpr.AddTerm(pCorrCphs.dblSharedSegLength / dblSecondPartDenominator, z[i][pCorrCphs.FrCph.ID][pCorrCphs.ToCph.ID][l]); pCompCostSecondPartExpr.AddTerm(pCorrCphs.dblSharedSegLength / dblSecondPartDenominator, z[i][pCorrCphs.ToCph.ID][pCorrCphs.FrCph.ID][l]); } } //var pSecondPartExpr = model.Prod(pCompCostSecondPartExpr, 1 / dblSecondPartDenominator); } if (intCpgCount == 1) { model.AddMinimize(pTypeCostExpr); //we just use an empty expression } else { //Our Model*************************************** var Ftp = model.Prod(pTypeCostExpr, 1 / lscrg.dblArea); var Fcp = model.Prod(dblConst, model.Sum(dblCompCostFirstPart, model.Negative(pCompCostSecondPartExpr))); //model.AddMinimize(model.Prod(model.Sum(Ftp, Fcp), 0.5)); model.AddMinimize(model.Sum( model.Prod(1 - CAreaAgg_Base.dblLamda, Ftp), model.Prod(CAreaAgg_Base.dblLamda, Fcp))); //model.AddMinimize(Fcp); //model.AddMaximize(Fcp); //model.AddObjective() } //for showing slacks var IRangeLt = new List <IRange>(); //a polygon $p$ is assigned to exactly one polygon at a step $t$ for (int i = 0; i < intCpgCount; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { ILinearNumExpr pOneCenterExpr = model.LinearNumExpr(); for (int l = 0; l < intCpgCount; l++) { pOneCenterExpr.AddTerm(x[i][j][l], 1.0); } model.AddEq(pOneCenterExpr, 1.0, "AssignToOnlyOneCenter"); } } //polygon $r$, which is assigned by other polygons, must be a center for (int i = 0; i < intCpgCount; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { for (int l = 0; l < intCpgCount; l++) { model.AddLe(x[i][j][l], x[i][l][l], "AssignedIsCenter__" + i + "__" + j + "__" + l); } } } //only one patch is aggregated into another patch at each step for (int i = 0; i < intCpgCount; i++) //i represents indices { ILinearNumExpr pOneAggregationExpr = model.LinearNumExpr(); for (int j = 0; j < intCpgCount; j++) { pOneAggregationExpr.AddTerm(x[i][j][j], 1.0); } model.AddEq(pOneAggregationExpr, intCpgCount - i, "CountCenters"); } //a center can disappear, but will never reappear afterwards for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { model.AddGe(x[i][j][j], x[i + 1][j][j], "SteadyCenters"); } } //to make sure that the final aggregated polygon has the same color as the target polygon ILinearNumExpr pFinalStateExpr = model.LinearNumExpr(); int intTypeIndexGoal = sscrg.GetSoloCphTypeIndex(); for (int i = 0; i < intCpgCount; i++) { if (aCph[i].intTypeIndex == intTypeIndexGoal) { pFinalStateExpr.AddTerm(x[intCpgCount - 1][i][i], 1.0); } } model.AddEq(pFinalStateExpr, 1.0, "EnsureTarget"); //to restrict *y* for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { for (int k = 0; k < intCpgCount; k++) { //IRangeLt.Add(model.AddLe(model.Sum(y[i][j][k][k], x[i][j][k], x[i + 1][j][k]), 2.0 , "RestrictY")); for (int l = 0; l < intCpgCount; l++) { var LieYRight = model.LinearIntExpr(-1); LieYRight.AddTerm(x[i][j][k], 1); LieYRight.AddTerm(x[i + 1][j][l], 1); model.AddGe(y[i][j][k][l], LieYRight, "RestrictY1"); model.AddLe(y[i][j][k][l], x[i][j][k], "RestrictY2"); model.AddLe(y[i][j][k][l], x[i + 1][j][l], "RestrictY3"); } } } } //to restrict *z* for (int i = 0; i < intCpgCount - 2; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { //for (int k = j; k < intCpgCount; k++) // pay attention for (int k = 0; k < intCpgCount; k++) { for (int l = 0; l < intCpgCount; l++) { var LieZRight = model.LinearIntExpr(-1); LieZRight.AddTerm(x[i + 1][j][l], 1); LieZRight.AddTerm(x[i + 1][k][l], 1); model.AddGe(z[i][j][k][l], LieZRight, "RestrictZ1"); model.AddLe(z[i][j][k][l], x[i + 1][j][l], "RestrictZ2"); model.AddLe(z[i][j][k][l], x[i + 1][k][l], "RestrictZ3"); } } } } //to restrict *c* double dblCpgCountReciprocal = 1 / Convert.ToDouble(intCpgCount); for (int i = 0; i < intCpgCount - 2; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { //for (int k = j; k < intCpgCount; k++) // pay attention for (int k = 0; k < intCpgCount; k++) { for (int l = 0; l < intCpgCount; l++) { if (k == l) { continue; } model.AddLe(c[i][j][k][l], x[i][j][k], "RestrictC1"); var pLieContiguityExpr = model.LinearIntExpr(); //pContiguityExpr.AddTerm(x[i][j][k], 1.0); //including polygon j itself foreach (var pAdjacentCph in aCph[j].AdjacentCphSS) { pLieContiguityExpr.AddTerm(x[i][pAdjacentCph.ID][l], 1); } model.AddLe(c[i][j][k][l], pLieContiguityExpr, "Contiguity"); foreach (var pAdjacentCph in aCph[j].AdjacentCphSS) { var pContiguityExpr2 = model.LinearNumExpr(-1); pContiguityExpr2.AddTerm(x[i][j][k], 1); pContiguityExpr2.AddTerm(x[i][pAdjacentCph.ID][l], 1); model.AddGe(c[i][j][k][l], pContiguityExpr2, "Contiguity2"); } var pContiguityExprRight3 = model.LinearIntExpr(); for (int m = 0; m < intCpgCount; m++) { pContiguityExprRight3.AddTerm(c[i][m][k][l], 1); } model.AddLe(y[i][k][k][l], pContiguityExprRight3, "Contiguity3"); } } } } //If two polygons have been aggregated into one polygon, then they will //be aggregated together in later steps. Our sixth constraint achieve this by requiring for (int i = 0; i < intCpgCount - 3; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { for (int k = 0; k < intCpgCount; k++) { var pAssignTogetherExprPre = model.LinearIntExpr(); var pAssignTogetherExprAfter = model.LinearIntExpr(); for (int l = 0; l < intCpgCount; l++) { pAssignTogetherExprPre.AddTerm(z[i][j][k][l], 1); pAssignTogetherExprAfter.AddTerm(z[i + 1][j][k][l], 1); } model.AddLe(pAssignTogetherExprPre, pAssignTogetherExprAfter, "AssignTogether"); } } } var2 = new IIntVar[1][][]; if (strAreaAggregation == _strSmallest) { IIntVar[][] w = new IIntVar[intCpgCount - 1][]; for (int i = 0; i < intCpgCount - 1; i++) { w[i] = model.BoolVarArray(intCpgCount); } var2[0] = w; //there is only one smallest patch will be involved in each aggregation step for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { var pOneSmallestExpr = model.LinearIntExpr(); for (int j = 0; j < intCpgCount; j++) { pOneSmallestExpr.AddTerm(w[i][j], 1); } model.AddEq(pOneSmallestExpr, 1.0, "OneSmallest"); } //forces that the aggregation must involve the smallest patch. for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { for (int j = 0; j < intCpgCount; j++) { var pInvolveSmallestExpr = model.LinearIntExpr(); for (int k = 0; k < intCpgCount; k++) { if (j == k) //o != r { continue; } pInvolveSmallestExpr.AddTerm(y[i][j][j][k], 1); pInvolveSmallestExpr.AddTerm(y[i][k][k][j], 1); } model.AddLe(w[i][j], pInvolveSmallestExpr, "InvolveSmallest"); } } //To guarantee that patch $o$ is involved in aggregation is indeed the smallest patch double dblM = 1.1 * lscrg.dblArea; //a very large value for (int i = 0; i < intCpgCount - 1; i++) //i represents indices { var aAreaExpr = ComputeAreaExpr(model, x[i], aCph); for (int j = 0; j < intCpgCount; j++) { for (int k = 0; k < intCpgCount; k++) { if (j == k) //o != r { continue; } var pSumExpr = model.Sum(2.0, model.Negative(model.Sum(w[i][j], x[i][k][k]))); //(2-w_{t,o}-x_{t,r,r}) var pProdExpr = model.Prod(pSumExpr, dblM); //M(2-w_{t,o}-x_{t,r,r}) //A_{t,o}-A_{t,r}<= M(2-w_{t,o}-x_{t,r,r}) model.AddLe(model .Sum(aAreaExpr[j], model.Negative(aAreaExpr[k])), pProdExpr, "IndeedSmallest"); } } } } //***************compare with number of constraints counted manually************ rng = new IRange[1][]; rng[0] = new IRange[IRangeLt.Count]; for (int i = 0; i < IRangeLt.Count; i++) { rng[0][i] = IRangeLt[i]; } }