Example #1
0
def optimizeconcurrentMIO(task, seeds):
  n = len(seeds)
  tasks = [ mosek.Task(task) for _ in range(n) ]  
  
  # Choose various seeds for cloned tasks
  for i in range(n):
    tasks[i].putintparam(mosek.iparam.mio_seed, seeds[i])

  # Solve tasks in parallel
  firstOK, res, trm = optimize(tasks)

  if firstOK >= 0:
    # Pick the task that ended with res = ok
    # and contains an integer solution with best objective value
    sense = task.getobjsense();
    bestObj = 1.0e+10 if sense == mosek.objsense.minimize else -1.0e+10
    bestPos = -1

    for i in range(n):
      print("{0}   {1}".format(i,tasks[i].getprimalobj(mosek.soltype.itg)))

    for i in range(n):
      if ((res[i] == mosek.rescode.ok) and
          (tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.prim_feas or
           tasks[i].getsolsta(mosek.soltype.itg) == mosek.solsta.integer_optimal) and
         ((tasks[i].getprimalobj(mosek.soltype.itg) < bestObj) 
           if (sense == mosek.objsense.minimize) else 
          (tasks[i].getprimalobj(mosek.soltype.itg) > bestObj))):
        bestObj = tasks[i].getprimalobj(mosek.soltype.itg)
        bestPos = i

    if bestPos >= 0:
      return bestPos, tasks[bestPos], trm[bestPos], res[bestPos]

  return -1, None, None, None
Example #2
0
def main(args):
    # To run a continuous problem example
    filename = "../data/25fv47.mps"
    slvr = "intpnt"

    # To run a mixed-integer example
    #filename = "../data/milo1.lp"
    #slvr     = ""

    if len(args) < 3:
        print("Usage: callback ( psim | dsim | intpnt ) filename")

    if len(args) > 1:
        slvr = args[1]
    if len(args) > 2:
        filename = args[2]

    with mosek.Env() as env:
        with mosek.Task(env) as task:
            task.readdata(filename)

            if slvr == 'psim':
                task.putintparam(iparam.optimizer,
                                 optimizertype.primal_simplex)
            elif slvr == "dsim":
                task.putintparam(iparam.optimizer, optimizertype.dual_simplex)
            elif slvr == "intpnt":
                task.putintparam(iparam.optimizer, optimizertype.intpnt)

            usercallback = makeUserCallback(maxtime=0.05, task=task)
            task.set_InfoCallback(usercallback)

            task.optimize()
Example #3
0
def main(args):

    if len(args) < 3:
        print("Too few input arguments. Syntax:")
        print("\tcallback.py psim inputfile")
        print("\tcallback.py dsim inputfile")
        print("\tcallback.py intpnt inputfile")
        return

    with mosek.Env() as env:
        with mosek.Task(env) as task:
            filename = args[2]
            task.readdata(filename)

            task.set_Stream(streamtype.log, msgPrinter)

            if args[1] == 'psim':
                task.putintparam(iparam.optimizer,
                                 optimizertype.primal_simplex)
            elif args[1] == "dsim":
                task.putintparam(iparam.optimizer, optimizertype.dual_simplex)
            elif args[1] == "intpnt":
                task.putintparam(iparam.optimizer, optimizertype.intpnt)

            # Turn all MOSEK logging off (note that errors and other messages
            # are still sent through the log stream)
            task.putintparam(iparam.log, 0)

            usercallback = makeUserCallback(maxtime=3600)
            task.set_Progress(usercallback)

            task.optimize()

            task.solutionsummary(streamtype.msg)
Example #4
0
def main():
    numcon = 2
    numvar = 2

    aval = [[-1.0], [1.0, 1.0]]
    asub = [[1], [0, 1]]

    ptrb = [0, 1]
    ptre = [1, 3]

    #int[]       bsub  = new int[numvar];
    #double[]    b     = new double[numvar];
    #int[]       basis = new int[numvar];

    with mosek.Env() as env:
        with mosek.Task(env) as task:
            # Directs the log task stream to the user specified
            # method task_msg_obj.streamCB
            task.set_Stream(mosek.streamtype.log,
                            lambda msg: sys.stdout.write(msg))
            # Put A matrix and factor A.
            # Call this function only once for a given task.

            basis = [0] * numvar
            b = [0.0, -2.0]
            bsub = [0, 1]

            put_a(task, aval, asub, ptrb, ptre, numvar, basis)

            # now solve rhs
            b = [1, -2]
            bsub = [0, 1]
            nz = task.solvewithbasis(0, 2, bsub, b)
            print("\nSolution to Bx = b:\n")

            # Print solution and show correspondents
            # to original variables in the problem
            for i in range(nz):
                if basis[bsub[i]] < numcon:
                    print("This should never happen")
                else:
                    print("x%d = %d" % (basis[bsub[i]] - numcon, b[bsub[i]]))

            b[0] = 7
            bsub[0] = 0

            nz = task.solvewithbasis(0, 1, bsub, b)

            print("\nSolution to Bx = b:\n")
            # Print solution and show correspondents
            # to original variables in the problem
            for i in range(nz):
                if basis[bsub[i]] < numcon:
                    print("This should never happen")
                else:
                    print("x%d = %d" % (basis[bsub[i]] - numcon, b[bsub[i]]))
Example #5
0
def optimizeconcurrent(task, optimizers):
  n = len(optimizers)
  tasks = [ mosek.Task(task) for _ in range(n) ]

  # Choose various optimizers for cloned tasks
  for i in range(n):
    tasks[i].putintparam(mosek.iparam.optimizer, optimizers[i])

  # Solve tasks in parallel
  firstOK, res, trm = optimize(tasks)

  if firstOK >= 0:
    return firstOK, tasks[firstOK], trm[firstOK], res[firstOK]
  else:
    return -1, None, None, None
Example #6
0
def main(argv):
    n = len(argv) - 1
    tasks = []

    with mosek.Env() as env:
        for i in range(n):
            t = mosek.Task(env, 0, 0)
            t.readdata(argv[i + 1])
            # Each task will be single-threaded
            t.putintparam(mosek.iparam.intpnt_multi_thread, mosek.onoffkey.off)
            tasks.append(t)

        res, trm = paropt(tasks)

        for i in range(n):
            print(
                "Task  {0}  res {1}   trm {2}   obj_val  {3}  time {4}".format(
                    i, res[i], trm[i],
                    tasks[i].getdouinf(mosek.dinfitem.intpnt_primal_obj),
                    tasks[i].getdouinf(mosek.dinfitem.optimizer_time)))
Example #7
0
def main(argv):
  with mosek.Env() as env:
    with mosek.Task(env, 0, 0) as task:
      if len(argv) >= 2:
        task.readdata(argv[1])
      else:
        task.readdata("../data/25fv47.mps")

      # Optional time limit
      if len(argv) >= 3:
        task.putdouparam(mosek.dparam.optimizer_max_time, float(argv[2]))

      if (task.getnumintvar() == 0):
        # If the problem is continuous
        # optimize it with three continuous optimizers.
        # (Simplex will fail for non-linear problems)
        optimizers = [
          mosek.optimizertype.conic,
          mosek.optimizertype.dual_simplex,
          mosek.optimizertype.primal_simplex
        ]

        idx, t, trm, res = optimizeconcurrent(task, optimizers)
      else:
        # Mixed-integer problem.
        # Try various seeds.
        seeds = [ 42, 13, 71749373 ]

        idx, t, trm, res = optimizeconcurrentMIO(task, seeds)

      # Check results and print the best answer
      if idx >= 0:
        print("Result from optimizer with index {0}:  res {1}  trm {2}".format(idx, res, trm))
        t.set_Stream(mosek.streamtype.log, streamwriter)
        t.optimizersummary(mosek.streamtype.log)
        t.solutionsummary(mosek.streamtype.log);
      else:
        print("All optimizers failed.")
Example #8
0
    def __init__(self, dat, dimPCA, presolveTol=1.0e-30, outputFlag=False):
        self.numPoints = dat.shape[0]
        self.numFields = dat.shape[1]
        self.dimPCA = dimPCA
        self.dat = (np.eye(self.numPoints) - np.ones(
            (self.numPoints, self.numPoints)) / self.numPoints).dot(
                dat)  #dat - np.ones((self.numPoints,1)).dot(sum(dat)[None,:])
        for col, var in enumerate(np.var(self.dat, axis=0)):
            self.dat[:, col] *= 1 / (math.sqrt(var) if var > 0 else 1)
        self.normalizer = la.norm(self.dat)
        self.B = np.zeros((self.numFields, self.dimPCA))
        self.RunTime = 0
        self.normConstr = 0
        self.cons = []
        self.symmats = []

        # Make mosek environment
        with mosek.Env() as env:

            # Create a task object and attach log stream printer
            with env.Task(0, 0) as task:
                task.putdouparam(mosek.dparam.presolve_tol_x, presolveTol)
                # options for convexity check are none, simple, full
                task.putintparam(mosek.iparam.check_convexity,
                                 mosek.checkconvexitytype.none)
                task.putintparam(mosek.iparam.infeas_report_auto,
                                 mosek.onoffkey.on)
                task.putintparam(mosek.iparam.infeas_report_level, 40)
                #task.putdouparam(mosek.dparam.intpnt_co_tol_infeas,1e-5)
                if outputFlag:
                    task.set_Stream(mosek.streamtype.msg, streamprinter)

                # Bound keys for constraints, vars
                barvardim = [self.numFields, self.numFields]
                numvar = 0
                numcon = 1 + int((self.numFields + 1) * self.numFields / 2)
                idx, jdx = np.tril_indices(self.numFields)

                bkc = [mosek.boundkey.fx] + [mosek.boundkey.fx] * (numcon - 1)
                blc = [self.dimPCA] + list((idx == jdx).astype(int))
                buc = [self.dimPCA] + list((idx == jdx).astype(int))

                task.appendvars(numvar)
                task.appendcons(numcon)
                task.putconboundslice(0, numcon, bkc, blc, buc)
                task.appendbarvars(barvardim)

                task.putbarcj(0, [
                    task.appendsparsesymmat(
                        self.numFields, idx, jdx,
                        -(1 / self.numPoints / self.normalizer) *
                        self.dat.T.dot(self.dat)[idx, jdx])
                ], [1.0])
                self.symmats.append(('Obj', self.numFields))
                task.putbaraij(0, 0, [
                    task.appendsparsesymmat(
                        self.numFields, range(self.numFields),
                        range(self.numFields), [1.0] * self.numFields)
                ], [1.0])
                self.cons.append('traceCon')
                self.symmats.append(('traceCon', self.numFields))
                for count, (i, j) in enumerate(zip(idx, jdx)):
                    task.putbaraij(count + 1, 0, [
                        task.appendsparsesymmat(self.numFields, [i], [j],
                                                [1.0])
                    ], [1.0])
                    task.putbaraij(count + 1, 1, [
                        task.appendsparsesymmat(self.numFields, [i], [j],
                                                [1.0])
                    ], [1.0])
                    self.cons.append('X<=ICon_%s_%s' % (i, j))
                    self.symmats.append(('X<=ICon%s_%s_%s' % (0, i, j), 1))
                    self.symmats.append(('X<=ICon%s_%s_%s' % (1, i, j), 1))
                task.putobjsense(mosek.objsense.minimize)
                self.m = mosek.Task(task)
Example #9
0
    try:
        try:
            import mosek
        except ImportError as e:
            import traceback
            text = traceback.format_exc()
            with open(logfile, "a", encoding='utf-8', errors="ignore") as f:
                f.write(text)
                f.write('\n')
                f.write(str(e))
        else:
            try:
                import solfmt

                with mosek.Env() as e:
                    with mosek.Task(e) as t:
                        t.readdata(probfile)
                        # Reset all string parameters. This should ensure that no rogue files are written
                        for p in mosek.sparam.members():
                            t.putstrparam(p, "")

                        t.set_Progress(pgscb)
                        t.linkfiletostream(mosek.streamtype.log, logfile, 0)
                        trm = t.optimize()

                        t.writetasksolverresult_file(tsolfile)
                        t.writejsonsol(jsolfile)
                        solfmt.formatSolution(t, 'ascii', asolfile)

                        with open(resfile, "wt", encoding="ascii") as f:
                            f.write("MSK_RES_OK")
Example #10
0
def mosek_inner_point_solver(A, output, y_target, threshold_value, r_change):
    """
    Solving Linear Programming on a Sample use Mosek
    the tutorial of Mosek on (https://docs.mosek.com/9.1/pythonapi/tutorial-lo-shared.html)
    :param A: jac matrix
        shape: [1, num_labels, input_dimension]
    :param output: label confidence
        shape: [1, num_labels]
    :param y_target: target label
        shape: [1, num_labels]
        value: {0, 1}
    :param threshold_value: target label confidence, default is threshold(0.5)
        shape: [1, num_labels]
        value: 0.5
    :param r_change:
    :return:
    """
    num_labels = y_target.shape[-1]
    num_instances = A.shape[0]
    d = A.shape[-1]
    inf = 0.0

    output = np.clip(output, 1e-6, 1.0 - (1e-6))
    loss_current = -(y_target * np.log(output) +
                     (1 - y_target) * np.log(1 - output))
    loss_target = -(y_target * np.log(np.ones(
        (num_labels)) * threshold_value) + (1 - y_target) * np.log(1 - np.ones(
            (num_labels)) * threshold_value))
    delta_loss = loss_target - loss_current
    r_change = r_change.reshape(num_instances, d)

    tasks = []
    with mosek.Env() as env:
        for i in range(num_instances):
            task = mosek.Task(env, 0, 0)
            # task.set_Stream(mosek.streamtype.log, streamprinter)
            task.putintparam(mosek.iparam.intpnt_multi_thread,
                             mosek.onoffkey.off)
            A = A[i]

            delta_loss = delta_loss[i]

            # delete the full zero rows in jac matrix
            delete_idx = []
            for j in range(num_labels):
                if (A[j] == 0).all():
                    delete_idx.append(j)
            if len(delete_idx) != 0:
                A = np.delete(A, delete_idx, axis=0)
                delta_loss = np.delete(delta_loss, delete_idx, axis=0)
            rows = A.shape[0]

            # Set the boundry of all variables
            # boundkey.fr [-inf, +inf]
            # boundkey.lo [value, +inf]
            # boundry for r1, r2,..., rd, z
            # bkx: boundry type
            # blx: low boundry
            # blx: up boundry
            bkx = [mosek.boundkey.ra for i in range(d)] + [mosek.boundkey.lo]
            blx = [-1 for i in range(d)] + [0.0]
            bux = [1 for i in range(d)] + [1.]

            # Set the boundry of all constraints
            # boundkey.up [-inf, value]
            # bkc: boundry type
            # blc: low boundry
            # blc: up boundry
            bkc = [mosek.boundkey.up for i in range(rows + d * 2)]
            blc = [-inf for i in range(rows + d * 2)]
            buc = delta_loss.tolist() + [0.0 for i in range(d * 2)]

            #  sparse matrix ordinal value, matrix stored by column.
            # shape: n_x_feature, n_label
            # [[0, 1, 2, ..., 19],
            #   ...
            # [0, 1, 2, ..., 19]]
            zeros_to_rows = np.tile(np.arange(rows).reshape(1, -1), (d, 1))

            # [20, 22, 24, ...,]
            rows_to_rows_add_d = np.arange(start=rows,
                                           stop=rows + d * 2,
                                           step=2).reshape(-1, 1)
            # [21, 23, 25, ...,]
            rows_add1_to_rows_add_d = (rows_to_rows_add_d + 1).reshape(-1, 1)
            B = np.c_[zeros_to_rows, rows_to_rows_add_d,
                      rows_add1_to_rows_add_d]
            asub = B.tolist()

            ones_d = np.ones(d).reshape(-1, 1)
            A = np.c_[A.T, ones_d, -ones_d]
            aval = A.tolist()

            asub.extend([[i for i in range(rows, rows + d * 2)]])
            aval.extend([[-1 for i in range(rows, rows + d * 2)]])
            c = [0.0 for i in range(d)] + [1]
            numvar = len(bkx)
            numcon = len(bkc)
            task.appendcons(numcon)
            task.appendvars(numvar)

            #garbage collection
            del A
            del B
            del output
            del zeros_to_rows
            del rows_to_rows_add_d
            del rows_add1_to_rows_add_d
            gc.collect()

            for j in range(numvar):
                # Set the linear term c_j in the objective.
                task.putcj(j, c[j])
                task.putvarbound(j, bkx[j], blx[j], bux[j])
                task.putacol(
                    j,  # Variable (column) index.
                    asub[j],  # Row index of non-zeros in column j.
                    aval[j])  # Non-zero Values of column j.
            for j in range(numcon):
                task.putconbound(j, bkc[j], blc[j], buc[j])

            task.putobjsense(mosek.objsense.minimize)
            task.putintparam(mosek.iparam.optimizer,
                             mosek.optimizertype.intpnt)
            task.putintparam(mosek.iparam.intpnt_basis, mosek.basindtype.never)

            tasks.append(task)

        # if the solution fails, then result is full zero
        result = np.zeros((num_instances, d))
        for i in range(num_instances):
            tasks[i].optimize()
            # tasks[i].solutionsummary(mosek.streamtype.msg)
            # Get status information about the solution
            solsta = tasks[i].getsolsta(mosek.soltype.itr)
            if (solsta == mosek.solsta.optimal):
                xx = [0.] * numvar
                tasks[i].getxx(
                    mosek.soltype.itr,  # Request the basic solution.
                    xx)
                xx = np.asarray(xx)
                result[i] = xx[:-1]
            elif (solsta == mosek.solsta.dual_infeas_cer
                  or solsta == mosek.solsta.prim_infeas_cer):
                print("Primal or dual infeasibility certificate found.\n")
            elif solsta == mosek.solsta.unknown:
                print("Unknown solution status")
            else:
                print("Other solution status")
    return result
Example #11
0
    def __init__(self,
                 rsp,
                 dat,
                 lam=1,
                 conic=True,
                 dual=False,
                 K=None,
                 Y=None,
                 presolveTol=1.0e-30,
                 outputFlag=False):
        inf = 0.0
        numPnt = dat.shape[0]
        self.K = K
        self.Y = Y
        self.numFields = dat.shape[1]
        self.numPoints = numPnt
        self.dat = dat
        # To normalize variances
        #for col,var in enumerate(np.var(self.dat,axis=0)):
        #    self.dat[:,col] -= np.mean(self.dat[:,col])
        #    self.dat[:,col] *= 1/math.sqrt(var)
        #    self.dat[:,col] += np.mean(self.dat[:,col])
        self.rsp = rsp
        self.lam = lam
        self.dual = dual
        self.alpha = [0.] * self.numPoints
        self.B = [0.] * self.numFields
        self.b = 0.0
        self.eps = [0.] * self.numPoints
        self.RunTime = 0
        self.conList = []
        self.quadConstr = False
        self.conic = conic

        # Make mosek environment
        with mosek.Env() as env:

            # Create a task object and attach log stream printer
            with env.Task(0, 0) as task:
                task.putdouparam(mosek.dparam.presolve_tol_x, presolveTol)
                # options for convexity check are none, simple, full
                task.putintparam(mosek.iparam.check_convexity,
                                 mosek.checkconvexitytype.none)
                if outputFlag:
                    task.set_Stream(mosek.streamtype.msg, streamprinter)

                if not dual:
                    # Bound keys for constraints, vars
                    bkc = [mosek.boundkey.lo] * numPnt
                    bkx = [mosek.boundkey.fr] * (
                        self.numFields + 1) + [mosek.boundkey.lo] * numPnt
                    blc = [1.0] * numPnt
                    buc = [+math.inf] * numPnt
                    blx = [-math.inf] * (self.numFields + 1) + [0.0] * numPnt
                    bux = [+math.inf] * (self.numFields + numPnt + 1)

                    # Below is the sparse representation of the A
                    # matrix stored by row.
                    asub = [
                        list(range(self.numFields + 1)) +
                        [self.numFields + i + 1] for i in range(numPnt)
                    ]
                    aval = [
                        list((2 * rsp[i] - 1) * dat[i]) +
                        [2 * rsp[i] - 1.0, 1.0] for i in range(numPnt)
                    ]

                    numvar = self.numFields + numPnt + 1
                    numcon = len(bkc)

                    task.appendvars(numvar)
                    task.appendcons(numcon)

                    # Set objective.
                    task.putclist(
                        [self.numFields + i + 1 for i in range(numPnt)],
                        [lam] * numPnt)

                    task.putvarboundslice(0, numvar, bkx, blx, bux)
                    task.putconboundslice(0, numcon, bkc, blc, buc)
                    for i in range(numcon):
                        task.putarow(i, asub[i], aval[i])

                    if self.conic:
                        # t>=0, z==1
                        task.appendvars(2)
                        task.putvarbound(task.getnumvar() - 2,
                                         mosek.boundkey.up, 0.0, +inf)
                        task.putvarbound(task.getnumvar() - 1,
                                         mosek.boundkey.fx, 1., 1.)
                        # t*z>=sumsqr(B), obj coeff of t == 1
                        task.appendcone(
                            mosek.conetype.rquad, 0.0,
                            [task.getnumvar() - 2,
                             task.getnumvar() - 1] +
                            list(range(self.numFields)))
                        task.putcj(task.getnumvar() - 2, 1.)
                    else:
                        # sumqrt(B) in obj
                        qsubi = list(range(self.numFields))
                        qsubj = list(range(self.numFields))
                        qval = [1.0] * self.numFields
                        task.putqobj(qsubi, qsubj, qval)

                else:
                    # Bound keys for constraints, vars
                    bkc = [mosek.boundkey.fx]
                    bkx = [mosek.boundkey.ra] * numPnt
                    blc = [0.0]
                    buc = [0.0]
                    blx = [0.0] * numPnt
                    bux = [lam] * numPnt

                    numvar = numPnt
                    numcon = 1

                    task.appendvars(numvar)
                    task.appendcons(numcon)

                    # Set objective.
                    task.putclist(list(range(numPnt)), [-1.0] * numPnt)

                    task.putvarboundslice(0, numvar, bkx, blx, bux)
                    task.putconboundslice(0, numcon, bkc, blc, buc)
                    task.putarow(0, range(numPnt), 2 * rsp - 1)

                    qval = K * Y + 1e-6 * np.eye(len(K))
                    if self.conic:
                        self.QChol = la.cholesky(
                            qval,
                            lower=True)  # such that K*Y = QChol.dot(QChol.T)
                        task.appendvars(self.QChol.shape[1] + 2)
                        task.appendcons(self.QChol.shape[1])
                        # c unbounded, t>=0, z==1
                        task.putvarboundslice(
                            numPnt, numPnt + self.QChol.shape[1],
                            [mosek.boundkey.fr] * self.QChol.shape[1],
                            [-math.inf] * self.QChol.shape[1],
                            [math.inf] * self.QChol.shape[1])
                        task.putvarbound(numPnt + self.QChol.shape[1],
                                         mosek.boundkey.lo, 0.0, math.inf)
                        task.putvarbound(numPnt + self.QChol.shape[1] + 1,
                                         mosek.boundkey.fx, 1.0, 1.0)
                        task.putconboundslice(1, self.QChol.shape[1] + 1,
                                              [mosek.boundkey.fx] *
                                              self.QChol.shape[1],
                                              [0.] * self.QChol.shape[1],
                                              [0.] * self.QChol.shape[1])
                        i, j = np.meshgrid(range(1, self.QChol.shape[1] + 1),
                                           range(numPnt))
                        # QChol.T.dot(alpha)==c
                        task.putaijlist(i.flatten(), j.flatten(),
                                        self.QChol.flatten())
                        task.putaijlist(
                            range(1, self.QChol.shape[1] + 1),
                            range(numPnt, numPnt + self.QChol.shape[1]),
                            [-1.] * self.QChol.shape[1])
                        # t*z>=sumsqr(c)==alpha.T.dot(K*Y).dot(alpha)
                        task.appendcone(
                            mosek.conetype.rquad, 0.0,
                            [task.getnumvar() - 2,
                             task.getnumvar() - 1] +
                            list(range(numPnt, numPnt + self.QChol.shape[1])))
                        task.putcj(task.getnumvar() - 2, 1.)
                    else:
                        qsubj, qsubi = np.meshgrid(range(numPnt),
                                                   range(numPnt))
                        idx = np.where(
                            np.tril(np.ones_like(qval)).flatten() != 0)
                        task.putqobj(qsubi.flatten()[idx],
                                     qsubj.flatten()[idx],
                                     qval.flatten()[idx])

                task.putobjsense(mosek.objsense.minimize)
                self.m = mosek.Task(task)