AMPL error from OutputHandler

Hi,
I am using AMPL model and data file while running the problem in python. Actually it’s a program run by AIMSUN API in their simulator. When I start the program, I am getting the error of:

Python Error (<class ‘RuntimeError’>): SWIG director method error. Error detected when calling ‘OutputHandler.output’

This error is occuring while running ampl.solve() line in python.

I tried to run simple program yet no success!

Could you please help me why this error is arising? and how can I tackle it?

Best wishes!

Hi @Fateh,

Thank you for reaching out. It looks like something is throwing an exception in an output handler. Could you please upgrade to the version 0.15.0b0 and test again?

python -m pip install "amplpy>=0.15.0b0" --upgrade

This version may be able to provide more information on what is going on in the output handler function.

1 Like

Hi,

Now, I am getting this as an error: Python Error (<class ‘SystemError’>): <cyfunction AMPL.solve at 0x000001FA28F1A0C0> returned a result with an exception set

Is there any remedy for that?

Best regards,
Haris

Can you share with us the program you are trying to run so that we can try to reproduce the issue?

Yes sure.

Here’s the simple program I am trying to run in the AIMSUN NEXT API:

mod file:
"set CITIES;
set LINKS within (CITIES cross CITIES);

param supply {CITIES} >= 0; # amounts available at cities
param demand {CITIES} >= 0; # amounts required at cities

check: sum {i in CITIES} supply[i] = sum {j in CITIES} demand[j];

param cost {LINKS} >= 0; # shipment costs/1000 packages
param capacity {LINKS} >= 0; # max packages that can be shipped

var Ship {(i,j) in LINKS} >= 0, <= capacity[i,j];
# packages to be shipped

minimize Total_Cost:
sum {(i,j) in LINKS} cost[i,j] * Ship[i,j];

subject to Balance {k in CITIES}:
supply[k] + sum {(i,k) in LINKS} Ship[i,k]
= demand[k] + sum {(k,j) in LINKS} Ship[k,j];"

dat file:
"data;

set CITIES := PITT NE SE BOS EWR BWI ATL MCO ;

set LINKS := (PITT,NE) (PITT,SE)
(NE,BOS) (NE,EWR) (NE,BWI)
(SE,EWR) (SE,BWI) (SE,ATL) (SE,MCO);

param supply default 0 := PITT 450 ;

param demand default 0 :=
BOS 90, EWR 120, BWI 120, ATL 70, MCO 50;

param: cost capacity :=
PITT NE 2.5 250
PITT SE 3.5 250

NE BOS 1.7 100
NE EWR 0.7 100
NE BWI 1.3 100

SE EWR 1.3 100
SE BWI 0.8 100
SE ATL 0.2 100
SE MCO 2.1 100 ;

And the API of AIMSUN is:
from AAPI import *

def AAPILoad():
return 0

def AAPIInit():
return 0

def AAPISimulationReady():
return 0

def AAPIManage(time, timeSta, timeTrans, acycle):
return 0

def AAPIPostManage(time, timeSta, timeTrans, acycle):
return 0

def AAPIFinish():
return 0

def AAPIUnLoad():
return 0

def AAPIPreRouteChoiceCalculation(time, timeSta):
return 0

def AAPIEnterVehicle(idveh, idsection):
return 0

def AAPIExitVehicle(idveh, idsection):
return 0

def AAPIEnterPedestrian(idPedestrian, originCentroid):
return 0

def AAPIExitPedestrian(idPedestrian, destinationCentroid):
return 0

def AAPIEnterVehicleSection(idveh, idsection, atime):
return 0

def AAPIExitVehicleSection(idveh, idsection, atime):
return 0

def AAPIVehicleStartParking(idveh, idsection, time):
return 0

I am running program inside AAPIManage function!

Could you please send us the part of the code where amplpy is used as well?

It looks like AAPI closes stdout and it blocks all output. Doing the following before instantiating the AMPL object should fix the issue.

sys.stdout = open(os.devnull, "w")
1 Like

Yes, indeed it fixed the issue, but now I am unable to do any print in the API code!

Is there any remedy for that?

I am using this piece of code for ampl, in AAPIManage():

from amplpy import AMPL

def AAPIManage(time, timeSta, timeTrans, acycle):
    ampl = AMPL()
    
    solver = "gurobi"
    
    ampl.set_option("solver", solver)
    
    ampl.read(r"Z:\Desktop\exp\net.mod")
    ampl.read_data(r"Z:\Desktop\exp\net.dat")
    
    ampl.solve()

return 0

I used this method, it solves the issue exactly as I needed:

import contextlib

with contextlib.redirect_stdout(None):
    print("will not print")
1 Like

Thanks a lot @fdabrandao :cowboy_hat_face: :innocent:

1 Like