Concurrent signal assignment with vector in VHDL - concurrency

I'm trying to compile this code using GHDL and I get the error: '=>' is expected instead of 'not'. I want the code to not have any processes, neither implicit ones.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
ENTITY decoder IS PORT
(c, b, a, g : IN std_logic;
y : OUT std_logic_vector(7 DOWNTO 0));
END decoder;
ARCHITECTURE beh_decoder OF decoder IS
BEGIN
y <= "10000000" WHEN (g NOT (a AND b AND c)) ELSE
"01000000" WHEN ((a AND g) NOT (b AND c)) ELSE
"00100000" WHEN ((b AND g) NOT (a AND c)) ELSE
"00010000" WHEN ((a AND b AND g) NOT c) ELSE
"00001000" WHEN ((c AND g) NOT (a AND b)) ELSE
"00000100" WHEN ((a AND c AND g) NOT b) ELSE
"00000010" WHEN ((b AND c AND g) NOT a) ELSE
"00000001" WHEN ((a AND g) NOT (b AND c)) ELSE
"00000000";
END ARCHITECTURE beh_decoder;

The when expressions are using the NOT gate as multi-input gate.
g NOT (a AND b AND c)
If we substitute Q = (a AND b AND c) you have
g NOT Q
Which is not meaningful? And likely what GHDL is complaining about.
Did you want
g OR NOT (a AND b AND c)
or
g AND NOT (a AND b AND c)
Based on your comment, the new code might look like this. You will need to confirm the logic but it should compile.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
ENTITY decoder IS PORT
(c, b, a, g : IN std_logic;
y : OUT std_logic_vector(7 DOWNTO 0));
END decoder;
ARCHITECTURE beh_decoder OF decoder IS
BEGIN
y <= "10000000" WHEN (g AND NOT (a AND b AND c)) ELSE
"01000000" WHEN ((a AND g) AND NOT (b AND c)) ELSE
"00100000" WHEN ((b AND g) AND NOT (a AND c)) ELSE
"00010000" WHEN ((a AND b AND g) AND NOT c) ELSE
"00001000" WHEN ((c AND g) AND NOT (a AND b)) ELSE
"00000100" WHEN ((a AND c AND g) AND NOT b) ELSE
"00000010" WHEN ((b AND c AND g) AND NOT a) ELSE
"00000001" WHEN ((a AND g) AND NOT (b AND c)) ELSE
"00000000";
END ARCHITECTURE beh_decoder;

Related

c++ no suitable constructor exists to convert from "int" to "std::pair<int, int>"

I am having trouble resolving this error. I am not having any luck on Google when I search for this error.
no suitable constructor exists to convert from "int" to "std::pair<int, int>"
#include <utility>
using namespace std;
pair<int, int> solve(int s, int g)
{
return s % g != 0 ? (-1, -1) : (g, s - g);
}
The error squiggle is under the first s in the return where it is checking...
s % g != 0
I cannot figure out how to resolve this. In C# this would work.
public static (int, int) solve(int s, int g) => s % g != 0 ? (-1, -1) : (g, s - g);
(a, b) is not a pair, it's an expression using the comma operator. It evaluates both the a and the b, but the result of the expression is the b only. That's why it's complaining that it cannot convert your single int into a pair.
For example:
d = (a++, b+=3, c);
will:
add one to a;
add three to b; and
set d to whatever value c has.
If you want an actual pair, you should be using something like std::make_pair(a, b). In your particular case, that would be something like:
return (s % g != 0) ? make_pair(-1, -1) : make_pair(g, s - g);

Linear programming with minimum and maximum fixed cost

I need your help in the following optimisation problem.
I have a maximisation Mixed Integer linear programming problem.
I would like to consider the minimum & maximum fixed fee.
I've done it in this way..
cost = max(minimum fixed cost , cost rate * x)
cost >= minimum fixed cost
cost >= cost rate * x
cost = min(maximum fixed cost , cost rate * x)
cost <= maximum fixed cost
cost <= cost rate * x
But, This turns infeasible solution.
Would you please help me in optimising such a problem.
piecewise linear functions
I think what you mean is the following:
Let
A = minimum fixed cost / cost rate
B = maximum fixed cost / cost rate
Then you want to model the piecewise linear function:
cost = minimum fixed cost if x < A
cost rate * x if A <= x <= B
maximum fixed cost if x > B
Using piecewise linear functions inside a MIP model is not a problem. You can do this by different approaches:
using extra binary variables (see (1))
using SOS2 variables (see (1))
systems like AMPL and Gurobi have special facilities to express piecewise linear functions.
Example formulation
A formulation with SOS2 variables can look as follows:
Introduce data points px and py
px py
--------------------------
0 minimum fixed cost
A minimum fixed cost
B maximum fixed cost
C maximum fixed cost
where we assume 0<=x<=C. I.e. C is an upper bound on x.
Then do:
set p = {1,2,3,4}
sos2 variables lambda(p)
sum(p, lambda(p)) = 1
x = sum(p, lambda(p)*px(p))
cost = sum(p, lambda(p)*py(p))
See e.g. (2)
What is wrong with your approach
Note that your approach (shown in the question) is incorrect:
cost >= minimum fixed cost
cost >= cost rate * x
cost <= maximum fixed cost
cost <= cost rate * x
is really
minimum fixed cost <= cost <= maximum fixed cost
cost = cost rate * x
which limits x to A <= x <= B.
References
(1) H.Paul Williams, "Model Building in Mathematical Programming", Wiley
(2) GAMS: Piecewise linear functions with SOS2 variables
Piecewise linear function:
cost = 0.02 x if 0 <= x <= 500
0.03 x if 500 <= x <= 1500
0.04 x if 1500<= x <= 10000
SOS2 Solution:
x[a, b, c, d, e] = (x1[a, b, c, d, e] * 0) +
(x2[a, b, c, d, e] * 500) +
(x3[a, b, c, d, e] * 1500) +
(x4[a, b, c, d, e] * 10000)
cost[a, b, c, d, e] = (x1[a, b, c, d, e] * 0 * 0 )+
(x2[a, b, c, d, e] * 500 * 0.02)+
(x3[a, b, c, d, e] * 1500 * 0.03)+
(x4[a, b, c, d, e] * 10000 * 0.04)
x1[a, b, c, d, e] + x2[a, b, c, d, e] + x3[a, b, c, d, e] + x4[a, b, c, d, e]>= 0
x1[a, b, c, d, e] <= y1[a, b, c, d, e]
x2[a, b, c, d, e] <= y1[a, b, c, d, e] + y2[a, b, c, d, e]
x3[a, b, c, d, e] <= y2[a, b, c, d, e] + y3[a, b, c, d, e]
x4[a, b, c, d, e] <= y3[a, b, c, d, e]
y1[a, b, c, d, e] + y2[a, b, c, d, e] + y3[a, b, c, d, e] = 1
I've done it in this way. However, the solver still shows unfeasible solution. Can you see something wrong in this formulation?!

C++ Algorithm to determine all possible join-orders

bI am currently doing some research in databases.
I need to find all possible join orders for a given join graph. The graph is implemented as a adjancence list.
Example:
SELECT * FROM a, b, c, d WHERE a.x = b.x && b.y = c.y && b.z = d.z
gives us the following graph (edges):
(a,b)
(b,c)
(b,d)
and all possible join orders would be (x is the join operator):
((a x b) x c) x d
((a x b) x d) x c
(a x (b x c)) x d
(a x (b x d)) x c
a x ((b x c) x d)
a x ((b x d) x c)
My first idea was running kind of a PRIM algorithm for each vertex. Everytime, we could add different edges to connect a vertex, which is not part of the MST, to the MST, we found another join order. Does this approach work? Is there a simpler solution to get all possible join orders?
Moritz

standard ml sorting 3 elements

I'm trying to make a standard ml function which takes 3 elements as input and returns a sorted list which is sorted from smallest to largest. I used 3 helper methods that gets me the min, max and the mid elements. the codes are below:
- fun min3 (a, b, c):real =
if a < b andalso a < c then a
else if b < a andalso b < c then b
else c;
- fun mid3 (a, b, c):real =
if (a < b andalso a > c) orelse (a > b andalso a < c) then a
else if (b < a andalso b > c) orelse (b > a andalso b < c) then b
else c;
- fun max3 (a, b, c):real =
if a > b andalso a > c then a
else if b > a andalso b > c then b
else c;
- fun sort3 (a, b, c):real =
min3(a, b, c)::mid3(a, b, c)::max3(a, b, c)::[];
the following worked perfectly when dealing with ints, but when i changed them to reals, the helper methods returned the correct results but i get error when typing the sort method which is the following (couldnt copy the error text so i took a screen shot) :
what could be wrong in the code?
Thanks
Also, is there another way of sorting 3 elements other than the way i posted here or not?
When changing the types, you've made a mistake with the return value of sort3. The error message is telling you that you declared sort3 to return a real while in fact it returns a list of reals.

How to calculate modulus of the form (a*b)%c?

How to calculate modulus of the form (a*b)%c?
i want to calculate modulus of multiplication of two int numbers where they are almost in the stage of overflow...
here c is also int
(a * b) % c == ((a % c) * (b % c)) % c
What about ((a % c) * (b % c)) % c? Depending on your architecture this could be faster or slower than casting to a bigger type.
You may cast a and c to long long, so the multiplication won't overflow.
((long long)a * (long long)b) % c