Google colab model with binary variables executed in Anvil

I have a Google Colab model and an Anvil APP that sends data from Datatables to a function in the model and receives the result of the model:

result = anvil.server.call(‘programming’, times_list, cambprod_list, limpprof_list, machines_list)

The model uses binary variables:

I declare them like:

y = [[[model.add_var(var_type=BINARY, name=‘y({},{},{})’.format(j+1, k+1, i+1)) for i in range(m)] for k in range(n)] for j in range(n)]

I use them like:

model += x[j][i] - x[k][i] + M*y[j][k][i] >= times[k][i]

When I execute the model only in Google colab interface, all works, but when I call the function in Anvil, I have the following error:

TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’

I thought that the model was always executed in Google and Anvil would just receive the final result like a black box but here it seems that Anvil reads the model?

How can I solve this situation?

Regards

Clone link:
share a copy of your app

It may look identical to code that you normally run in Google Colab (or elsewhere). But if I put it in my Anvil Server Module, and call that function, then it executes on an Anvil Server, instead, and uses the semantics defined there.

Things can get dicey if you have multiple anvil.server.callable functions with the same name, at different locations, e.g., in your Anvil Server code, and in multiple Uplink programs (like ones running in Google Colab). As I understand it, Anvil will try the remote (Uplink) versions first, but if they’re not connected at that moment, then it will fall back to the one in your Server Module.

Just to be sure of the answer… I don´t copy the google colab model into Anvil but just use an uplink to the Google colab model. Then I call a model function with an anvil.server.callable function that sends some parameters and should receive the result, using Google Colab just as a black box. Anvil should not run the model, just send data and receive data. Is it possible? If not, then I have to rewrite my model to fit with Anvil? This can be hard I imagine.

Anvil sending and receiving data, and Colab receiving and sending data, has been proven to work before, provided that you send and receive only the data types that Anvil knows how to send and receive.

It is clear that some of your data values are not of the type your code was expecting. To determine why, someone has to identify and examine the code that prepared those values. If we are to help, then we will need to see that code.

Hi… thank you for your time.

I just made a very simple test… the Google Colab function now just returns a single value d:

d= 8

I put the result in different positions of the code
return(d)

and I receive the 8 in Anvil.

Even after declaring the binary variable “y”:

y = [[[model.add_var(var_type=BINARY, name=‘y({},{},{})’.format(j+1, k+1, i+1)) for i in range(m)] for k in range(n)] for j in range(n)]

return(d)

It stills returns the 8.

But when I use the “y” binary variable y and put the return(d) after this line:

for (j, k) in product(range(n), range(n)):
for i in range(m):
model += x[j][i] - x[k][i] + M*y[j][k][i] >= times[k][i]
return(d)

The error appears:

TypeError: unsupported operand type(s) for +: ‘int’ and ‘list’

Anvil is reading the code even if I don’t return the “y” value… Google Colab is not just a Black Box for Anvil… Google Colab is not just executed in Google but Anvil also works with the fucntion equations if I am not wrong. If so, shoud I make changes in the code to fit to Anvil code? not an easy task.

Hope the example is clear.

Regards

No, any code in your Uplink function is running in your Uplink program, not on the Anvil Server.

But you have found a way to narrow down the location of your coding error. Think about it: any code after the return doesn’t get executed, so it doesn’t get a chance to encounter the problem.

It’s time to start checking the type of value that each variable and subexpression has, both in the case where the code fails, and where it works.

You’re going to see differences between the fails cases and the works cases. Then you can try to figure out why those differences are occurring. It may be that the module containing your anvil.server.callable function is being initialized differently in those cases.

1 Like