Whats wrong with the following script http://rise4fun.com/Z3Py/Cbl?
Adding the last two lines gives me the following error 'instancemethod' object is not #subscriptable
x,t,t1,t2,x_next=Reals ('x t t1 t2 x_next')
location,location_next=Bools('location location_next')
x=0
location=BoolVal(False)
k=20
for i in range(k):
s=Solver()
s.add(Or(And(x_next-x>=2*t,x_next-x<=3*t,x_next<=12,t>0,Not(location)),
And(x_next-x>=-2*t,x_next-x<=-t,x_next>=0,t>0,location)),location_next==If(And(Not(location),x_next>=12),True,If(And(location,x_next<=0),False,location)))
print s.check()
print s.model()
m=s.model
#x=m[x_next]
#location=m[location_next]
m=s.model assigns a function to m. I believe you meant to assign the result of calling s.model, so include parenthesis to make the call:
m=s.model()
Related
I implemented the bubble sort method in Maxima as:
burbuja(l) := block(
for i : 1 step 1 thru length(l) do (
for j : length(l) - 1 step -1 while j >= i do (
if l[j] > l[j + 1] then (
elemento : l[j],
l[j] : l[j + 1],
l[j + 1] : elemento
)
)
), print(l)
);
The problem is that the output is something like this:
(%i1) burbuja([3,2,1]);
[1, 2, 3]
(%o1) [1, 2, 3]
If I remove print(l) from the penultimate line, this is the output:
(%i1) burbuja([3,2,1]);
(%o1) done
The question is how can I print the result without duplicating it?
Thanks.
Instead of print(l) just put l to return the value of l from the function. (Do not put return(l) as the effect of return is actually somewhat different from other languages.)
I also recommend copying the list before sorting it, so that you don't modify the original list. Try l : copy(l) before anything else.
As a complement of Robert Dodier's answer, some more words for explaining what is happening. Maxima has a functional language with following features:
last term in a block is also the value "returned" by this block;
print has to be taken as a function rather than a mere statement.
These two ideas explain what happend in your case:
the print(l) in your code performs something on your screen but also is an expression evaluating to l (try for instance something like a:print(5)$ which will print the number 5 but also assign the value 5 to the variable a;
since print(l) is also the last term in your block, your block will evaluate to l.
Thus your code prints l and also "return" it.
I found the problem. Instead of print(l) I should use append(l).
I was trying to get an hologram projector working, but in run into these errors:
bad arguments #3 (number expected, got no value)
My script is:
local component = require("component")
local hologram = component.hologram
function setVoxel(x, y, z, value)
print(x)
print(y)
print(z)
print(value)
local current = hologram.get(x, z)
local positiveMask = bit32.lshift(1, y - 1)
if value then
hologram.set(x, z, bit32.bor(current, positiveMask))
else
local negativeMask = bit32.bnot(positiveMask)
hologram.set(x, z, bit32.band(current, negativeMask))
end
end
local args = {...}
print(args[1])
print(args[2])
print(args[3])
print(args[4])
setVoxel(tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), args[4])
I used:
holo-set 8 16 20 true
The print commands returned:
8
16
20
true
but its not working.
I have checked the spelling.
Also the hologram is correctly initialized.
That error means some function (what's the rest of the error?) which expected to get three arguments only got two.
Given that code snippet the only function I can see to which that might apply is hologram.get.
Which, given a quick look at the documentation (thank you Google), does in fact appear to require three arguments.
get(x:number, y:number, z:number):number
Returns the value at the specified position.
I am trying to solve a problem, for example I have a 4 point and each two point has a cost between them. Now I want to find a sequence of nodes which total cost would be less than a bound. I have written a code but it seems not working. The main problem is I have define a python function and trying to call it with in a constraint.
Here is my code: I have a function def getVal(n1,n2): where n1, n2 are Int Sort. The line Nodes = [ Int("n_%s" % (i)) for i in range(totalNodeNumber) ] defines 4 points as Int sort and when I am adding a constraint s.add(getVal(Nodes[0], Nodes[1]) + getVal(Nodes[1], Nodes[2]) < 100) then it calls getVal function immediately. But I want that, when Z3 will decide a value for Nodes[0], Nodes[1], Nodes[2], Nodes[3] then the function should be called for getting the cost between to points.
from z3 import *
import random
totalNodeNumber = 4
Nodes = [ Int("n_%s" % (i)) for i in range(totalNodeNumber) ]
def getVal(n1,n2):
# I need n1 and n2 values those assigned by Z3
cost = random.randint(1,20)
print cost
return IntVal(cost)
s = Solver()
#constraint: Each Nodes value should be distinct
nodes_index_distinct_constraint = Distinct(Nodes)
s.add(nodes_index_distinct_constraint)
#constraint: Each Nodes value should be between 0 and totalNodeNumber
def get_node_index_value_constraint(i):
return And(Nodes[i] >= 0, Nodes[i] < totalNodeNumber)
nodes_index_constraint = [ get_node_index_value_constraint(i) for i in range(totalNodeNumber)]
s.add(nodes_index_constraint)
#constraint: Problem with this constraint
# Here is the problem it's just called python getVal function twice without assiging Nodes[0],Nodes[1],Nodes[2] values
# But I want to implement that - Z3 will call python function during his decission making of variables
s.add(getVal(Nodes[0], Nodes[1]) + getVal(Nodes[1], Nodes[2]) + getVal(Nodes[2], Nodes[3]) < 100)
if s.check() == sat:
print "SAT"
print "Model: "
m = s.model()
nodeIndex = [ m.evaluate(Nodes[i]) for i in range(totalNodeNumber) ]
print nodeIndex
else:
print "UNSAT"
print "No solution found !!"
If this is not a right way to solve the problem then could you please tell me what would be other alternative way to solve it. Can I encode this kind of problem to find optimal sequence of way points using Z3 solver?
I don't understand what problem you need to solve. Definitely, the way getVal is formulated does not make sense. It does not use the arguments n1, n2. If you want to examine values produced by a model, then you do this after Z3 returns from a call to check().
I don't think you can use a python function in your SMT logic. What you could alternatively is define getVal as a Function like this
getVal = Function('getVal',IntSort(),IntSort(),IntSort())
And constraint the edge weights as
s.add(And(getVal(0,1)==1,getVal(1,2)==2,getVal(0,2)==3))
The first two input parameters of getVal represent the node ids and the last integer represents the weight.
I have created a module with the below code
-module('calc') .
-export([sum/2]) . (0)
sum(L) -> sum(L,0); (1)
sum([],N) -> N; (2)
sum([H|T], N) -> sum(T, H + N) (3) .
and in shell, when I compile it is returning me error like below
calc.erl:5: head mismatch
calc.erl:2: function sum/2 undefined
error
As per my understanding from the book, 1 clause will receive the list and will pass it to the (3). Then (3) will return the desired result.
But I don't know where I have committed the mistake. Please help me on this.
And please help me to understand what is /2 in export statement.
You have a syntax error at line (1). The functions sum/1 and sum/2 are different, so your code should look like:
sum(L) -> sum(L,0). %% notice the . instead of ;
sum([],N) -> N;
sum([H|T], N) -> sum(T, H + N).
The /2 is the arity of your function, that is, the number of arguments it takes. So, in your case, the function you want to export is sum/1.
Check this link
Write a function that calculate the sum of integers in a list in Erlang
/2 in export statement indicates the number of arguments to the function sum.
From an old final for my class:
Here is some prolog code:
mystery(1, 1).
mystery(N, F) :-
N1 is N-1,
mystery(N1,F1),
F is F1*N.
Question 1: What value is unified with P in
mystery(3, P).
Question 2: If a semicolon is pressed after Prolog produces and answer for mystery, and interpreter will eventually report "ERROR: Out of local stack". Why dies this occur, and how could you modify mystery to avoid the error?
Question 1:
I get
P = 6 ?
Question 2:
If I press semi-colon to get all answers, I get an out of local stack error.
I'm not sure what this code is trying to accomplish or how to fix it so I don't go out of local stack. Any ideas?
The out of stack error is probably because, when you get down to mystery(1, F1) which resolves to mystery(1, 1) and ask for more solutions, you hit the next clause, which calls mystery(0, F1). At this point, it tries to find a solution, but the only clause that matches calls for mystery(-1, F1), and that calls for mystery(-2, F1) and so forth. This means that there's matches called for until either the first number wraps around to 1 or you get a stack overflow, since each pending match takes stack space.
#David Thornley already explained why searching for more answers blows up your stack. You could fix it by using a cut operator:
mystery(1, F) :- !, F is 1.
mystery(N, F) :-
N1 is N-1,
mystery(N1,F1),
F is F1*N.
The cut here makes it so that if the first argument is 1, the second rule may not be applied.