Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Write the solution on a file

    Posted 10/23/18 04:17 AM

    Originally posted by: felycite28


    Hello everyone

    How can I write the solution value of a certain variable on a file when I code in python ?

    What I did :

                for m in range(N):
                    for t in range(T):
                        x = "x(" + str(m + 1) + ")(" + str(t + 1) + ")"
                        ResultX.append((x, model.solution.get_values(x)))
                        model.solution.write(ResultX,"File.txt")

    It did not work

    Merci

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Write the solution on a file

    Posted 10/23/18 06:24 AM

    What exactly are you trying to do? Do you just want to print the solution to a text file? Then you should use standard Python files. Or do you want to write the solution in a particular format?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Write the solution on a file

    Posted 10/23/18 08:29 AM

    Originally posted by: felycite28


    I am trying to the solution in a particular format like in a txt file.End of the code , I have so many variables but I just wanted to write x variable  and its solutiona values . I will use this saved file for another code and I will read the values . Thank you so much

    x[1][1]=1

    x[1][2]=2

    x[1][3]=3

    x[2][1]=6

    x[2][2]=7

    x[2][3]=8


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Write the solution on a file

    Posted 10/23/18 09:10 AM

    For this format you should just use plain Python files.

    with open('file.txt', 'w') as f:
      for m in range(N):
        for t in range(T):
          f.write("x(" + str(m + 1) + ")(" + str(t + 1) + ")=" + model.solution.get_values(yyy)))

    If you are unclear about this then it is probably best to consult some Python tutorial for file I/O. Note that instead of yyy you need to put a reference to the variable with index (m,t).


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Write the solution on a file

    Posted 10/23/18 09:26 AM

    Originally posted by: felycite28


    I tried this way but I get an error for "f" because I solve the problem

    like

    model=model.Cplex()

    I have contraints here

    model.add linear constraints ()

    ...........

    ...........

    ...........

    model.solve ()

    In this case I want to write the solution values of x variables.  When I do in the manner you proposed , I get an error for "f".

    How can I modify it according to my case ?

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Write the solution on a file

    Posted 10/23/18 09:42 AM

    'f' is completely unrelated to your model or how you define it. Can you show the offending code and also tell what the error is that you get?

    It seems this is more a general Python problem than something related to CPLEX?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Write the solution on a file

    Posted 10/23/18 09:46 AM

    Originally posted by: felycite28


    I get this error

        f.write("x(" + str(m + 1) + ")(" + str(t + 1) + ")=" + model.solution.get_values(x)))
                                                                                            ^
    : cannot concatenate 'str' and 'float' objects


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Write the solution on a file

    Posted 10/23/18 10:45 AM

    Well, of course you should not just blindly copy code from here but double-check it. Sorry for posting this buggy code.

    From the error message it is also pretty obvious what is wrong: you have to wrap the float into 'str()'. Or maybe even better use formatted output using the '%' operator.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: Write the solution on a file

    Posted 10/23/18 11:20 AM

    Originally posted by: felycite28


    Thank you so much Daniel


    #DecisionOptimization
    #OPLusingCPLEXOptimizer