Decision Optimization

Decision Optimization

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

 View Only

How to do input in OPL ? How to ask ?

  • 1.  How to do input in OPL ? How to ask ?

    Posted Thu February 08, 2018 04:38 AM

    Hi,

    in many languages it is very easy to let the user enter a value , not only as a file or a parameter but in a very interactive way.

    In OPL that s not possible but let me offer a workaround through IloOplExec and python.

     

        execute
        {

        function input(question)
        {
            var quote="\"";
            
            var python=new IloOplOutputFile("c:\\input.py");
            
            python.writeln("from tkinter import *");

            python.writeln("def save():");
            python.writeln("   print(popup.get())");
            python.writeln("   master.quit");

            python.writeln("master = Tk()");
            python.writeln("Label(master, text=",quote,question,quote,").grid(row=0)");


            python.writeln("popup= Entry(master)");

            python.writeln("popup.grid(row=0, column=1)");

            python.write("Button(master, text='Ok', command=master.quit).");
            python.writeln("grid(row=3, column=1, sticky=W, pady=4)");

            python.writeln("mainloop( )");

            python.writeln("res = open(",quote,"c:\\\\respopup.txt",quote,",",quote,"w",quote,")");
            python.writeln("res.write(popup.get())");
            
            python.writeln("res.close()");
            python.writeln("master.destroy()");
           
            python.close();
            
            IloOplExec("C:\\Users\\IBM_ADMIN\\AppData\\Local\\Programs\\Python\\Python36\\python.exe c:\\input.py",true);
            var f=new IloOplInputFile("c:\\respopup.txt");
            var res=f.readline();
            f.close();
            return res;
        }


        var s=input("what is your name ?");
        writeln("hello ",s);

        }

    gives

    and then you may answer

    and then in the scripting log you get

    hello Boris

    regards

     

    Alex Fleischer

    PS:

    Many how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    Many examples from a very good book : https://www.linkedin.com/pulse/model-building-oplcplex-alex-fleischer/

    Making optimization simple : https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer