Example #1
0
 def to_lp(self):
     self.update()
     with TemporaryFilename(suffix=".lp") as tmp_file_name:
         glp_write_lp(self.problem, None, tmp_file_name)
         with open(tmp_file_name) as tmp_file:
             lp_form = tmp_file.read()
     return lp_form
Example #2
0
 def to_lp(self):
     self.update()
     with TemporaryFilename(suffix=".lp") as tmp_file_name:
         glp_write_lp(self.problem, None, tmp_file_name)
         with open(tmp_file_name) as tmp_file:
             lp_form = tmp_file.read()
     return lp_form
Example #3
0
 def __str__(self):
     with tempfile.NamedTemporaryFile(suffix=".lp",
                                      delete=True) as tmp_file:
         tmp_file_name = tmp_file.name
         glp_write_lp(self.problem, None, tmp_file_name)
         with open(tmp_file_name) as tmp_file:
             cplex_form = tmp_file.read()
     return cplex_form
Example #4
0
def solve_with_glpsol(glp_prob):
    """Solve glpk problem with glpsol commandline solver. Mainly for testing purposes.

    # Examples
    # --------

    # >>> problem = glp_create_prob()
    # ... glp_read_lp(problem, None, "../tests/data/model.lp")
    # ... solution = solve_with_glpsol(problem)
    # ... print 'asdf'
    # 'asdf'
    # >>> print solution
    # 0.839784

    # Returns
    # -------
    # dict
    #     A dictionary containing the objective value (key ='objval')
    #     and variable primals.
    """
    from swiglpk import glp_get_row_name, glp_get_col_name, glp_write_lp, glp_get_num_rows, glp_get_num_cols

    row_ids = [glp_get_row_name(glp_prob, i) for i in range(1, glp_get_num_rows(glp_prob) + 1)]

    col_ids = [glp_get_col_name(glp_prob, i) for i in range(1, glp_get_num_cols(glp_prob) + 1)]

    with tempfile.NamedTemporaryFile(suffix=".lp", delete=True) as tmp_file:
        tmp_file_name = tmp_file.name
        glp_write_lp(glp_prob, None, tmp_file_name)
        cmd = ['glpsol', '--lp', tmp_file_name, '-w', tmp_file_name + '.sol', '--log', '/dev/null']
        term = check_output(cmd)
        log.info(term)

    try:
        with open(tmp_file_name + '.sol') as sol_handle:
            # print sol_handle.read()
            solution = dict()
            for i, line in enumerate(sol_handle.readlines()):
                if i <= 1 or line == '\n':
                    pass
                elif i <= len(row_ids):
                    solution[row_ids[i - 2]] = line.strip().split(' ')
                elif i <= len(row_ids) + len(col_ids) + 1:
                    solution[col_ids[i - 2 - len(row_ids)]] = line.strip().split(' ')
                else:
                    print(i)
                    print(line)
                    raise Exception("Argggh!")
    finally:
        os.remove(tmp_file_name + ".sol")
    return solution
Example #5
0
def solve_with_glpsol(glp_prob):
    """Solve glpk problem with glpsol commandline solver. Mainly for testing purposes.

    # Examples
    # --------

    # >>> problem = glp_create_prob()
    # ... glp_read_lp(problem, None, "../tests/data/model.lp")
    # ... solution = solve_with_glpsol(problem)
    # ... print 'asdf'
    # 'asdf'
    # >>> print solution
    # 0.839784

    # Returns
    # -------
    # dict
    #     A dictionary containing the objective value (key ='objval')
    #     and variable primals.
    """
    from swiglpk import glp_get_row_name, glp_get_col_name, glp_write_lp, glp_get_num_rows, glp_get_num_cols

    row_ids = [glp_get_row_name(glp_prob, i) for i in range(1, glp_get_num_rows(glp_prob) + 1)]

    col_ids = [glp_get_col_name(glp_prob, i) for i in range(1, glp_get_num_cols(glp_prob) + 1)]

    with tempfile.NamedTemporaryFile(suffix=".lp", delete=True) as tmp_file:
        tmp_file_name = tmp_file.name
        glp_write_lp(glp_prob, None, tmp_file_name)
        cmd = ['glpsol', '--lp', tmp_file_name, '-w', tmp_file_name + '.sol', '--log', '/dev/null']
        term = check_output(cmd)
        log.info(term)

    try:
        with open(tmp_file_name + '.sol') as sol_handle:
            # print sol_handle.read()
            solution = dict()
            for i, line in enumerate(sol_handle.readlines()):
                if i <= 1 or line == '\n':
                    pass
                elif i <= len(row_ids):
                    solution[row_ids[i - 2]] = line.strip().split(' ')
                elif i <= len(row_ids) + len(col_ids) + 1:
                    solution[col_ids[i - 2 - len(row_ids)]] = line.strip().split(' ')
                else:
                    print(i)
                    print(line)
                    raise Exception("Argggh!")
    finally:
        os.remove(tmp_file_name + ".sol")
    return solution
Example #6
0
 def to_lp(self):
     self.update()
     with TemporaryFilename(suffix=".lp") as tmp_file_name:
         code = glp_write_lp(self.problem, None, tmp_file_name)
         if code != 0:
             raise Exception("GLPK could not successfully create the LP.")
         with open(tmp_file_name) as tmp_file:
             lp_form = tmp_file.read()
     return lp_form
Example #7
0
 def to_lp(self):
     self.update()
     with TemporaryFilename(suffix=".lp") as tmp_file_name:
         code = glp_write_lp(self.problem, None, tmp_file_name)
         if code != 0:
             raise Exception("GLPK could not successfully create the LP.")
         with open(tmp_file_name) as tmp_file:
             lp_form = tmp_file.read()
     return lp_form
Example #8
0
 def __str__(self):
     tmp_file = tempfile.mktemp(suffix=".lp")
     glp_write_lp(self.problem, None, tmp_file)
     cplex_form = open(tmp_file).read()
     return cplex_form
Example #9
0
    def write_lp_cplex(self, filename):
        '''write the lp in CPLEX format'''

        if glpk.glp_write_lp(self.lp, None, filename) != 0:
            raise RuntimeError('Error saving CLPEX-format LP to {}'.format(filename))