Table Of Contents

Previous topic

hero.py

Next topic

optimizer.py

This Page

jlp.py

class JLP(Optimizer):

Interface between SIMO optimizer and JLP linear programming library.

Parameters:

  • ...

def __init__(self, logger, logname, optlogger, taskdef, simdbin, opdbin, simdbout, opdbout, **keywords):

Initialize optimizer:

>>> epsilon = 0.00001
>>> from simo.optimization.jlp import JLP
>>> execfile('optimization/test/mocks4optimizer.py')
>>> chart_path = 'optimization/test'

>>> jlp = JLP(logger, logname, taskdef, simdbin, simdbout, False, False,
...           False, chart_path, True, 'test', True, timeout=30)
Called Logger.log_message(
    'optimization-test',
    'ERROR',
    "Parameter 'maxobjects' missing")
Called Logger.log_message(
    'optimization-test',
    'ERROR',
    "Parameter 'jfolder' missing")
Called Logger.log_message(
    'optimization-test',
    'ERROR',
    "Parameter 'prefix' missing")

>>> jlp = JLP(logger, logname, taskdef, simdbin, simdbout, False, False,
...           False, chart_path, True, 'test', True,
...           maxobjects=1000, jfolder='', prefix='test', timeout=30,
...           do_split=False, skip_weight_splits=None)

>>> jlp2 = JLP(logger, logname, taskdef2, simdbin, simdbout, False, False,
...           False, chart_path, True, 'test', True,
...           maxobjects=1000, jfolder='', prefix='test', timeout=30,
...           do_split=False, skip_weight_splits=None)

def optimize(self):

Construct J input files, run J program as a subprocess and process J output

def _process_branch(self, unitsol, weights):

Process unit split by the JLP algorithm

def _process_infeasible(self):

Process JLP output when the problem is infeasible

def _copy_result_file(self, respath, iteration):

Parameters:

respath – result file path, str iteration – iteration indice, int

Copy JLP result file to result folder

def _read_solution(self, iteration):

Parameters:

iteration – iteration indice, int

Read and process solution from JLP output files

def _remove_j_files(self):

Remove J input and output files

def _update_stack(self, stack):

Parameters:

stack – postfix stack, list

Update infix stack:

>>> stack = [['a'], ['b']]
>>> jlp._update_stack(stack)
>>> print stack
[['a', 'b']]

def _process_const_expr(self, num_of_expr, expressions):

Parameters:

num_of_expr – number of expressions as integer expressions – SIMO postfix expressions in a list

Process invalid constraint postfix expression:

>>> jlp._init_ok = True
>>> jlp._process_const_expr(invalid_taskdef.constraints)
Called Logger.log_message(
    'optimization-test',
    'ERROR',
    "Invalid operator for J '/'")
Called Logger.log_message(
    'optimization-test',
    'ERROR',
    'JLP condition right hand side must be a numerical value')
>>> jlp.j_consts.infix 
['c0 / c1 > 1.05', 'c2 > c3']
>>> jlp.j_consts.names
['c0', 'c1', 'c2']
>>> jlp.j_consts.order
[0, 1, 2]
>>> print jlp._init_ok
False

Process constraint postfix expression back to infix form and try to figure out if the expression is good for JLP:

>>> jlp._process_const_expr(taskdef.constraints)
>>> jlp.j_consts.infix 
['c0 - c1 > 0.00', 'c2 - c3 > 0.00', 'tr1 - tr2 > 0.00']
>>> jlp.j_consts.names 
['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7']
>>> jlp.j_consts.order
[0, 1, 2, 3, 4, 5, 6, 7]
>>> jlp.transformations
['tr1 = c4 * c5', 'tr2 = c6 * c7']

def _process_gp_vars(self, no):

Parameters:

no – number of subobjectives, int

Construct slack and surplus variables needed in JLP goal programming tasks

>>> jlp2._process_gp_vars(4)

def _write_j_par(self, partype):

Parameters:

partype – parameter type, either ‘init’ or ‘close’

Write J parameter file (j.par):

>>> jlp._write_j_par('init')
>>> fp = open('j.par', 'r')
>>> contents = fp.read()
>>> print contents
!1000
;incl('test_problem.txt')

>>> fp.close()

def _write_j_problem(self):

Write the optimisation problem definition to a file for J:

>>> jlp._obj_vars = ['x0', 'x1']
>>> jlp._write_j_problem()
>>> fp = open('test_problem.txt', 'r')
>>> contents = fp.read()
>>> print contents  
! SUBOBJECTIVE VARIABLES
!   x0 = 1.0 * (sum[1:5](operation:cashflow:discount))
!   x1 = 1.0 * (sum[6:10](operation:cashflow:discount))

! CONSTRAINT VARIABLES (see SIMO lexicon for details)
!   c0 = V, steps 20-20
!   c1 = V, steps 10-10
!   c2 = Volume, steps 20-end
!   c3 = Volume, steps 10-end
!   c4 = V, steps 20-20
!   c5 = AREA, steps 20-20
!   c6 = V, steps 10-10
!   c7 = AREA, steps 10-10

! READ OBJECTIVE AND CONSTRAINT VARIABLE VALUES FROM FILE
udat=data(read->(id,nbranch),in->'test_unitdata.txt')
sdat=data(read->(x0,x1,c0,c1,c2,c3,c4,c5,c6,c7), in->'test_vardata.txt')
linkdata(data->udat,subdata->sdat,nobsw->nbranch)

! TRANSFORM ILLEGAL CONSTRAINTS
strans=trans()
tr1 = c4 * c5
tr2 = c6 * c7
/

! OPTIMISATION PROBLEM DEFINITION
pr=problem()

! OBJECTIVE FUNCTION DEFINITION
0.3*x0+0.7*x1==max

! CONSTRAINT DEFINITIONS
c0 - c1 > 0.00
c2 - c3 > 0.00
tr1 - tr2 > 0.00
/
jlp(problem->pr,data->udat,subtrans->strans,report->'...jlp_output.txt')
branch_report=trans()
do(i,1,weights())
write('...bresult.txt',$,matrix%udat(unit(i),1),schedw(i),price%unit(unit(i)))
enddo
/
weight_report=trans()
do(i,1,partweights())
write('...wresult.txt',$,partunit(i),partschedw(i),partweight(i))
enddo
/
;if(Feasible);then
call(branch_report)
call(weight_report)
;else
write('...bresult.txt','(~infeasible problem~)')
;endif
end


>>> fp.close()

>>> jlp2._obj_vars = ['x0', 'x1', 'x2', 'x3']
>>> jlp2._write_j_problem()
>>> fp = open('test_problem.txt', 'r')
>>> contents = fp.read()
>>> print contents  
! SUBOBJECTIVE VARIABLES
!   x0 = 0.25 * (sum[1:5](operation:cashflow))
!   x1 = 0.25 * (sum[6:10](operation:cashflow))
!   x2 = 0.25 * (sum[11:15](operation:cashflow))
!   x3 = 0.25 * (sum[16:20](operation:cashflow))

! READ OBJECTIVE AND CONSTRAINT VARIABLE VALUES FROM FILE
udat=data(read->(id,nbranch),in->'test_unitdata.txt')
sdat=data(read->(x0,x1,x2,x3), in->'test_vardata.txt')
linkdata(data->udat,subdata->sdat,nobsw->nbranch)

! OPTIMISATION PROBLEM DEFINITION
pr=problem()

! OBJECTIVE FUNCTION DEFINITION
0.25*sp0+0.25*sl0+0.25*sp1+0.25*sl1+0.25*sp2+0.25*sl2+0.25*sp3+0.25*sl3==min
0.25*x0-sp0+sl0=1000.0
0.25*x1-sp1+sl1=2000.0
0.25*x2-sp2+sl2=3000.0
0.25*x3-sp3+sl3=4000.0
/
jlp(problem->pr,data->udat,z->,report->'...jlp_output.txt')
branch_report=trans()
do(i,1,weights())
write('...bresult.txt',$,matrix%udat(unit(i),1),schedw(i),price%unit(unit(i)))
enddo
/
weight_report=trans()
do(i,1,partweights())
write('...wresult.txt',$,partunit(i),partschedw(i),partweight(i))
enddo
/
;if(Feasible);then
call(branch_report)
call(weight_report)
;else
write('...bresult.txt','(~infeasible problem~)')
;endif
end

def _write_j_data(self):

Write the optimisation data to two data files for J. The unit data contains the simulation unit id and number of simulated alternatives for each unit. The variable contains the objective and constraint variable values for each alternative in each simulation unit:

>>> omatrix2.task_def = taskdef
>>> jlp._data = omatrix2
>>> jlp._write_j_data()  
Called OMatrix._evaluator.evaluate(...)

>>> fp = open('test_unitdata.txt', 'r')
>>> print fp.read()
0,2
1,5
2,3
3,4
4,1

>>> fp.close()
>>> fp = open('test_vardata.txt', 'r')
>>> print fp.read()
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69
1.2345,1.2345,4.69,4.69,4.69,4.69,4.69,4.69,4.69,4.69

>>> fp.close()