# Plotting a maximization of a definite integral

I am applying Alex Trounev's approach suggested in this post. It worked for the function in that post. But it is now not working for another, more complicated function.

My objective function is

$$f=\int _{\frac{d-k r}{1-r}}^k\int _{\frac{d+r x-x}{r}}^k\text{AA}dydx+\int _{\frac{d-k r}{1-r}}^k\int _k^s\text{BB}dydx+\int _k^{d+k r}\int _{\frac{d+k r-x}{r}}^k\text{CC}dydx+\int _k^{d+k r}\int _k^s\text{DD}dydx+\int _{d+k r}^s\int _0^k\text{EE}dydx+\int _{d+k r}^s\int _k^s\text{FF}dydx$$

where

$$\text{AA}=\frac{(1-t) (-J+(1-r) x+r y)-(d-t)}{s^2}$$ if $$AA>0$$, while $$AA=0$$ if $$AA \leq 0$$

$$\text{BB}=\frac{(1-t) (-J+k r+(1-r) x)-(d-t)}{s^2}$$ if $$BB>0$$, while $$BB=0$$ if $$BB \leq 0$$

$$\text{CC}=\frac{(1-t) (-J+r (x-k)+(1-r) x+r y)-(d-t)}{s^2}$$ if $$CC>0$$, while $$CC=0$$ if $$CC \leq 0$$

$$\text{DD}=\frac{(1-t) (-J+r (x-k)+k r+(1-r) x)-(d-t)}{s^2}$$ if $$DD>0$$, while $$DD=0$$ if $$DD \leq 0$$

$$\text{EE}=\frac{(1-t) (-J+r (x-k)+(1-r) x+r y)-(d-t)}{s^2}$$ if $$EE>0$$, while $$EE=0$$ if $$EE \leq 0$$

$$\text{FF}=\frac{(1-t) (-J+r (x-k)+k r+(1-r) x)-(d-t)}{s^2}$$ if $$FF>0$$, while $$FF=0$$ if $$FF \leq 0$$

$$J=c \left(\frac{(q-1) \left(k^2 r ((r-3) r+1)+\frac{8 k r^2}{5}+\frac{16}{25} (1-2 r)\right)}{4 (r-1)^2}+\frac{\frac{8}{5} k (r-1)+k (k (r-3) r+k+4 r)-\frac{64}{25}}{8 (r-1)}\right)-\frac{\frac{8}{5} k (r-1)+k (k (r-3) r+k+4 r)-\frac{64}{25}}{40 (r-1)}$$

I would like to maximize my objective function $$f$$ with respect to $$r$$ and $$k$$ and Plot3D them, i.e. the maximum value of $$f$$, and the corresponding $$r$$ and $$k$$, against $$c \in [0,1]$$ and $$q \in [1,2]$$ under the following parameter values and constraints: $$s=2$$, $$d=0.8$$, $$t=0$$, $$d \leq k \leq 2$$, $$0 \leq r , $$0\leq c \leq 1$$, $$1 \leq q \leq 2$$, $$cq \leq 1$$.

My Mathematica Code is as follows:

s = 2; d = 4/5; t = 0;
J=-((-(64/25) + 8/5 k (-1 + r) + k (k + 4 r + k (-3 + r) r))/(40 (-1 + r))) + c (((-1 + q) (16/25 (1 - 2 r) + (8 k r^2)/5 + k^2 r (1 + (-3 + r) r)))/(4 (-1 + r)^2) + (-(64/25) + 8/5 k (-1 + r) + k (k + 4 r + k (-3 + r) r))/(8 (-1 + r)));
AA = (((1 - r)*x + r*y - J)*(1 - t) - (d - t))/s^2;
BB = (((1 - r)*x + r*k - J)*(1 - t) - (d - t))/s^2;
CC = (((1 - r)*x + r*(x - k) + r*y - J)*(1 - t) - (d - t))/s^2;
DD = (((1 - r)*x + r*(x - k) + r*k - J)*(1 - t) - (d - t))/s^2;
EE = (((1 - r)*x + r*(x - k) + r*y - J)*(1 - t) - (d - t))/s^2;
FF = (((1 - r)*x + r*(x - k) + r*k - J)*(1 - t) - (d - t))/s^2;
regAA = ImplicitRegion[AA > 0 && (d - r*k)/(1 - r) <= x <= k && (d + r*x - x)/r <= y <= k, {x, y}];
regBB = ImplicitRegion[BB > 0 && (d - r*k)/(1 - r) <= x <= k && k <= y <= s, {x, y}];
regCC = ImplicitRegion[CC > 0 && k <= x <= d + r*k && (d + r*k - x)/r <= y <= k, {x, y}];
regDD = ImplicitRegion[DD > 0 && k <= x <= d + r*k && k <= y <= s, {x, y}];
regEE = ImplicitRegion[EE > 0 && d + r*k <= x <= s && 0 <= y <= k, {x, y}];
regFF = ImplicitRegion[FF > 0 && d + r*k <= x <= s && k <= y <= s, {x, y}];
f = Integrate[AA, {x, y} \[Element] regAA] + Integrate[BB, {x, y} \[Element] regBB] + Integrate[CC, {x, y} \[Element] regCC] + Integrate[DD, {x, y} \[Element] regDD] + Integrate[EE, {x, y} \[Element] regEE] + Integrate[FF, {x, y} \[Element] regFF];
max = Flatten[Table[{c, q, MaxValue[{f, 0 <= c <= 1, 1 <= q <= 2, c*q <= 1, d <= k <= 2, 0 <= r*k < d}, {k, r}]}, {c, 0, 1, .1}, {q, 1, 2, .1}], 1];
maxk = Flatten[Table[{c, q, k /. Last@Maximize[{f, 0 <= c <= 1, 1 <= q <= 2, c*q <= 1, d <= k <= 2, 0 <= r*k < d}, {k, r}]}, {c, 0, 1, .1}, {q, 1, 2, .1}], 1];
maxr = Flatten[Table[{c, q, r /. Last@Maximize[{f, 0 <= c <= 1, 1 <= q <= 2, c*q <= 1, d <= k <= 2, 0 <= r*k < d}, {k, r}]}, {c, 0, 1, .1}, {q, 1, 2, .1}], 1];
{ListPlot3D[max, AxesLabel -> {"c", "q", "f"}], ListPlot3D[maxk, PlotRange -> {0, 2}, AxesLabel -> {"c", "q", "k"}], ListPlot3D[maxr, PlotRange -> {0, 1}, AxesLabel -> {"c", "q", "r"}]}


This code ran forever, so I had to abort it, and I got the following list of error messages along with some diagram results, which seem incomplete.

• Running forever? If you evaluate all the lines other than the last, does it terminate? – mjw Jul 16 '19 at 17:39
• Crossposted here. – Rohit Namjoshi Jul 16 '19 at 17:44
• @Rohit Namjoshi: Yes, as it was urgent, I am seeking help everywhere possible. Please let me know if I have to delete one of them. – ppp Jul 16 '19 at 17:47
• @mjw: In fact, when I stopped running, I got some messages and graph results. I have added them in the post. – ppp Jul 16 '19 at 17:48
• You could try scaling down the problem (less gridpoints), check the timing, and then estimate how long it will take for the larger problem. – mjw Jul 16 '19 at 18:12