Permalink
Browse files

Add all files.

  • Loading branch information...
1 parent 60a17a4 commit 8b147f957c67baee2e8c5b647202f020c751f5fd @hczhu committed Sep 8, 2012
Showing with 15,434 additions and 0 deletions.
  1. +668 −0 Geometry-algorithms/Geometry.cpp
  2. +123 −0 ML-algorithms/logistic-MSE.py
  3. +573 −0 ML-algorithms/trainMAXENT.java
  4. +434 −0 Misc/Huffman.cpp
  5. +72 −0 Misc/LongestCommonSubsequence.cpp
  6. +106 −0 Misc/file_reader.cpp
  7. +310 −0 Misc/struct_print.cpp
  8. +402 −0 Misc/toolFunctions.cpp
  9. +94 −0 commons/common_header.h
  10. +187 −0 data-structures/FibHeap.cpp
  11. +52 −0 data-structures/LeftistTree.cpp
  12. +59 −0 data-structures/MinMaxQuery.h
  13. +697 −0 data-structures/RBTree.cpp
  14. +75 −0 data-structures/SMAWK.cpp
  15. +86 −0 data-structures/binaryHeap.cpp
  16. +77 −0 data-structures/common_header_zhc.h
  17. +89 −0 data-structures/data_structures_common.h
  18. +471 −0 data-structures/hash_table.h
  19. +104 −0 data-structures/hash_tables.cpp
  20. +67 −0 data-structures/im_vector.h
  21. +203 −0 data-structures/interval_tree.cpp
  22. +592 −0 data-structures/linked_list.h
  23. +671 −0 data-structures/mem_pool.h
  24. +153 −0 data-structures/segment_tree.h
  25. +445 −0 data-structures/static_RBTree.cpp
  26. +69 −0 data-structures/tester.cpp
  27. +157 −0 graph-algorithms/Bipartite_matching.cpp
  28. +280 −0 graph-algorithms/General_matching.cpp
  29. +140 −0 graph-algorithms/MaxFlowDinic.cpp
  30. +137 −0 graph-algorithms/Strongly_connected_component.cpp
  31. +122 −0 graph-algorithms/Undirected_graph_component.cpp
  32. +136 −0 graph-algorithms/chordalGraph.cpp
  33. +47 −0 graph-algorithms/graph-algorithms.cpp
  34. +231 −0 graph-algorithms/minCostFlow.cpp
  35. +35 −0 graph-algorithms/stableMarriage.cpp
  36. +76 −0 graph-algorithms/tree-algorithms.cpp
  37. +68 −0 language-features/11-21.cpp
  38. +91 −0 language-features/13-2.cpp
  39. +394 −0 language-features/5-9-6.cpp
  40. +439 −0 language-features/calculator.cpp
  41. +46 −0 language-features/class-definition.cpp
  42. +71 −0 language-features/new_syntax.cpp
  43. +55 −0 language-features/return_throw_performance.cpp
  44. +46 −0 language-features/var_argments.cpp
  45. +281 −0 numeric-algorithms/BigInteger.cpp
  46. +91 −0 numeric-algorithms/FFT.cpp
  47. +191 −0 numeric-algorithms/LPSimplex.cpp
  48. +634 −0 numeric-algorithms/Number.cpp
  49. +115 −0 numeric-algorithms/fraction.cpp
  50. +113 −0 numeric-algorithms/fractions.cpp
  51. +243 −0 numeric-algorithms/matrix_linear.cpp
  52. +30 −0 numeric-algorithms/numeric.h
  53. +139 −0 numeric-algorithms/prime_generator.cpp
  54. +136 −0 string-algorithms/2DMatcher.h
  55. +753 −0 string-algorithms/ACAuto.cpp
  56. +432 −0 string-algorithms/ACAuto_old.cpp
  57. +356 −0 string-algorithms/string-algorithms.cpp
  58. +124 −0 string-algorithms/suffixArray.cpp
  59. +99 −0 string-algorithms/suffixArray2.cpp
  60. +200 −0 string-algorithms/suffixTree.cpp
  61. +109 −0 testers/test_all.cpp
  62. +76 −0 testers/tester.cpp
  63. +82 −0 threads-lib/common_header_zhc.h
  64. +416 −0 threads-lib/parallel_framework.h
  65. +453 −0 threads-lib/parallel_framework_buffer.h
  66. +344 −0 threads-lib/parallel_framework_lock.h
  67. +525 −0 threads-lib/parallel_framework_semaphore.h
  68. +42 −0 topcoder-related/TopCoder_template.cpp

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,123 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2011 Google Inc. All Rights Reserved.
+
+"""One-line documentation for logistic-MSE module.
+
+A detailed description of logistic-MSE.
+"""
+
+__author__ = 'hczhu@google.com (Hongcheng Zhu)'
+
+import sys
+import math
+import getopt
+
+def readData(filename):
+ sys.stderr.write('Loading data file:'+filename+'\n')
+ X,Y=[],[]
+ Dim=-1
+ for line in file(filename):
+ #print line.strip().split(',')
+ t=map(float,line.strip().split(','))
+ if Dim!=-1 and Dim != len(t):
+ sys.stderr.write('Bad line:'+line)
+ continue
+ Dim=len(t)
+ Y.append(t[len(t)-1])
+ t.pop()
+ t.append(1.0)
+ X.append(t)
+ return X,Y
+def calculate_sigmod(X,Para):
+ res=0.0
+ for i in range(len(X)): res+=X[i]*Para[i]
+ return 1.0/(1.0+math.exp(-res))
+
+def prediction(X,Para):
+ Y=[]
+ for i in range(len(X)):
+ Y.append(calculate_sigmod(X[i],Para))
+ return Y
+def squaredSum(Y1,Y2):
+ res=0.0
+ for i in range(len(Y1)):
+ res+=(Y1[i]-Y2[i])**2
+ return res
+def doTraining(X,Y,itr_times):
+ eps=10.0
+ dim=len(X[0])
+ P=[0]*dim
+ Y1=prediction(X,P)
+ pre_sum=squaredSum(Y1,Y)
+ itr=-1
+ good=0
+ while eps>1e-6:
+ itr+=1
+ if itr>itr_times: break
+ Z=[]
+ for i in range(len(X)):
+ Z.append(2.0*(Y1[i]-Y[i])*Y1[i]*(1-Y1[i]))
+ new_P=list(P)
+ new_Y1=list(Y1)
+ for k in range(dim):
+ for i in range(len(Y)):
+ new_P[k]-=eps*X[i][k]*Z[i]
+ new_Y1=prediction(X,new_P)
+ new_sum=squaredSum(new_Y1,Y)
+ #print P,new_P
+ if new_sum<pre_sum:
+ pre_sum,P,Y1=new_sum,new_P,new_Y1
+ good+=1
+ if good==10:
+ eps*=1.1
+ good=0
+ else:
+ eps/=1.2
+ good=0
+ sys.stderr.write('Iteration #'+str(itr)+': error='+str(pre_sum)+' eps='+str(eps)+'\n')
+ return P
+
+def main(argv):
+ try:
+ #p: predict. d: data file(csv). t: iteration times
+ opts,args=getopt.getopt(argv,'p:d:t:')
+ except getopt.GetoptError,err:
+ sys.stderr.write(str(err))
+ sys.exit(2)
+ do_pred=False
+ datafile=''
+ parafile=''
+ itr_times=1000000000
+ for o,a in opts:
+ if o in ('-p'):
+ parafile=a
+ do_pred=True
+ elif o in ('-d'): datafile=a
+ elif o in ('-t'): itr_times=int(a)
+ if datafile=='':
+ sys.stderr.write('no data file\n')
+ sys.exit(1)
+ if do_pred and parafile=='':
+ sys.stderr.write('no para file for prediction\n')
+ sys.exit(1)
+ X,Y=readData(datafile)
+ sys.stderr.write('training sample size='+str(len(Y))+'\n')
+ sys.stderr.write('feature dimention='+str(len(X[0]))+'\n')
+ sys.stderr.write('itr times='+str(itr_times)+'\n')
+ if not do_pred:
+ P=doTraining(X,Y,itr_times)
+ for item in P: print '%f'%(item)
+ elif do_pred:
+ P=[]
+ for line in file(parafile):
+ P.append(float(line.strip()))
+ if len(P)!=len(X[0]):
+ sys.stderr.write('Dimension mismatched in parameter file.\n')
+ sys.exit(1)
+ Y1=prediction(X,P)
+ for i in range(len(Y1)):
+ print '%f\t%f'%(Y[i],Y1[i])
+
+if __name__ == '__main__':
+ main(sys.argv[1:])
Oops, something went wrong.

0 comments on commit 8b147f9

Please sign in to comment.