thawan kooburat cs635 tools and environments for optimization spring 2011 neos java developer...

33
THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

Upload: jonas-jones

Post on 16-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

1

THAWAN KOOBURAT

CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION

SPRING 2011

NEOS Java Developer Tutorial

Page 2: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 3: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

3

Architecture

Job submission

Server

Solver

MINOS : GAMS

User

User

Web: Upload file

XML-RPC: Job XML

Page 4: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 5: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 6: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 7: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 8: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 9: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 10: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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()

Page 11: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 12: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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()

Page 13: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 14: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 15: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 16: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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.

Page 17: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 18: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

18

Classes

SimpleDietApplet SimpleDietPanel

SimpleFoodSelectionPanel

SimpleDietResultFrame SimpleDietChartPanel

Page 19: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

19

SimpleDietPanel

UI LayoutJFrame

JPanel (SimpleDietPanel)

JPanel (buttonPanel)

JButton JButton

JPanel (FoodSelectionPanel)

Page 20: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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(); }

Page 21: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 22: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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, …

Page 23: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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()

Page 24: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 25: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 26: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 27: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 28: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 29: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 30: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 31: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 32: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

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

Page 33: THAWAN KOOBURAT CS635 TOOLS AND ENVIRONMENTS FOR OPTIMIZATION SPRING 2011 NEOS Java Developer Tutorial 1

33

Questions

Q/A