How to use the mip.write function in mip

To help you get started, we’ve selected a few mip examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github discreteoptimization / setcover / lns_mip_002 / lns.py View on Github external
for var in vars:
        if best_sol[var] == 0 and random.random() < FIX_RATIO:
          added_constraints.append(g_model.addConstr(var == best_sol[var]))

      g_model.optimize()

      # Remove the additional constraints again
      for constraint in added_constraints:
        g_model.remove(constraint)

      #print g_model.status
      if g_model.status != grb.GRB.INFEASIBLE:
        FEASIBLE_COUNT += 1;
        if g_model.objval < best_obj:
          sys.stdout.write("\nNext solution:    {0}\n".format(g_model.objval))
          m.write(model, False)

          best_obj = g_model.objval
          best_sol = {var:int(var.x) for var in vars}

          g_model.remove(obj_constraint)
          obj_constraint = g_model.addConstr(g_model.getObjective() <= best_obj - 1)

          FEASIBLE_COUNT = 0
          INFEASIBLE_COUNT = 0
        else:
          sys.stdout.write('_')
      else:
        INFEASIBLE_COUNT += 1;
        sys.stdout.write('.')
        sys.stdout.flush()
github discreteoptimization / setcover / lns_mip_001 / lns.py View on Github external
added_constraints = []
      for var in vars:
        if best_sol[var] == 0 and random.random() < FIX_RATIO:
          added_constraints.append(g_model.addConstr(var == 0))

      g_model.optimize()

      # Remove the additional constraints again
      for constraint in added_constraints:
        g_model.remove(constraint)

      #print g_model.status
      if g_model.status != grb.GRB.INFEASIBLE:
        if g_model.objval < best_obj:
          print("\nNext solution:    {0}".format(g_model.objval))
          m.write(model, False)

          best_obj = g_model.objval
          best_sol = {var:int(var.x) for var in vars}

          g_model.remove(obj_constraint)
          obj_constraint = g_model.addConstr(g_model.getObjective() <= best_obj - 1)
        else:
          print "(ERROR)"
      else:
        sys.stdout.write('.')
        sys.stdout.flush()


  g_model.setParam("TimeLimit", float('inf'))
  g_model.optimize()
  if g_model.status == grb.GRB.OPTIMAL:
github discreteoptimization / setcover / lns_mip_002 / lns.py View on Github external
SAMPLE_SIZE = 30
  FEASIBLE_COUNT = 0
  INFEASIBLE_COUNT = 0


  g_model, vars = model
  g_model.setParam("OutputFlag", 0)
  g_model.setParam("TimeLimit", len(vars)*0.05)

  print("Processing " + g_model.getAttr("ModelName"))

  # Warmup
  g_model.optimize()
  print("Initial solution: {0}".format(g_model.objval))
  m.write(model)
  #g_model.setParam("SolutionLimit", 2147483647)
  g_model.setParam("TimeLimit", TIMELIMIT)
  
  best_obj = g_model.objval
  best_sol = {var:int(var.x) for var in vars}
  obj_constraint = g_model.addConstr(g_model.getObjective() <= best_obj - 1)
  
  if g_model.status != grb.GRB.OPTIMAL:
    while g_model.status != grb.GRB.INTERRUPTED:
      # Add the additional constraints as described above
      added_constraints = []
      for var in vars:
        if best_sol[var] == 0 and random.random() < FIX_RATIO:
          added_constraints.append(g_model.addConstr(var == best_sol[var]))

      g_model.optimize()
github discreteoptimization / setcover / lns_mip_001 / lns.py View on Github external
the sets with the highest cost/size ratio?)

  Args:
    model: The set cover MIP model as created by mip.create_model().
  """
  g_model, vars = model
  g_model.setParam("OutputFlag", 0)
  g_model.setParam("TimeLimit", TIMELIMIT)
  g_model.setParam("SolutionLimit", 1)

  print("Processing " + g_model.getAttr("ModelName"))

  # Warmup
  g_model.optimize()
  print("Initial solution: {0}".format(g_model.objval))
  m.write(model)
  g_model.setParam("SolutionLimit", 2147483647)
  
  best_obj = g_model.objval
  best_sol = {var:int(var.x) for var in vars}
  obj_constraint = g_model.addConstr(g_model.getObjective() <= best_obj - 1)
  
  if g_model.status != grb.GRB.OPTIMAL:
    while g_model.status != grb.GRB.INTERRUPTED:
      # Add the additional constraints as described above
      added_constraints = []
      for var in vars:
        if best_sol[var] == 0 and random.random() < FIX_RATIO:
          added_constraints.append(g_model.addConstr(var == 0))

      g_model.optimize()
github discreteoptimization / setcover / lns_mip_001 / lns.py View on Github external
best_sol = {var:int(var.x) for var in vars}

          g_model.remove(obj_constraint)
          obj_constraint = g_model.addConstr(g_model.getObjective() <= best_obj - 1)
        else:
          print "(ERROR)"
      else:
        sys.stdout.write('.')
        sys.stdout.flush()


  g_model.setParam("TimeLimit", float('inf'))
  g_model.optimize()
  if g_model.status == grb.GRB.OPTIMAL:
    print("Optimal solution: {0}".format(g_model.objval))
  m.write(model)