Can anyone help someone brand new to AMPL? - ampl

Brand new to AMPL and don't understand the error at all. I've tried removing the variable definitions and get the error (no variables identified) so anyway here is the error message:
syntax error
context: subject to >>> 90 <<< * x1 + 120 * x2 + 106 * x3 + 97 * x4 + 130 * x5 + 180 * x6 >= 300;
here is my code
reset;
var x1 >=0;
var x2 >=1;
var x3 >=0;
var x4 >=0;
var x5 >=.5;
var x6 >=0;
minimize cost: 2 * x1 + 3.5 * x2 + 8 * x3 + 1.5 * x4 + 11 * x5 + x6;
subject to 90 * x1 + 120 * x2 + 106 * x3 + 97 * x4 + 130 * x5 + 180 * x6 >= 300;
subject to 4 * x1 + 8 * x2 + 7 * x3 + 1.3 * x4 + 8 * x5 + 9.2 * x6 >= 10;
subject to 15 * x1 + 11.7 * x2 + .4 * x3 + 22.6 * x4 + 17 * x6 >= 10;
subject to x1 + 5 * x2 + 9 * x3 + 0.1 * x4 + 7 * x5 + 7 * x6 >= 8;

You are experiencing a syntax error because you are not using a valid syntax to declare your constraints. More specifically, your constraints are missing a name and a colon. So, instead of
subject to 90 * x1 + 120 * x2 + 106 * x3 + 97 * x4 + 130 * x5 + 180 * x6 >= 300;
you should write
subject to c1: 90 * x1 + 120 * x2 + 106 * x3 + 97 * x4 + 130 * x5 + 180 * x6 >= 300;
where c1 is whatever name you want to give to that constraint --- just don't use the same name twice.
You can find more info about AMPL's syntax here.

Related

Block If without End If during the barrier option calculation

I am not able to compute the Barrier Option, because it shows me an error in the first line (where I wrote function). The code is as following. Thank you in advance.
Function UOBarrierOption(S As Double, q As Double, T As Double, X As Double, r As Double, _
sigma As Double, CallPutFlag As String, H As Double, K As Double, phi As Double, eta As Double)
Dim x1 As Double, x2 As Double
Dim y1 As Double, y2 As Double
Dim z As Double, mu As Double, lambda As Double
Dim AA As Double, BB As Double, CC As Double, DD As Double, EE As Double, FF As Double
mu = (r - q - sigma ^ 2 / 2) / (sigma ^ 2)
lambda = Sqr(mu ^ 2 + 2 * r / sigma ^ 2)
x1 = Log(S / X) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
x2 = Log(S / H) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
y1 = (Log(H ^ 2) / S / S) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
y2 = (Log(H / S)) / (sigma * Sqr(T)) + (1 + mu) * sigma * Sqr(T)
z = Log(H / S) / (sigma * Sqr(T)) + lambda * sigma * Sqr(T)
AA = phi * S * Exp(-q * T) * Application.NormSDist(phi * x1) - phi * X * Exp(-r * T) * Application.NormSDist(phi * x1 - phi * sigma * Sqr(T))
BB = phi * S * Exp(-q * T) * Application.NormSDist(phi * x2) - phi * X * Exp(-r * T) * Application.NormSDist(phi * x2 - phi * sigma * Sqr(T))
CC = phi * S * Exp(-q * T) * (H / S) ^ (2 * (mu + 1)) * Application.NormSDist(eta * y1) - phi * X * Exp(-r * T) * (H / S) ^ (2 * mu) * Application.NormSDist(eta * y1 - eta * sigma * Sqr(T))
DD = phi * S * Exp(-q * T) * (H / S) ^ (2 * (mu + 1)) * Application.NormSDist(eta * y2) - phi * X * Exp(-r * T) * (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T))
EE = K * Exp(-r * T) * (Application.NormSDist(eta * x2 - eta * sigma * Sqr(T)) - (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T)))
FF = K * Exp(-r * T) * (Application.NormSDist(-eta * x2 + eta * sigma * Sqr(T)) + (H / S) ^ (2 * mu) * Application.NormSDist(eta * y2 - eta * sigma * Sqr(T)))
If CallPutFlag = "Cdi" Then
If X > H Then
UOBarrierOption = CC + EE
ElseIf X < H Then
UOBarrierOption = AA - BB + DD + EE
End Function
ElseIf CallPutFlag = "Cui" Then
If X > H Then
UOBarrierOption = AA + EE
ElseIf X < H Then
UOBarrierOption = BB - CC + DD + EE
End Function
ElseIf CallPutFlag = "Pdi" Then
If X > H Then
UOBarrierOption = BB - CC + DD + EE
ElseIf X < H Then
UOBarrierOption = AA + EE
End Function
ElseIf CallPutFlag = "Pui" Then
If X > H Then
UOBarrierOption = AA - BB + DD + EE
ElseIf X < H Then
UOBarrierOption = CC + EE
End Function
ElseIf CallPutFlag = "Cdo" Then
If X > H Then
UOBarrierOption = AA - CC + FF
ElseIf X < H Then
UOBarrierOption = BB - DD + FF
End Function
ElseIf CallPutFlag = "Cuo" Then
If X > H Then
UOBarrierOption = F
ElseIf X < H Then
UOBarrierOption = AA - BB + CC - DD + FF
End Function
ElseIf CallPutFlag = "Pdo" Then
If X > H Then
UOBarrierOption = AA - BB + CC - DD + FF
ElseIf X < H Then
UOBarrierOption = F
End Function
ElseIf CallPutFlag = "Puo" Then
If X > H Then
UOBarrierOption = BB - DD + FF
ElseIf X < H Then
UOBarrierOption = AA - CC + FF
End Function
End If
End Function
P.S. I have different "phi"s and "eta"s for different types of option barriers (cdi, pdi and etc.). Right now I am trying different combinations, but it also gives "end if function missing" type of error
If your function returns something you must declare the type returned in the function, and assign the returned value to the function, for exmaple:
Function CalculateSquareRoot(NumberArg As Double) As Double
If NumberArg < 0 Then ' Evaluate argument.
Exit Function ' Exit to calling procedure.
Else
CalculateSquareRoot = Sqr(NumberArg) ' Return square root.
End If
End Function
See the As Double and the CalculateSquareRoot = Sqr(NumberArg). That is what the function returns.
If it does not return anything, and its just a method, you should declare it with Sub().
Sub()
'your method
End Sub

Visual basic function for intermittent antibiotic dosing

I am a beginner in VB. I wrote a little program to simulate dosing regimens of antibiotics using some exponential equations and pharmacokinetic data.
The problem I have is that I want to display on a graph the following mathematical function:
That simulates the concentration variation at different intervals of time:
Where:
b(t) is the concentration at time t that will be plotted as Y axis, t is time (plotted on the x-axis).
b(0) is the concentration at time 0 and it is a known variable.
u(t-a1) is a function that has the value u(t-a1)=b(o) if t=a1 or 0 if t<>a1
a1 is the time at which a next dose is given.
alpha is the elimination rate constant, a variable that is known.
What I have so far:
Dim y, x As Double
For x = 0 To 24 Step 1
For n As Double = 1 To 24 / tau
y = (1 - test_condition(n * tau, x)) * css * Math.Exp(-ke * x) + test_condition(n * tau, x) * css * Math.Exp(-ke * (x - n * tau))
Chart1.Series("Concentratie1").Points.AddXY(x, y)
Next
Next
The test_condition:
if x=tau then test_condition= 1 else 0
It is close but I don't get an exponential decay after a dose ... don't know how to make that happen.
This works!! for tau (dosing interval) every 4 hours. Can it be rearranged somehow because the tau (dosing interval) will vary (sometimes 4 hours, sometimes every 6 hours)?:
Dim y, x, y2, x2, y3, x3, y4, x4, x5, x6, y5, y6 As Double
For x = 0 To tau Step 1
y = exponential_decay(css, ke, x) + test_condition(tau, x) * (css - Val(mic))
Chart1.Series("Bolus 1").Points.AddXY(x, y)
Next
For x2 = tau To 2 * tau Step 1
y2 = exponential_decay(css, ke, x2 - tau) + test_condition(2 * tau, x2) * (css - Val(mic))
Chart1.Series("Bolus 2").Points.AddXY(x2, y2)
Next
For x3 = 2 * tau To 3 * tau Step 1
y3 = exponential_decay(css, ke, x3 - 2 * tau) + test_condition(3 * tau, x3) * (css - Val(mic))
Chart1.Series("Bolus 3").Points.AddXY(x3, y3)
Next
For x4 = 3 * tau To 4 * tau Step 1
y4 = exponential_decay(css, ke, x4 - 3 * tau) + test_condition(4 * tau, x4) * (css - Val(mic))
Chart1.Series("Bolus 4").Points.AddXY(x4, y4)
Next
For x5 = 4 * tau To 5 * tau Step 1
y5 = exponential_decay(css, ke, x5 - 4 * tau) + test_condition(5 * tau, x5) * (css - Val(mic))
Chart1.Series("Bolus 4").Points.AddXY(x5, y5)
Next
For x6 = 5 * tau To 32 Step 1
y6 = exponential_decay(css, ke, x6 - 5 * tau)
Chart1.Series("Bolus 4").Points.AddXY(x6, y6)
Next
End Sub
I managed to solve the problem:
this function f relates time (t) to dosing interval (tau)
Private Function f(ByVal t As Double, ByVal tau As Double)
Dim n As Integer
For n = 0 To 24 / tau
If t = n * tau Then
f = n * tau
ElseIf t < tau Then
f = 0
ElseIf t > n * tau And t < (n + 1) * tau Then
f = n * tau
ElseIf t >= (n + 1) * tau Then
f = n * tau
End If
Next
End Function
And this is what I draw on the chart:
For x = 0 To 36 Step 0.5
y = exponential_decay(css, ke, x - f(x, tau))
Chart1.Series("Intermitent Dosage").Points.AddXY(x, y)
Next

how to fix the coordinates of the lines?

I have two lines - the first one is straight horizontal line with x1y1 as start point and x2y2 as end point. There is another line with start point as x1y1 and end point as x3y3.
Is there any way that I can fix the coordinate x1y1 of the lines so that if I rotate the second line the point x1y1 is not detached?
I tried grouping the lines but it didn't work.
Set p1 = ActiveSheet.Shapes.AddConnector(msoConnectorStraight, x1, y1, x2, y2)
p1.Select
Selection.ShapeRange.Line.BeginArrowheadStyle = msoArrowheadOval
Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOval
Set p2 = ActiveSheet.Shapes.AddConnector(msoConnectorStraight, x1, y1, x3, y3)
p2.Select
Selection.ShapeRange.Line.BeginArrowheadStyle = msoArrowheadOval
Selection.ShapeRange.Line.EndArrowheadStyle = msoArrowheadOval
Dim R As Variant
Set R = ActiveSheet.Shapes.Range(Array(p1.Name, p2.Name))
R.Group
Problem and code
If I understood correctly, you want to input an angle and to obtain the coordinates of the point (x3,y3) to redraw a line.
The solution can be done on the coordinates x3 and y3, since, as #SJR said "Rotation is around the midpoint of the line". So you need to use geometry to do it.
Using the Law of Sines code on Math.Stackexchange answered by Jean Marie, the following code can be done:
'Initial Values
x1 = 100
y1 = 100
x2 = 300
y2 = 100
DesiredAngle = 45
'Find coordinates
Angle1 = Application.WorksheetFunction.Radians(DesiredAngle)
Angle2 = Application.WorksheetFunction.Radians((180 - DesiredAngle) / 2)
Deltax = x2 - x1
Deltay = y2 - y1
a3 = Sqr(Deltax ^ 2 + Deltay ^ 2)
Angle3 = Application.WorksheetFunction.Pi() - Angle1 - Angle2
a2 = a3 * Sin(Angle2) / Sin(Angle3)
RHS1 = x1 * Deltax + y1 * Deltay + a2 * a3 * Cos(Angle1)
RHS2 = y2 * Deltax - x2 * Deltay + a2 * a3 * Sin(Angle1)
x3 = (1 / a3 ^ 2) * (Deltax * RHS1 - Deltay * RHS2)
y3 = (1 / a3 ^ 2) * (Deltay * RHS1 + Deltax * RHS2)
Debug.Print x3 & " " & y3
'Draw Lines
Set Line1 = ActiveSheet.Shapes.AddLine(x1, y1, x2, y2)
Set Line2 = ActiveSheet.Shapes.AddLine(x1, y1, x3, y3)
'Verify angle to know if it worked
'Method1 to obtain angle of 3 points
alpha = Application.WorksheetFunction.Atan2((y2 - y1), (x2 - x1))
beta = Application.WorksheetFunction.Atan2((y3 - y1), (x3 - x1))
Debug.Print Application.WorksheetFunction.Degrees(beta - alpha)
'Method2
m1 = (y2 - y1) / (x2 - x1)
m2 = (y3 - y1) / (x3 - x1)
Debug.Print Application.WorksheetFunction.Degrees(Atn((m1 - m2) / (1 + m1 * m2)))
'Check Length
Debug.Print Sqr((x3 - x1) ^ 2 + (y3 - y1) ^ 2)
On the code, the example is that the initial value is a line as you drew and after inputting the DesiredAngle, a line is drawn with this angle, with the new x3 and y3 coordinates.
Result
On the Result, the example uses a DesiredAngle of 45°.
Further References
You can refer to many questions about this on Math.Stackexchange, like this, this, this.
EDIT:
To test it, you can make a simple For loop and check that a circle is made, i.e., the circle radius is the same length:
'Initial Values
x1 = 500
y1 = 300
x2 = 700
y2 = 300
For i = 1 To 360
On Error Resume Next
DesiredAngle = i
'Find coordinates
Angle1 = Application.WorksheetFunction.Radians(DesiredAngle)
Angle2 = Application.WorksheetFunction.Radians((180 - DesiredAngle) / 2)
Deltax = x2 - x1
Deltay = y2 - y1
a3 = Sqr(Deltax ^ 2 + Deltay ^ 2)
Angle3 = Application.WorksheetFunction.Pi() - Angle1 - Angle2
a2 = a3 * Sin(Angle2) / Sin(Angle3)
RHS1 = x1 * Deltax + y1 * Deltay + a2 * a3 * Cos(Angle1)
RHS2 = y2 * Deltax - x2 * Deltay + a2 * a3 * Sin(Angle1)
x3 = (1 / a3 ^ 2) * (Deltax * RHS1 - Deltay * RHS2)
y3 = (1 / a3 ^ 2) * (Deltay * RHS1 + Deltax * RHS2)
Debug.Print x3 & " " & y3
'Draw Lines
Set Line1 = ActiveSheet.Shapes.AddLine(x1, y1, x2, y2)
Set Line2 = ActiveSheet.Shapes.AddLine(x1, y1, x3, y3)
Next i

NLopt error in Julia and Ipopt alternative

I have the following simple problem that I want to solve with NLopt:
using JuMP
using NLopt
"""
min_x = x1 * x4* (x1 + x2 + x3) + x3
s.t.
x1 * x2 * x3 * x4 >= 25
x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40
1 <= x1,x2,x3,x4 <= 5
starting values: vec(x) = (x1 = 1, x2 = 5, x3 = 5, x4 = 1)
"""
tic()
m = Model(solver=NLoptSolver(algorithm=:LD_MMA))
#defVar(m, 1 <= x1 <= 5)
#defVar(m, 1 <= x2 <= 5)
#defVar(m, 1 <= x3 <= 5)
#defVar(m, 1 <= x4 <= 5)
#setNLObjective(m, Min, x1 * x4 * (x1 + x2 + x3) + x3)
#addNLConstraint(m, x1^2 + x2^2 + x3^2 + x4^2 == 40)
#addNLConstraint(m, x1 * x2 * x3 * x4 >= 25)
setValue(x1, 1)
setValue(x2, 5)
setValue(x3, 5)
setValue(x4, 1)
status = solve(m)
println("got ", getObjectiveValue(m), " at ", [getValue(x1),getValue(x2), getValue(x3), getValue(x4)])
toc()
However I get an argument error. Is there any way to make this work with NLopt and if not how this code can change so as to use it with the other free optimizers that can be installed in Julia (maybe Ipopt but not Gurobi)?
Well, I was unable to solve the problem using NLopt, but instead I managed to solve it with Ipopt.
The solution is simple for using Ipopt. Firstly you have to download Ipopt (I used the Windows version for now and I will also try in Linux) from this site and put it in the path (if you put it in the path and go to the command line and type ipopt it must show no error-it will actually show ipopt options). Just go at the very end to find the newest version.
Then I sliglty modified the code that I provided before to account for Ipopt in this way:
using JuMP
using Ipopt
"""
The problem that I want to solve has 4 variables and 6 constraints.
It is the following:
min_x = x1x4(x1+x2+x3) + x3
s.t.
x1*x2*x3*x4 >= 25
x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40
1 <= x1,x2,x3,x4 <= 5
starting values: x0 = (x1 = 1, x2 = 5, x3 = 5, x4 = 1)
"""
tic()
m = Model(solver=IpoptSolver())
#defVar(m, 1 <= x1 <= 5)
#defVar(m, 1 <= x2 <= 5)
#defVar(m, 1 <= x3 <= 5)
#defVar(m, 1 <= x4 <= 5)
#setNLObjective(m, Min, x1 * x4 * (x1 + x2 + x3) + x3)
#addNLConstraint(m, x1^2 + x2^2 + x3^2 + x4^2 == 40)
#addNLConstraint(m, x1 * x2 * x3 * x4 >= 25)
setValue(x1, 1)
setValue(x2, 5)
setValue(x3, 5)
setValue(x4, 1)
status = solve(m)
println("got ", getObjectiveValue(m), " at ", [getValue(x1),getValue(x2),
getValue(x3), getValue(x4)])
toc()
More information about the right name of the solvers etc. can be found here: https://jump.readthedocs.org/en/latest/installation.html#getting-solvers

how to calculate (A*B*C)%10000007 where A,B,C can be at maximum 10^18

how to calculate (A*B*C)%10000007 where A,B,C can be maximum 10^18
Let I = 10000007, so
A = n1 * I + X1
B = n2 * I + X2
C = n3 * I + X3
A * B => (n1 * I + X1) (n2 * I + X2) => n1 * n2 * I^2 + n1 * X2 * I + n2 * X1 * I + X1 * X2
Only X1 * X2 can't div by I
Hence, A * B % I === X1 * X2 % I === (A % I) * (B % I) % I
Therefore (A * B * C) % I === [(A % I) * (B % I) % I] * (C % I) % I