thawan kooburat cs635 tools and environments for optimization spring 2011 neos java developer...
TRANSCRIPT
1
THAWAN KOOBURAT
CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION
SPRING 2011
NEOS Java Developer Tutorial
2
NEOS
The Network-Enabled Optimization System Since 1995 Provides interface to varieties of solvers and modeling
languages Both commercial and open-source solvers Modeling Languages: GAMS, AMPL, MPS, etc.
Provides various submission interface Web, Email and XML-RPC
Solvers are located at various institutions and countries UW-Madison, ANL, ASU, Austria, Spain
3
Architecture
Job submission
Server
Solver
MINOS : GAMS
User
User
Web: Upload file
XML-RPC: Job XML
4
Web Submission
Combine data and model into the same file Some commands are not available or may create error
option minlp = dicopt;
Choose solver for the list http://www.neos-server.org/neos/solvers/index.html
Upload model files MINOS - GAMS
5
Let’s try
Solving diet problem using MINOS Using test server
http://neos1.chtc.wisc.edu/neos/solvers/nco:MINOS/GAMS.html
Use the model file from tutorial package resources/tutorial/simplediet.txt
The result is same as the list file produced by GAMS
6
XML-RPC
Language-independent protocol Java, Python, etc.
Need to submit problem to NEOS using Job XML Go back to submission page Check Dry run box at the bottom and hit submit File content is included as a node in a tree
See the XML Specification of each solver Click on XML-RPC link on the top right corner
7
Developer Tutorial
Learn how to submit job to NEOS using Java (via XML-RPC)
Writing a simple applet Accept user input Generate model and submit job to NEOS Parse result Display output
8
Development Environment
Recommended IDE: EclipsePrepare tutorial project
Extract zip file into Eclipse’s workspace folder Open Eclipse File-> New -> JavaProject
Type “DietGAMS” and press next all the way Or uncheck “default location”
Point to path where tutorial is extracted
9
Hello World!
org.neos.tutorial.HelloNEOS.java Solver support + and x operation
See Job XML spec from http://www.neos-server.org/neos/solvers/test:HelloNEOS/default-help.html
Create job XML NeosJobXml job = NeosJobXml("test", "HelloNEOS",
"default"); Category : test Solver Name: HelloNEOS Input Method: default
10
Hello World! (cont.)
Add parameters job.addParam("num1", "55");
Parameter Name: num1 Value: 55
Connect to server NeosClient client = new NeosClient(HOST, PORT);
Submit job NeosJob result = client.submitJob(job.toXMLString());
Getting result result.getResult()
11
SimpleDiet
Start with a command line versionThe diet model is already loaded into “model”
variable. Modify line 39 and 40 to submit this problem
to MINOS solverLookup Job XML spec to see what are needed
http://neos1.chtc.wisc.edu/neos/solvers/nco:MINOS/GAMS-help.html
The result should print to console
12
Parsing Result
We may need to visualize the result We provide facility to parse solution output: SolutionParser
http://neos2.chtc.wisc.edu/NEOS/index.php/Developer_Guides#Output_Parser
SolutionData xx = parser.getSymbol("xx", SolutionData.VAR, 2);
xx.getRows().get(1)
getIndex(0)
getIndex(1)
getLevel()
parser.getModelStatusCode()
13
Solution Parser Usage
Enable solution parser in our example SOLUTION_PARSER = true
Look at the result and indentify data to extract
Modify the rest to get the actual data
14
Results.txt
SolutionParser limitation The output from list file has low precision We want data in other forms
Results.txt allows you to send back anything you want from the model Use ‘put’ facility to write output to results.txt Retrieve the results.txt file from the server You may use comma-separate format for easy parsing
15
Results.txt (cont.)
Enable results.txt in our example RESULTS_TXT = true
Check the model file resources/tutorial/simplediet.txt
[Optional] Add more put command, use comma to separate each column
Run the example
16
Simple Diet Applet
Accept user input to modify the data Let user select the list of food
Generate model and submit problem to NEOS Incorporate the modified data into model file
Retrieve and parse the output Parse the output into our internal representation
Visualize the result Use Java swing components or chart to display.
17
Java Applet Crash Course
Java Applet Java application which use Applet as a top-level container Archived in ‘jar’ format The applet is execute locally on user machine
GUI model – Layers of components Useful reference guides
Components: http://download.oracle.com/javase/tutorial/ui/features/components.html
Layouts: http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
JScrollPane JTable
JPanel JButton
Applet
18
Classes
SimpleDietApplet SimpleDietPanel
SimpleFoodSelectionPanel
SimpleDietResultFrame SimpleDietChartPanel
19
SimpleDietPanel
UI LayoutJFrame
JPanel (SimpleDietPanel)
JPanel (buttonPanel)
JButton JButton
JPanel (FoodSelectionPanel)
20
SimpleDietPanel
How the buttons works
JPanel (buttonPanel)
Submit Reset
public void createGUI() { JButton submitButton = new JButton("Submit"); submitButton.setActionCommand(SUBMIT_COMMAND); submitButton.addActionListener(this);
JButton resetButton = new JButton("Reset"); resetButton.setActionCommand(RESET_COMMAND); resetButton.addActionListener(this);}
public class SimpleDietPanel extends JPanel implements ActionListener
public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals(SUBMIT_COMMAND)) submitJob(); else if (command.equals(RESET_COMMAND)) clearSelection(); }
21
SimpleDietPanel
How stuff get displayed
public void createGUI() {
setLayout(new BorderLayout()); .. JPanel buttonPanel = new JPanel(new GridLayout(1, 2)); buttonPanel.add(submitButton); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.SOUTH);
selectionPanel = new SimpleFoodSelectionPanel(foodList); add(selectionPanel, BorderLayout.CENTER);
setPreferredSize(new Dimension(500, 300));
SimpleDietPanel
selectionPanel
submit reset
Layout Reference Guide: http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
North
South
Center
22
SimpleDietPanel
Application Logic
SimpleFood class is internal data representation String shortName – name used in the model (Read from text file) String longName – named used to display to user (Read from text file) Boolean selected – is this food selected by user (Modify by user) Double buy – The amount purchase (Need to extract from GAMS
output)
submitJob() Divide model into 3 parts
dataDomain – Set declarations selected – Set of selected food model – the rest of data and model
Need to allow user to select list of food
Set fs(f) foods selected / Bro1,Chi66,Por50,Pot7,Tor56 /;
Parameters ..Table ..Positive Variable ..Equations ..
Sets n nutrients / Calories, Cholesterol,…Set f foods / Bro1, Car2, Cel3, Cor4, Let5, …
23
SimpleFoodSelectionPanel
Use JTable to display data Need to extends AbstractTableModel to create a table that
map each cell (row, column) to our data Specify column names, Modify getValueAt() and setValueAt()
columnNames
food.getSelected()food.setSelected()
food.getLongName()
24
SimpleDietPanel
Translate user selection into GAMS data The model is expecting a selected list of food
Set fs(f) foods selected / Bro1,Chi66,Por50,Pot7,Tor56 /;
Uncomment code in the constructor to enable FoodSelectionPanel Modify getSelection() to generate this string
Use our wrapper class to create GAMS data http://neos2.chtc.wisc.edu/NEOS/index.php/Developer_Guides#Input_Data_Generator
25
SimpleDietChartPanel
Assume that we are able to get back correct data food.getBuy() will return the amount of food bought
Display this data using chart (JFreeChart) JFreeChart is free, but its documentations are not Distributed binary contains demo to show sample charts
Modify generateChart() to populate JFreeChart dataset dataset.addValue( VALUE, SERIES, CATEGORY );
Category Name : blank (empty string) Series Name: longName Value: buy
26
SimpleDietResultFrame
Extends JFrame So that it can be launch as a stand-alone popup
Implements ResultCallback interface Will explain later why we should use this interface Receive job info after it is submitted
handleJobInfo(int jobNo, String jobPass); Receive output when job completes
handleFinalResult(String results);
Modify handleFinalResult() Use solution parser to extract value (level) from buy variable Update by “buy” value
27
SimpleDietPanel
Putting all togetherNeed to launch job popup when we submit a job
Create an instance of SimpleDietResultFrame
[1] Call handleJobInfo, and handleFinalResult to display output Did you notice that UI freeze when you hit submit
button?• Application is single thread, so it is blocked waiting for
result
[2] Use submitJobNonBlocking(String xml, ResultCallback callback) It accepts object that implement ResultCallback
interface Will spawn a thread can call those methods when
information is available
28
SimpleDietApplet
Wrapper to provide container in Applet environment
Tell SimpleDietPanel to run in applet mode Applet to a different method to read file included in its
archive FileUtils already take care of this, but we need to tell
it which mode we are using FileUtils.APPLET_MODE : Running inside a jar
file FileUtils.APPLICATION_MODE : Running normally
29
Prepare Applet Archive
Use Eclipse utility to create archive Right click DietJar.jardesc Select “Create JAR”
Sign the archive Certain operations are restricted when running as
Applet Signing allow us to remove some restrictions
Windows: Double-click “signjar.bat” in Windows explorer
UNIX: Run “signjar.sh” Any external libraries need to be signed as well
All jar in slib folder is signed
30
Run Applet in Browser
SimpleDietApplet.html Use <APPLET/> tag to display applet
CODE: Fully-qualify class name ARCHIVE: List of jars file used by this applet WIDTH, HEIGHT: Control display area
Open this file in web browser to see the applet
31
Publish to Wiki
See Diet Case study for example http://neos2.chtc.wisc.edu/NEOS/index.php/Diet_Problem_
DemoCreate account, add new page, upload Jar fileUse <java_applet/> tag to display applet
code: Fully-qualify class name archive: List of jars file used by this applet.
Jar used by this demo is already uploaded. archive="SignedDiet.jar,Commons-logging-1.1.jar, .."
width, height:Control display area
32
Tips
Works in modules and compose them together Handle exact component layout at the last stage
Get a command line version working firstThink about infeasible
What to say to user, Limit execution time Good internal data representation make life
easy
33
Questions
Q/A