genconstr_vb.vb


genconstr_vb.vb


“Copyright 2021, G狗万app足彩urobi Optimization, LLC”在这个例子中,我们展示了对一些常见表达式建模的一般约束的使用。我们使用作为一个例子一个sat问题,我们想是否可以满足至少四(或者全部)条款的逻辑的“L = (x0或~ x1和x2)和(x1 ~ x2或x3)和(x2或~ x3或x0)和(x3 ~ x0或x1)和' (~ x0或~ x1和x2)和(~ ~ x1和x2或x3)和“x2 (~ ~ x3或x0)和(~ x3 ~ x0或x1)“我们通过引入两个变量对于每个文字(本身及其“否定价值),一个变量对于每一个条款,然后两个“变量指示如果我们能满足四,和另一个确定的最低条款(如果它,我们可以满足所有条款),然后把这两个变量放到目标中。例如,目标函数将是“最大化Obj0 + Obj1”“Obj0 = MIN(第1条,…Clause8)”Obj1 = 1 -> Clause1 +…+第8条>= 4 ' '因此,客观值将是两个当且仅当,我们可以满足所有的'子句;至少四个子句中的一个“如果”和“仅当”可以满足,“否则为零”。import Gurobi Class genconstruc_vb Public Const NLITERALS As Integer = 4 and n Public Const nclause As Integer = 8 Public Const NOBJ As Integer = 2 ' e.g. {0, n+1, 2} means clause (x0 or ~x1 or x2) Dim Clauses As Integer(,) = New Integer(,) { _ { 0, n + 1, 2}, { 1, n + 2, 3}, _ { 2, n + 3, 0}, { 3, n + 0, 1}, _ {n + 0, n + 1, 2}, {n + 1, n + 2, 3}, _ {n + 2, n + 3, 0}, {n + 3, n + 0, 1}} Dim i As Integer, status As Integer ' Create environment Dim env As New GRBEnv("genconstr_vb.log") ' Create initial model Dim model As New GRBModel(env) model.ModelName = "genconstr_vb" ' Initialize decision variables and objective Dim Lit As GRBVar() = New GRBVar(NLITERALS - 1) {} Dim NotLit As GRBVar() = New GRBVar(NLITERALS - 1) {} For i = 0 To NLITERALS - 1 Lit(i) = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, String.Format("X{0}", i)) NotLit(i) = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, String.Format("notX{0}", i)) Next Dim Cla As GRBVar() = New GRBVar(NCLAUSES - 1) {} For i = 0 To NCLAUSES - 1 Cla(i) = model.AddVar(0.0, 1.0, 0.0, GRB.BINARY, String.Format("Clause{0}", i)) Next Dim Obj As GRBVar() = New GRBVar(NOBJ - 1) {} For i = 0 To NOBJ - 1 Obj(i) = model.AddVar(0.0, 1.0, 1.0, GRB.BINARY, String.Format("Obj{0}", i)) Next ' Link Xi and notXi Dim lhs As GRBLinExpr For i = 0 To NLITERALS - 1 lhs = New GRBLinExpr() lhs.AddTerm(1.0, Lit(i)) lhs.AddTerm(1.0, NotLit(i)) model.AddConstr(lhs, GRB.EQUAL, 1.0, String.Format("CNSTR_X{0}", i)) Next ' Link clauses and literals For i = 0 To NCLAUSES - 1 Dim clause As GRBVar() = New GRBVar(2) {} For j As Integer = 0 To 2 If Clauses(i, j) >= n Then clause(j) = NotLit(Clauses(i, j) - n) Else clause(j) = Lit(Clauses(i, j)) End If Next model.AddGenConstrOr(Cla(i), clause, String.Format("CNSTR_Clause{0}", i)) Next ' Link objs with clauses model.AddGenConstrMin(Obj(0), Cla, GRB.INFINITY, "CNSTR_Obj0") lhs = New GRBLinExpr() For i = 0 To NCLAUSES - 1 lhs.AddTerm(1.0, Cla(i)) Next model.AddGenConstrIndicator(Obj(1), 1, lhs, GRB.GREATER_EQUAL, 4.0, "CNSTR_Obj1") ' Set global objective sense model.ModelSense = GRB.MAXIMIZE ' Save problem model.Write("genconstr_vb.mps") model.Write("genconstr_vb.lp") ' Optimize model.Optimize() ' Status checking status = model.Status If status = GRB.Status.INF_OR_UNBD OrElse _ status = GRB.Status.INFEASIBLE OrElse _ status = GRB.Status.UNBOUNDED Then Console.WriteLine("The model cannot be solved " & _ "because it is infeasible or unbounded") Return End If If status <> GRB.Status.OPTIMAL Then Console.WriteLine("Optimization was stopped with status {0}", status) Return End If ' Print result Dim objval As Double = model.ObjVal If objval > 1.9 Then Console.WriteLine("Logical expression is satisfiable") ElseIf objval > 0.9 Then Console.WriteLine("At least four clauses can be satisfied") Else Console.WriteLine("Not even three clauses can be satisfied") End If ' Dispose of model and environment model.Dispose() env.Dispose() Catch e As GRBException Console.WriteLine("Error code: {0}. {1}", e.ErrorCode, e.Message) End Try End Sub End Class