Variable changing unseen - c++

I'm (trying to) write a program that's a sort of simplified neural net, where offby is my function to measure how close to the goal value the neural net gets on this iteration. It seems to be working completely fine except for the fact that (seemingly randomly) it sometimes assigns a negative value to offby.
The only block of code where offby can change is (aside from setting it back to 0 at the start of every iteration):
offby = offby + std::abs((sigmoid(outcome[k]) - goal));
After that bit of code I have it output the offby value, and it's working fine. Then I have the bit of code below (where it changes the value of one of the connections in the net).
std::cout << "Offby is before 5: " << offby << "\n";
std::uniform_int_distribution<int> which_unit(0, unitsinlayer3);
std::uniform_int_distribution<int> which_connection(0, unitsinlayer4);
one = which_unit(rng);
two = which_connection(rng);
thirdtofourth[one][two] = +mutateby;
std::cout << "Offby is after 5: " << offby << "\n";
At "offby is before 5" it still outputs the correct value, then at "offby is after 5" it sometimes outputs a negative value. There is literally no other code in between, the only reason I've even figured out it's this part of the code that assigns a negative value somehow is through having it output offby at practically every bit of code and seeing where it changes.
Now I might be missing something completely obvious, but how come changing these other variables might change my offby value?
In case this is relevant, mutateby is assigned as follows (earlier in the code)
std::normal_distribution<double> mutation(0, 0.1);
double mutateby = mutation(rng);
Just as an example, this just happened when I ran the program:
Offby is before 5: 0.000000
Offby is after 5: -0.056328
Any help would be much appreciated.
Edit: Turns out that it changes specifically in this line:
thirdtofourth[one][two] = +mutateby;

Related

Understanding the Syntax of Gps_traits_2::Polygons_with_holes_2 in CGAL and Additional Related Questions

Good day,
I am currently in the learning process of CGAL while being relatively new to C++. For my current project I need to use Minkowski sums and then do additional operations on the boundary of it.
However, before I do these additional operations I need to get a better understanding of the output of offset_polygon_2(), the exact Minkowski offset computation.
Question 1: What is the Syntax of the output for .outer_boundary?
From what I understand so far, it outputs a list of a conic circles defined here. I would also imagine you would need some kind of arc-angle range for each of these concic circles and origin point, correct? An example of the output goes something like this:
89 {-1*x^2 + -1*y^2 + 0*xy + 1400*x + 0*y + -489975} : (705,0) --ccw--> (700,5) {0*x^2 + 0*y^2 + 0*xy + 0*x + 0*y + 0} : (700,5) --l--> (699.97,5)...
Question 2: How do you use CGAL::draw() for the above?
I have the following code, but I am unsure of what else needs to be done before it can be drawn.
Offset_polygon_with_holes_2 offset = CGAL::offset_polygon_2(P, 5, traits);
double secs = timer.time();
std::cout << "The offset polygon has " << offset.outer_boundary().size()
<< " vertices, " << offset.number_of_holes() << " holes."
<< std::endl;
std::cout << "Offset computation took " << secs << " seconds." << std::endl;
Question 3: What other operations can be done on the "offset"?
So in the example code for Minkowski sums (also see above) offset.outer_boundary() is done, is there a list of other operations that can be done? Note: I do not think "operations" is the correct term here, please correct me.
I think that is all I have for now, thanks!

Am I Missing Something? Changing Dictionary Value

I've been messing around with this code for much longer than necessary. I'm changing to change the value of a dictionary entry depending on a person's choice.
while points <= 10:
print "You have " + str(points) + " points left.\n"
stats = {
"Strength": 0,
"Dexterity": 0,
"Constitution": 0,
"Intelligence": 0,
"Wisdom": 0,
"Charisma": 0
}
for i in sorted(stats):
print i + ": \t" + str(stats[i])
statInc = raw_input("\nWhere do you want to put your points? ").capitalize()
if statInc in stats:
points -= 1
stats[statInc] += 1
I started off with the stats[statInc] as a if/elif that specifies the strings by name. I can't get the values to change, but the point number will decrease accordingly. I know this because I originally had points set to 10.
I've never had this problem before with my other codes that revolved around dictionaries and their values. But I've tried tackling this from every angle and I feel like an idiot.
Nothing is changing because you are setting stats to {"Strength": 0,"Dexterity": 0,"Constitution": 0,"Intelligence": 0,"Wisdom": 0,"Charisma": 0} within your while loop. Every time it loops around, it will recreate stats, making it appear like it never changed.
The way to fix this would be to put the stats = {"Strength": 0,"Dexterity": 0,"Constitution": 0,"Intelligence": 0,"Wisdom": 0,"Charisma": 0} line before you enter your while loop.
You're re-instantiating your dictionary every time the loop evaluates. Move your initial stats declaration out of your loop (before it) so those values aren't reset continuously.
Note that you'll also want to test for while points > 0 instead of points <= 10 since you're starting at 10 and decrementing rather than starting at 0 and incrementing. You could also just test your max points value against sum(stats.values()) to be sure you're getting the current sum rather than using a counter variable, though in this case it doesn't really matter.

Trouble with output function C++

I am having trouble getting this program to output properly. It simulates a drunken sailor on a board that randomly goes one step to the left or right. At the end of the simulation, the program outputs the percentage of times he fell off the board vs not falling off. My percentage is always zero, and I can't figure out whats wrong with my code.
This function correctly outputs the "experiments" and "fallCount" variable, but always displays "fallCount / experiments" as zero.
This should read "After 2 experiments, sailor fell 1 time, fall percentage was 0.5%"
(if experiments = 2 and fallCount = 1) instead, its 0% every time.
Let me know what I am doing wrong. Thank you!
void outputExperimentStats(int experiments, int fallCount)
{
cout << "After " << experiments << " experiments, sailor fell "
<< fallCount << " time, fall percentage was " << fallCount / experiments << "%\n";
}
That is because you are using integer division. There are no decimals, so things get truncated. E.g.
1 / 2 --> 0 // integer division
This is correct, and expected behavior.
To get the behavior you want, use double or float.
1.0 / 2.0 --> 0.5 // double division
In your example, you can either change the types of your inputs to double or if you want to keep them int, you can convert them during the division
static_cast<double>(fallCount) / static_cast<double>(experiments)

finding the big oh estimate of the a simple print function

while (num > 1)
{
cout << "num is now " << num << endl;
cout << "hello // \n";
num /= 2;
}
I am trying to give the big O estimate for the print statements.
The user gets to input num. I tried a few inputs and am starting to see a pattern.
1 gives 0 print.
2-3 gives 1 print.
4-7 gives 2 prints.
8-15 gives 3 prints.
16-31 gives 4 prints.
let p = the number of prints. I see that the range of numbers giving you a certain amount of prints is 2^p.
So is the big O estimate 2^p ?
You are definitely thinking in the right direction and what is more important - you are approaching the problem in the correct manner. Still your final conclusion is a bit off the target. Try to use 2p for the values you've computed and you will see that the number of prints is not 2p, but rather the inverse of this function.
It's O(log N). The reason is that your halving the range with each iteration. If you're familar with binary search then your loop if doing something similar.
You've correctly observed the 2^p pattern. The inverse of this is log base-2 (rather than log base-10). When people mention log in big-O notation they typically mean the base-2 version.
just use the master theorem.
Link here
b = 2 f(n) = 1 a = 1.

c++ ignoring same number in an array

I have an array of random numbers, for example
6 5 4 4 8
I need to sort it and remove/ignore the same numbers while printing afterwards, so what I did is I sorted everything with bubble sorth algorithm and got something like this
4 4 5 6 8
Now in order to print only different numbers I wrote this for loop
for(int i=0;i<n;i++){
if(mrst[i]!=mrst[i-1] && mrst[i]>0){
outFile << mrst[i] << " ";
}
}
My question is, the array I have is at the interval of [0:12], though the first time when I call it, it checks an array index of -1 to see if there was the same number before, but it doesn't really exist, but the value stored in there usually is a huge one, so is there a possibility that there may be stored 4 and because of it, the first number won't be printed out. If so, how to prevent it, rewrite the code so it would be optimal?
Perhaps, you're looking for std::unique algorithm:
std::sort(mrst, mrst + n);
auto last = std::unique(mrst, mrst + n);
for(auto elem = mrst; elem != last; ++elem)
outFile << *elem << " ";
Well, as you noted already, you cannot do the check mrst[i] != mrst[i-1] in case i == 0. So I'm sure you can think of a way of not doing that check in exactly this case ... (This looks very much like a homework assignment, so I'm not really willing to give you a complete solution, but I guess I hinted enough)
Note also that it's undefined behaviour to access memory outside the boundaries of an array, so what you're doing there can do anything from working correctly to crashing your program, entirely at the discretion of the compiler.
Basically you can read from any place in heap. So mrst[-1] may give you some garbage from the memory. But you really should avoid doing this. In your case you can just change "mrst[i]!=mrst[i-1] && mrst[i]>0" to "i==0 || mrst[i]!=mrst[i-1]".
In c++ "A || B" don't execute "B" if the "A" is ok.