Multiple dimensional sampling with first element string

Yes I agree.

I am trying to see how to get the correct structure of x_train, which should be (string, int64) if I’m correct? It should not be a Tuple{String,Int64} if I am correct.

x_train, y_train will pass through into the following:

sgt = NeuralSurrogate(x_train, y_train, bounds..., model=model, loss=loss, opt=optimizer, n_echos=n_epochs)

I’m not sure how to achieve that, but I can say that even if d is a Tuple{String, Int64}, you cannot index into it with d[s,n], only a single index can be used, and that index must be either 1 or 2, since they are the only valid indices in a tuple of length 2.

Edit: Well, to be honest, you can also index with a vector. For example, d[1:2], or d[[2,1]] will work.

Okay it appears you are roughly trying to follow the Neural network tutorial?

I don’t know much about JuMP or Surrogates, but in that tutorial it seems to me like they have an objective function schaffer which takes an input x and produces an output y, which you don’t seem to have. Is g supposed to be your objective function? It’s not defined in a way such that g(x) produces on objective value, so g.(x_train) won’t work pretty much irrespective of how you define x_train.

Thank you for this comment. I did not use that tutorial, it is my 1st time seeing it.

But yes g is the objective function. I previously attempted to try and do a taylor expansion of g so that I use that as a replacement. I was not successful with that. Is there a another method you could propose that will give me a different form of g?

I don’t think I quite understand what you are proposing?

I don’t think it will be achievable because the sampling methods still return tuples from what I just read. It still will not work in y_train no matter what x_train is.

However, thank you so much for your assistance and commitment to assisting me.

It’s not a proposal, I was just correcting my claim that you can only index with a single number. It is still not possible to index with a two-dimensional index.

Your problem appears to be that this function

g = d -> @objective m Max begin
    sum((price[s]*d[s,n]) for n in N for s in S)
end

is simply not correctly defined.

Here is the original function. Did I define it correctly?

eqn

But to call this function, the input to g must be a matrix, not a tuple. And also, you seem to try to index with a string, which won’t work, anyway. Indexing with a string can work for dicts, but they are not 2-dimensional.

So what is d supposed to be here?

d is a variable that is described as the amount of material (s) at a point in time (n).

x-ref: ArgumentError: invalid index: "s1" of type String

@GraceMabele you need to step back and re-consider what you are trying to do.

g = d -> @objective m Max begin
    sum((price[s]*d[s,n]) for n in N for s in S)
end

is a JuMP objective. it doesn’t make sense to try and fit a surrogate model to this, regardless of which method you chose.

It seems you really want something like

const S = ["s1","s2","s3","s4"]
const N = 1:10

const price = Dict(
    "s1" => 0,
    "s2" => 0,
    "s3" => 0,
    "s4" => 5
)

function objective(d::Matrix{<:Real})
    return sum(price[s] * d[i, n] for n in N for (i, s) in enumerate(S))
end

objective(rand(4, 10))  # Objective given a random demand

and now your take is to approximate objective given a 4x10 input matrix.

Here’s something to get you started

function objective(d::NTuple)
    return objective(reshape(collect(d), length(S), length(N)))
end

lb = fill(0.0, 40)
ub = fill(1.0, 40)
x_train = sample(10, lb, ub, SobolSample())
y_train = objective.(x_train)

However, I would again caution you. If you can write down the JuMP objective, why are you trying to fit a surrogate?

1 Like

I suspected this but wasn’t sure whether JuMP objectives would work with surrogates or not.

While I think Oscar’s suggestion is spot on (basically decide whether you just want to solve a simple maximisation problem in JuMP or fit a neural surrogate to a non JuMP objective), I would also note that your current problem seems at a minimum to be lacking constraints, if I’m reading it correctly the solution will always be the highest possible value for d.

Also, again if I’m reading correctly, your profit is price times demand (presumably quantity demanded?), and seems to not consider costs? It is also a bit unusual to use demand as the choice variable here, when we would usually think of a firm facing a demand schedule and choosing prices to maximise profits. Now in general the firm’s optimisation problem can of course be formulated as an optimal quantity (supply) choice,but I would expect some link between that quantity choice and the equilibrium price?

@odow

I see the assumption that this is the whole model.

The complete model is a MILP(of a batch chemical process). I have omitted the rest of the model. The model I am using is not formulated in that way.
The values of d are dependent on the prementioned constraints.

Hence I cannot enumerate S in this case.

This is more of what I am trying to do.

Unfortunately cannot provide full details of my work here since it a public platform.